Monday, April 19, 2010

Playing with Update Panel in asp.net

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.

protected void btnHelloWorld_Click(object sender, EventArgs e)
{
    txtHello.Text = "This is without postback";
}

here it will simple fill the textbox with string “This is without post back”. Now lets create trigger like following.

<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.

Technorati Tags: ,,,

Shout it

kick it on DotNetKicks.com
Share:

7 comments:

  1. it's very useful post....
    As this question is ask so many time in the interview

    Good one

    Thank

    ReplyDelete
  2. it works without triggers part. then what is the use of triggers?

    ReplyDelete
  3. @ 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

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews