Search code examples
asp.netajaxupdatepanel

How To Update TextBox using Update panel in asp.net


I Have a TextBox outside the UpdatePanel and a Button inside the Updatepanel When i click on the Button it show value in TextBox.

I Have Write Following Code.

<%@ Register TagPrefix="AjaxToolKit" Assembly="AjaxControlToolkit"         
    Namespace="AjaxControlToolkit" %>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" 
         EnablePageMethods="true" />
    <asp:TextBox ID="TextBox5" runat="server" />
    <asp:UpdatePanel runat="server" ID="Up1">
       <ContentTemplate>
           <asp:Button ID="btn" runat="server" onclick="btn_Click" />
       </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

----------Code Behind---------

protected void btn_Click(object sender, EventArgs e)
{
    TextBox5.Text = "20000";
}

Solution

  • Your textbox value can't be updated untill you put it in an update panel.

    <ContentTemplate>
      <asp:TextBox ID="TextBox5" runat="server" />
      <asp:Button ID="btn" runat="server" onclick="btn_Click" />
    </ContentTemplate>
    

    OR, it would be better if you register a trigger of the button and pull your button out from your update, like...

    <asp:UpdatePanel runat="server" ID="upnl" UpdateMode="Conditional" >
    <ContentTemplate>
        <asp:TextBox ID="TextBox5" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>