What I have is a popup defined within a user control, that is opened within a taskbar user control, which is attached to a master page, such that:
MasterPage.TaskBarUC.PopupUC
Within the popup, there are two buttons, one to save what the user is trying to do within a database, and one to cancel. I have some codebehind that does the work when the user clicks either button.
So every page has the taskbar, and they can click a button on the taskbar to access the popup and save the information. It works perfectly. However, there is one specific page within the site that needs to access the same popup from a button located on the page, as well as from the taskbar.
The button works, and it opens the popup, but for some reason, and only in this case, the "Save" button doesn't activate my codebehind. It DOES perform the validation that I have it do though. What is also weird is that the cancel button, which also activates some codebehind, does do what its supposed to.
I tried putting a breakpoint right at the beginning of my "Save" event handler, and it never reaches it. Its as if the button is only validating, and completely ignoring its OnClick event.
.aspx
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"
ValidationGroup="Validation" />
I guess what happens is that the validation process has failed, which cancel the handling of the Button.Click event.
To check this possibility try to set the value of the Button.CausesValidation property to false and see if the Button.Click handling is going to continue or not.
Button.CausesValidation property to false worked for me. I am using required field validator for different button. I think this could stop post back even for all other button click events (serverside).
Related
I was doing some research on my project (ASP.NET Web Forms) and came across 2 event types that seem similar, OnCommand and OnClick. I have read through the MSDN pages but I am still unclear on the differences between them.
What is the difference between OnCommand vs OnClick and when would I want to use which event?
//edited to specify for type of app
If you find the documentation a bit unclear, that's understandable.
What happens is that OnClick is a lower-level event than OnCommand. You can think of OnClick as being more like a physical event, and OnCommand as being more like a logical event, so to speak.
When the control is clicked, it receives an OnClick event which tells the control that there has been a click. The control may do various things as a response. If the control is associated with a command, and if default processing of the event is allowed to take place, then the system will follow this with an OnCommand event.
(I am not sure / don't remember how much control you have over the suppression of default processing in WinForms, but you can certainly control that in the underlying Win32 programming layer.)
The OnCommand event may be triggered by means other than a click. For example, you may use a keyboard shortcut to activate a control, in which case there will be no OnClick.
The knowledge to take home from all this is the following:
Never use OnClick when OnCommand will suffice.
If you write your program to use OnClick for triggering Business Logic operations, then your program will require the user to use the mouse. There are many users who cannot use the mouse due to disability, there are many users who simply prefer the keyboard over the mouse, and there are many devices that do not even have a mouse.
(On those devices a click will often be simulated by a touch, but that's not guaranteed, and in any case all these clunky simulations should have never been necessary in the first place, the only reason they are added is because programmers were careless in the first place and they were doing silly things like triggering actions on OnClick instead of OnCommand.)
It would be okay to use OnClick if you were writing your own control. For example, if you were writing your own edit box control, and you wanted the user to be able to click somewhere within the control in order to place the caret at that exact location, then it would be okay to do that as a response to the OnClick event being triggered. But note how this action is for internal use of the control only, it does not trigger any action which is external to the control.
This is straight from the msdn page https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
But trying to explain with little verbosity:
OnCommand is used to pass extra arguments to the handler. For example, if you have two buttons to sort list ascending and descending. Instead of writing separate OnClick handlers for each button , you can define single handler for both buttons and pass CommandName and CommandArgument
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "Sort":
// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;
}
}
so here you have single handler where you can handle multiple buttons' clicks depending on what command argument buttons are passing.
Within the MSDN documentation it states in the remarks section the key difference.
With OnCommand:
The Command event is raised when the Button control is clicked. This event is commonly used when a command name, such as Sort, is associated with the Button control. This allows you to create multiple Button controls on a Web page and programmatically determine which Button control is clicked.
With OnClick:
The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button).
So, if you just want some code to execute when a button is clicked, but don't care about specific execution for specific cases then OnClick is just fine.
Edited to add: With OnCommand you could potentially have one handler that handles events from multiple buttons and executes the proper code based on the command name that's passed.
I have a page that consists of an updatepanel with a DropDownList, a textbox and a button in it. The updatepanel has a trigger for the button. This works fine for all my "error" cases, such as "No input!", when I pop an alert with
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),"noName", "window.onload = function(){alert('Please enter a username!');window.location ='Accounts.aspx';}", true); and the like.
But when I don't hit the alerts and the button event runs all it should I seem to land in some dead zone. My alerts stop displaying even though the code runs (checked by debugging), the button event does still preform all tasks as it should. However, if I refresh the page I get the classic:
To display webpage again. After I click retry my "latest" message pops up and then it works just like on first load.
So, I'm guessing that the PostBackTrigger on my button triggers a PostBack before or during the event execution since when the event is done nothing more happens until I reload. I'm thinking I need to add a reload at the end of my button event. Therefore my question is: How do I add a reload of the page/panel at the end of a button event?
Edit:
Forgot to mention that when the button event runs all it should it edits the web.config file, if this somehow could cause my issue.
I found a solution to my problem, by removing my triggers and instead using:
ScriptManager1.RegisterPostBackControl(Button);
Then I could also put an:
UpdatePanel1.Update();
At the end of my button event.
I have a requirement to check for a certain condition on postback before redirecting (Response.Redirect) to another page.
Note... I cannot use JavaScript to detect whether or not to confirm (this is also a requirement) :s
Pseudo:
protected void lbtnRedirect_OnClick(object sender, EventArgs e)
{
if (showConfirm)
{
// Set flag for client side
this.ShowConfirm = true;
// Track this event for next postback.
}
else
{
Response.Redirect("somepage.aspx");
}
}
If the showConfrim flag == true, then the client will be show a modal dialog box asking them if they are sure they want to redirect. If the user clicks on "Yes", then the page posts back and the desired effect is that the lbtnRedirect_OnClick event is fired. How would I about tracking the lbtnRedirect event?
Edit:
I have no problem tracking the flag to show the modal (yes JS must be used to show the modal... somethings you just cannot get rid of :)). I should have been more clear.
It is when the user clicks "Yes" to continue the redirect. The page will postback again but needs to know which event to go through.
i.e. Suppose there are 3 onclick events, 1) lbtnRedirect1_Onclick 2) lbtnRedirect2_OnClick 3) lbtnRedirect3_OnClick... each of which does the confirm check.
Each onclick event does the check. So when the user clicks on "Yes" on the modal, how does the page know which event to drop back into?
You can use ViewState if you're in WebForms.
Implement a ShowConfirm property encapsulating ViewState["ShowConfirm"].
In the first postback you'll set ShowConfirm 'true', and this will activate that modal during the render (if ShowConfirm is true, that's setting as visible 'true' some control).
In the next postback, you'll set ShowConfirm 'false' because is 'true', and finally you'll do the whole redirect!
You can use an ajax call from javascript to set the required values.
Since the postback will happen before even the execution reaches to your button click event we need a workaround here, And if you don't need JS as your requirement, so take a look at
Implementing Client Callbacks Programmatically without Postbacks in ASP.NET
This is much like a wrapper for XMLHttp Ajax call IMHO.
You cannot easily create a model form, without javascipt.
One suggestion I would make is to have panels in your page.
Panel one is visible.
On submit one; panel one hides and panel two is visible asking for a confirmation.
On panel two is a confirm button, clicking this button your redirection is performed.
I have a button, contained in a panel, with a click event, that works fine. However when a users presses another button, I need to move this button into another panel (this is actually a panel with a modalpopupextender), so I this code to do so:
newPanel.Controls.Add(buttonPanel)
It all get's moved and looks fine. However now when the button is clicked it doesn't fire the associated event. I have tried re-adding the event in the page_init, with this code
((Button)this.FindControl("serverModalSave")).Command += new CommandEventHandler(modalSave_Click);
But with no luck. How can I get this button to fire it's click event when moved, and why does it stop working when it's moved?
EDIT:
This Button needs to be added to a panel specified by the user at run time, so there is not a way to determine where the button will go in advance.
I could instead of moving this button, create a new one, but because this button is not created in the page_init I am having issues getting that to fire an event either.
Instead of moving the button, have another button on the other panel set to hidden.
Hide the button you wanted to move and show the hidden one when needed.
Moving the control changes the naming hierarchy and now the button can't be found and the click event can't fire.
This is due to how the page life cycle works. Here is a good (if somewhat dated) article about how view state works - if you understand this, you will understand what went wrong.
If you are creating the button in the new panel, when this button is then clicked do you re-create it in the postback ?
You must re-create all controls on each postback see here
I would like to close a modal form when the user clicks outside (anywhere on the computer desktop) the modal form. How can we do this as a modal form is not meant to lose focus.
You need to hook mouse (and keyboard if required) and capture their events. Then check if the click happened outside the form (and area). If yes, flag a sign which can be read by the model form that it can close down.
Algo:
Hook mouse click event.
When callback function is called, check for the click position - if it's inside your form or not (you might need to translate the locations to Desktop locations - I hope you know how to!)
If the point is outside the form, set a flag (boolean or anything that makes you happy). Make sure the form can read the flag somehow.
Trigger an event for form to capture. In it's handler read the flag status. If true, close/unload the form.
This page will tell you technical details and functions.
I don't think you need to make it modal... then you can take siride's option of closing it on the Deactivate event.
The reason you don't need to make it modal: The first time you display it, it will have the focus and be topmost. Modal prevents you from clicking somewhere else, but you want to be able to click somewhere else... and when you do, the form goes away, so there are no modal needs.