更改UpdatePanel外部的TextBox中的文本

时间:2013-09-20 07:25:32

标签: c# asp.net asp.net-ajax ajaxcontroltoolkit

我在UpdatePanel中有一个gridview(gridview在弹出窗口中显示)。单击该网格中的选择按钮,我试图在页面中设置文本框文本。但它不起作用,如果我删除更新面板,那么它将工作。 请帮忙。

这是我在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>

1 个答案:

答案 0 :(得分:7)

我认为TextBox位于另一个UpdatePanel。使其UpdateMode Conditional允许以编程方式调用其Update方法。

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

另一个方法是使用AsyncPostBackTriggerUpdatePanel向外ControlID=grvLenderDetails添加EventName=SelectedIndexChanged

修改:如果它不在UpdatePanel内进行更改。以下是示例:

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

这里的触发方法不需要手动从代码隐藏中调用Update()

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