I have an ASP.NET WebForms page with several buttons added programmatically like this:
private void AddExportButton(Control control, Action clickAction) {
LinkButton exportButton = new LinkButton {
Text = "Export",
EnableViewState = false /*Buttons will be recreated on each Postback anyway.*/
};
exportButton.Click += (sender, e) => clickAction();
control.Controls.Add(exportButton);
}
Now this works, as long as the AddExportButton() method is called along the path from the OnLoad() or OnPreLoad() method. It does not fire the handler action however, when AddExportButton() called from the OnLoadComplete() method.
I would like to add/create the buttons also when another event handler (coming from a dropdown) gets called. This only happens after the OnLoad(), which will break my code.
Why is this, and how can I use anonymous methods as event handlers in this case?
See this nice cheat sheet about the ASP.NET Page LifeCycle by Léon Andrianarivony for more info about the order of the page/control creation.
In the page life cycle, the internal RaisePostBackEvent method (which raises the button's Click event) occurs between OnLoad and OnLoadComplete. If you wait until OnLoadComplete to add the LinkButton and hook up its Click event, then obviously the event won't be raised: it's too late.
(The fact that you're using an anonymous method is irrelevant.)
Can you add the export button in the .aspx but set its Visible property to false when you don't want it to appear?
Hot to disable devexpress button after first postback occur so user couldn't submit it twice.
I have tried to disable it with onClientClick event using javascript but that would also disable the button when some of validators is still not valid too.
Since i use update panel as ajax call. i come across this way.
function pageLoad() {
if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AjaxBegin);
}
}
function AjaxBegin(sender, args) {
btnxSignUp.SetEnabled(false);
}
When you have multiple UpdatePanels on a page, is there a way, in the code behind, find out which Update Panel triggerred the postback? It appears that the Request["__EVENTTARGET"] is not a reliable way of doing this.
An UpdatePanel doesn't trigger PostBacks, it intercepts them. The originator of the PostBack would be something like a button. If you have event handlers for all your interactive elements, you naturally know which one fired by which event handler runs.
you can get the id of the postback element on the client with the following
function pageLoad(sender, args) {
// add function to the PageRequestManager to be executed on async postback initialize
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
}
function InitializeRequest(sender, args) {
if(args._postBackElement.id === 'id_of_element_in_question' {
// do whatever
}
}
to get it on the server, presumably you'll know which control/event raised the postback as it will be handled in the relevant event handler in the code-behind.
How to dynamically Change DefaultButton Property of the Form from UserControl present in a page.Page is loaded into a master page contains Update Panel.
When Try to change the Default button in onload event of the UserControl, It is not changing.
if(lastpage)
{
this.Page.Form.DefaultButton = btnSave.UniqueID;
}
else
{
this.Page.Form.DefaultButton = btnNext.UniqueID;
}
Quote from msdn:
The following scenarios for setting
the default postback button on a page
are not supported during asynchronous
postbacks:
* Changing the DefaultButton programmatically during an asynchronous post back.
http://msdn.microsoft.com/en-us/library/bb386454.aspx
Could you not postback the state of the form and then work out which action to take rather than passing this information based on which button was clicked?
I have a button control. Once the user clicks on it, the click event should fire and then the button should get disabled. How can I do this? I have the option to use JQuery or JavaScript or both.
Here is my button declaration:
<asp:Button
ID="Button1"
runat="server"
Text="Click Me"
onclick="Button1_Click"
/>
On the button click code behind, I have added a Response.Write(). That should get executed and then the button should be disabled
For whatever reason, the HTML spec dictates that disabled elements should not be included in POST requests. So, if you use JavaScript to disable the HTML element in the client-side onclick event, the input element will be disabled when the browser assembles the POST request, the server won't be properly notified which element raised the postback, and it won't fire server-side click event handlers.
When you set the UseSubmitBehavior property to false, ASP.NET renders an input element of type button instead of the regular input of type submit that the ASP.NET Button control normally generates. This is important because clicking a button element does not trigger the browser's form submit event.
Instead of relying on a browser form submission, ASP.NET will render a client-side call to __doPostBack() within that button element's onclick handler. __doPostBack will raise the postback explicitly, regardless of what POST data comes through in the request.
With the postback being raised independent of the browser submit event, you're freed of the previously mentioned HTML quirk. Then, you can set an OnClientClick of "this.disabled = true;", which will render as "this.disabled = true; __doPostBack('Button1', '');", and things will work as intended.
add an OnClientClick="this.disabled = true;" to your button.
If you are using Asp.net Ajax you might want to look at using PostBack Ritalin.
Have you tried this?
Add an OnClientClick="MyFunction();" to your .NET button.
Then in the .aspx page script tags you add the following JS function:
<script type="text/javascript">
function MyFunction()
{
window.setTimeout(function ()
{
// get the button/control to disable using your favourite clientside ...
// ... control grabbing code snippet ...
// ... eg. JQUERY $('Button1'), getElementById, etc.)
document.getElementsByName('Button1').Button1.disabled = true;
// I've used "getElementsByName" because .NET will render a button with
// ... a "name" attribute, and not an "id" attribute, by default
}, 1);
}
</script>
This gives the browser a chance to post back, followed by a quick button disable.
You need to be careful that the postback occurs before you disable the button through client script. This is a common gotcha with ajax and input boxes. Disabling an input box prevents the data from being sent from the browser, even if you can see text within it while it is disabled. The answer is that you need to use jquery for this to ensure the server-side code runs first before it is disabled.
-Oisin
// to disable
this.setAttribute('disabled', true);
// to enable
this.removeAttribute('disabled');
this is a cross browser solution
There is really cool event for body tag "<"body onBeforeunload="buttonId.disabled = true;" ">"
This event triggers right before form submits, in other words your data will be submitted correctly.
When using the "this.disabled = true" method make sure you check if the page is valid before disabling the control if you have validators on the page. If validation fails you won't be able to re-enable the control without reloading the page.
if (Page_IsValid) this.disabled = true;
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
document.getElementById('<%= lblMessage.ClientID %>').innerText = "Processing...";
document.getElementById('<%= btnSubmit.ClientID %>').innerText = "Processing";
args.get_postBackElement().disabled = true;
}
</script>
Add Script Tag in source page . change Id of button in code . You can disable the button till the process completes execution .
you can disable it server side
Button1.Enabled = false;