Change text in TextBox outside of UpdatePanel

Saritha.S.R picture Saritha.S.R · Sep 20, 2013 · Viewed 7.6k times · Source

I have a gridview inside an UpdatePanel (the gridview is showing in a popup). On click of a select button in that grid, I am trying to set a textbox text in the page. But its not working; if I remove the update panel then it will work.

This is my code in aspx:

<div><asp:TextBox ID="txt" runat="server /></div>
     <asp:UpdatePanel ID="updLendersearch" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnLenderSearch" EventName="Click" />
        </Triggers>
        <ContentTemplate>
        <div id="divLender" runat="server" class="white_content" style="height: 450px;top: 20%;width: 57%;">
        <asp:Label ID="lblBenificiary" runat="server" Text="Beneficiary/Lender :" Font-Names="Candara" ></asp:Label>
        <asp:TextBox ID="txtBeneficiaryName" Style="border: 1px solid red" runat="server" Width="80px" CssClass="txtboxes" Font-Names="Candara" ></asp:TextBox>
        <asp:RequiredFieldValidator ID="reqBeneficiaryName" runat="server" ErrorMessage="*" ForeColor="Red" ControlToValidate="txtBeneficiaryName" ValidationGroup="lender"></asp:RequiredFieldValidator>
        <asp:Label ID="lblLenderState" runat="server" Text="State :" Font-Names="Candara" ></asp:Label>
          <asp:DropDownList ID="ddlLenderState" runat="server" AutoPostBack="true" Style="border: 1px solid red" 
                        AppendDataBoundItems="true" CssClass="drpdown" OnSelectedIndexChanged="ddlLenderState_SelectedIndexChanged">
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="reqLenderState" runat="server" ErrorMessage="*" ForeColor="Red" ControlToValidate="ddlLenderState" ValidationGroup="lender"></asp:RequiredFieldValidator>
                    <asp:Label ID="lblLenderCity" runat="server" Text="City :" Font-Names="Candara" ></asp:Label>
                      <asp:DropDownList ID="ddlLenderCity" runat="server" Width="100px" AutoPostBack="true"
                        AppendDataBoundItems="true" CssClass="drpdown" OnSelectedIndexChanged="ddlLenderCity_SelectedIndexChanged">
                    </asp:DropDownList>
                    <asp:Label ID="lblBeneficiaryZip" runat="server" Text="Zip :" Font-Names="Candara" ></asp:Label>
                     <asp:DropDownList ID="ddlBeneficiaryZip" runat="server" AppendDataBoundItems="true" Width="100px"
                            AutoPostBack="true" CssClass="drpdown">
                        </asp:DropDownList>
                        <asp:Button ID="btnBenefeciary" ValidationGroup="lender" runat="server" Text="Search" Font-Names="Candara" CssClass="btnBenefeciary" OnClick="btnBenefeciary_Click"/>
                        <br><br><br><br>
                         <div>
    <asp:GridView ID="grvLenderDetails" CssClass="GridViewStyle" ShowHeaderWhenEmpty="true" OnRowCommand="grvLenderDetails_RowCommand" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="true">
    <Columns>
    .. .. ..
    </Columns>
    <EmptyDataTemplate>
            No Records To Display
        </EmptyDataTemplate>
    </asp:GridView>
    </div>
        </div>
        <div id="fadeLender" class="black_overlay" runat="server">
            <asp:ImageButton ID="imgLenderClose" ImageUrl="../Images/closepnlbtn.png" runat="server"
                align="right" Style="margin-right: 140px; margin-top: 78px; border: 0px" OnClick="imgLenderClose_Click" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

Answer

Tim Schmelter picture Tim Schmelter · Sep 20, 2013

If the TextBox is in another UpdatePanel. Make its UpdateMode Conditional which allows you to call its Update method programmatically.

protected void ddlLenderState_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
    TxtInfo.Text = "Hello, i'm coming from the GridView.";
    UpdInfo.Update();
}

Another approach would be to add an AsyncPostBackTrigger to the outer UpdatePanel with ControlID=grvLenderDetails and EventName=SelectedIndexChanged.

If it's not in an UpdatePanel, then you need to change that. Here are examples:

<asp:UpdatePanel runat="server" ID="UpdInfo" UpdateMode="Conditional">
   <ContentTemplate>
       <asp:TextBox ID="TxtInfo" runat="server />
   </ContentTemplate>
</asp:UpdatePanel>

Here is the trigger approach that does not require you to call Update() from codebehind manually:

<asp:UpdatePanel runat="server" ID="UpdInfo" UpdateMode="Conditional">
   <ContentTemplate>
       <asp:TextBox ID="TxtInfo" runat="server />
   </ContentTemplate>
   <Triggers>
       <asp:AsyncPostBackTrigger ControlID="grvLenderDetails" EventName="SelectedIndexChanged" />
   </Triggers>
</asp:UpdatePanel>

Otherwise you'll need to use some sort of JS tricks, e.g.:

ScriptManager.RegisterStartupScript(this, GetType(), "ChangeTextBoxText", "<script type='text/javascript'>$('#"+txt.ClientId+"').val('Hello, i'm from the GridView');</script>", false);