Archive for the ‘ASP.Net’ Category
ASP.Net UpdatePanel Triggers
Any control residing inside an updatepanel automatically causes a partial refresh and updates the update panel. However, sometimes you want to trigger an update on an update panel from a control that is outside of the control panel and that is where triggers come in.
<asp:UpdatePanel ID="MyUpdatePanel" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="MyButton" EventName="OnClick" /> </Triggers> <ContentTemplate> </ContentTemplate> </asp:UpdatePanel>
This works fine a dandy however if you are in some sort of template e.g. a create user wizard template you are bound to get the following error
A control with ID ‘MyButton’ could not be found for the trigger in UpdatePanel ‘MyUpdatePanel’.
My first approach was to use the ScriptManager RegisterAsyncPostBackControl
Master.GetScriptManager.RegisterAsyncPostBackControl(MyButton);
Two Notes
<li>The script manager is accessed via a property on the master page
<li>The MyButton is referenced via a findcontrol call
This does not work since the postback is not tied to an update panel and causes a full postback. The solution is to register a trigger on the update panel from the code behind page like so
UpdatePanel MyUpdatePanel = ControlFinder.FindChildControl<UpdatePanel>(Page, "MyUpdatePanel"); AsyncPostBackTrigger MyUpdatePanelTrigger = new AsyncPostBackTrigger(); MyUpdatePanelTrigger.ControlID = MyButton.UniqueID; MyUpdatePanelTrigger.EventName = "Click"; MyUpdatePanel.Triggers.Add(MyUpdatePanelTrigger);
Calling ontextchanged from JavaScript
At times you want to trigger a server ontextchanged event from javascript. A typical example would be a javascript popul calendar, the data selection is done on the client by as soon as a date has been selected some server event should happen.
From javascript call this method
document.getElementById(‘TextBox’).onchange();
From asp.net
The two tricks are
1) Call onchange from javascript
2) Have AutoPostBack set to true on the asp.net textbox control
ReLoad/Refresh an asp.net page
If you mix asp.net and javascript you are bound to run into the following error:

This error is caused when you postback a page and than have some javascript reload the page. The common javascript that causes this error is document.location.reload().
Instead of using the reload command use this instead document.location.href=’url’; it has the same effect as the document.location.reload() except IE does not throw the error since you are reloading the page explicitly instead of having the brower reload the page for you and throwing up the warning.
Having this message come up should raise a red flag that you are mixing asp.net style development (Post-Backs) and client side javascript. Their is not really anything wrong with either development style just don’t mix them.