While taking interview for asp.net I often asked questions regarding update panel and how they works and how we can asynchronous post back using update panel without post backing whole page. To my surprise most of people don’t know how to use update panel using triggers. Let’s create simple example to demonstrate use of trigger.
We will take one button and then we will use a textbox control and when button’s click event fires its will change textbox text value and fill textbox with that value. But lot’s people here believes that button should be inside update panel but that’s is not true you can use trigger of a update panel for any control outside update panel.
First let’s take look of server side code of button click. Following is a code for that.
here it will simple fill the textbox with string “This is without post back”. Now lets create trigger like following.
It’s simple and you don’t need button insides the update panel. Hope this will help you.
We will take one button and then we will use a textbox control and when button’s click event fires its will change textbox text value and fill textbox with that value. But lot’s people here believes that button should be inside update panel but that’s is not true you can use trigger of a update panel for any control outside update panel.
First let’s take look of server side code of button click. Following is a code for that.
protected void btnHelloWorld_Click(object sender, EventArgs e) { txtHello.Text = "This is without postback"; }
<asp:ScriptManager ID="myScriptManager" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="updTextbox" runat="server"> <ContentTemplate> <asp:TextBox ID="txtHello" runat="server"></asp:TextBox> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="btnHelloWorld" EventName="Click" /> </Triggers> </asp:UpdatePanel> <asp:Button ID="btnHelloWorld" runat="server" Text="Submit" onclick="btnHelloWorld_Click"/>
It’s simple and you don’t need button insides the update panel. Hope this will help you.
Nice one....
ReplyDeleteNice one.
ReplyDeleteso simple!
ReplyDeleteits very good
ReplyDeleteit's very useful post....
ReplyDeleteAs this question is ask so many time in the interview
Good one
Thank
it works without triggers part. then what is the use of triggers?
ReplyDelete@ Swagat- Trigger are for specific events. If you don't specify the trigger it will update every time if any event of any control in update panel will fire. That will be a performance problem. So if you specify trigger it will update once that event will fire. Like in above example we have click event of button. So it will update only on click event of button.
ReplyDelete