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?
Related
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?
I have a custom user control which contains a textbox and some other logic / controls, including a custom validator. When dropping my custom control on an aspx page, i want to be able to attach a method to the customer validator within the control, by providing a value for the OnServerValidate property in the html.
How can this be done? I want to be able to pass the validation method name as a property in the user control's html, rather than having to attach to the custom validator's event through the code behind.
You can wrap custom validator's ServerValidate event into your own, and then use it in the markup for the handler assignment. In you control all that is needed is a proper declaration of the event:
public event ServerValidateEventHandler ServerValidate
{
add { this.CustomValidator1.ServerValidate += value; }
remove { this.CustomValidator1.ServerValidate -= value; }
}
Now in the markup it is possible to sign up for this event, effectively signing up for the custom validator's event at the same time:
<yourTagPrefix:YourControlName
OnServerValidate="YourControlName_ServerValidate"
runat="server"
... />
when I click a button the content inside my updatepannel changes. The id of the button is set as a trigger for the updatepannel, so that the other updatepannels are not affected.
How can I replace the buttonclick event with a page load event?
Is there a way to specify the pageload javasript event as a trigger for the updatepannel?
There's no way to register pageLoad() as a trigger, but you can call __doPostBack() in that function to force your UpdatePanel to refresh itself. However, that will cause an infinite loop if you're not careful, because pageLoad() is also called when partial postbacks complete.
One way around that problem is to set a flag before refreshing the panel, and persist that flag in the window object so it survives partial postbacks:
function pageLoad()
{
if (!this.panelRefreshed) {
this.panelRefreshed = true;
__doPostBack("yourUpdatePanelClientID", "");
}
}
I have a web user control (ascx) that exposes an "ID" property. What I want to do is set this property when the SelectedIndexChanged event fires in a gridview in the containing page. However, I can't seem to do it.... Here's my code:
protected void grdPhysicians_SelectedIndexChanged(object sender, EventArgs e)
{
physicians_certif1.mdID = grdPhysicians.SelectedDataKey.ToString();
mvData.SetActiveView(viewEdit);
panAdditional.Visible = true;
}
Physicians_certif1 is the user control. It seems the user control is loading before the SelectedIndexChanged event has a chance to set it's property.
Any ideas folks?
ASP.Net page lifecycles can be hard to understand especially with ascx user controls which also have their own lifecycle. If you are setting the mdID property in Page_Load of either the page or the ASCX control or have hardcoded a default value into it in the XHTML, it is probably being reset after SelectedIndexChanged fires.
Set a breakpoint in grdPhysicians_SelectedIndexChanged, set a watch on physicians_certif1.mdID and step through the code using the debugger.
Yes, that is exactly what is happening. You should look at (and be familiar with) the following resource:
ASP.Net Page Life Cycle
The page will load, then the control will load, then your events will begin to fire. If you have configuration needs based on event triggers, it is best either to place those configurations in the Page_LoadComplete or Page_PreRender events of the user control in question or apply "Rebinding" instructions in the Set method of your property:
public MyValue MyProperty()
{
get
{
return _myProperty;
}
set
{
RebindMyControls();
_myProperty = value;
}
}
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;