Programmatically updating one update panel elements from another update panel elements
- by Jalpesh P. Vadgama
While taking interviews for asp.net candidate I am often asking this question but most peoples are not able to give this answer. So I decided to write a blog post about this. Here is the scenario. There are two update panels in my html code in first update panel there is textbox hello world and another update panel there is a button called btnHelloWorld. Now I want to update textbox text in button click event without post back. But in normal scenario It will not update the textbox text as both are in different update panel. Here is the code for that. <form id="form1" runat="server">
<asp:ScriptManager ID="myScriptManager" runat="server" EnableCdn="true"></asp:ScriptManager>
<asp:UpdatePanel ID="firstUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtHelloWorld" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="secondUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnHelloWorld" runat="server" Text="Print Hello World"
onclick="btnHelloWorld_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
Here comes magic!!. Lots of people don’t know that update panel are providing the Update method from which we can programmatically update the update panel elements without post back. Below is code for that.
protected void btnHelloWorld_Click(object sender, System.EventArgs e)
{
txtHelloWorld.Text = "Hello World!!!";
firstUpdatePanel.Update();
}
That’s it here I have updated the firstUpdatePanel from the code!!!. Hope you liked it.. Stay tuned for more..Happy Programming..
Technorati Tags: UpdatePanel,ASP.NET