【问题标题】:GridView within an UpdatePanel - Get Updated Textbox values on button clickUpdatePanel 中的 GridView - 单击按钮时获取更新的文本框值
【发布时间】:2011-10-06 19:51:26
【问题描述】:

忽略这个:

我在 Onload 方法中重新绑定网格。

我有一个带有一些文本框的网格视图,我必须将它们包装在 UpdatePanel 控件中。所以它看起来像这样:

<asp:UpdatePanel ID="upDistribution" runat="server" OnLoad="upDistribution_OnLoad">
    <ContentTemplate>
        <asp:GridView ID="gvDistributions" runat="server" AutoGenerateColumns="false"
            OnRowDataBound="gvDistributions_RowDataBound"
            CssClass="TallCells ContrastTable MaxWidth LeftHeaders"
            GridLines="Both" ShowFooter="True" style="">
            <RowStyle HorizontalAlign="Left"  />

            <EmptyDataTemplate>
                No pricing history data found.
            </EmptyDataTemplate>

            <Columns>
                <asp:TemplateField HeaderText="PriceA">
                    <ItemTemplate>

                        <asp:TextBox ID="txtPriceA" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DistPrice">
                    <ItemTemplate>

                        <asp:TextBox ID="txtDistPrice" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
              </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

当我想获取为 txtboxes 输入的值时,有一个“保存”按钮。如果 GV 周围没有 UpdatePanel,我可以轻松获取 txtbox 值,例如:

((TextBox)gvDistributions.Rows[0].Cells[0].FindControl("txtPriceA")).Text

但是,当 GridView 被 UpdatePanel 包裹时,上述语句返回页面加载时设置的值。

如何在不去掉 updatePanel 的情况下获取文本框的值。我需要更新面板,因为行数和日期取决于另一个用户控件中的另一个变量。

【问题讨论】:

  • 能否把hte按钮放到更新面板中,并使用脚本管理器将按钮注册为回发控件(假设您需要按钮进行回发)?
  • @JamesJohnson 不,我不能。
  • 嗯...我的想法可能在这里倒退,但尝试在代码隐藏中添加ScriptManager.GetCurrent().RegisterPostBackControl(txtDistPrice);
  • 我们应该在这里忽略什么?您找到问题的答案了吗?如果是这样,请将其作为答案发布并标记为已接受。

标签: c# asp.net gridview updatepanel


【解决方案1】:

根据我的理解,您应该为更新面板使用触发器,如下所示。

    <asp:UpdatePanel ...>
       <ContentTemplate>
       ...
       </ContentTemplate>
       <Triggers>
           <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
       </Triggers>
    </asp:UpdatePanel>

希望这会有所帮助!

【讨论】:

  • 如何从 UserControl 的文本框条目中设置触发器。 Usercontrol A 与包含 UpdatePanel 的 UserControl B 在同一页面上如果我感到困惑 - UserControl B = 包含更新面板的用户控件 UserContorl A = 我希望更新面板收听的用户控件
  • 触发器是否进入 GridView 的 UpdatePanel?我的问题在这里说明:stackoverflow.com/questions/25530822/…
【解决方案2】:

使用您的TextBoxes 和他们的AutoPostBack 属性集True

<asp:TextBox ID="txtPriceA" runat="server" AutoPostBack="True"/>

【讨论】:

    【解决方案3】:

    我在 onLoadMethod 中重新绑定 gridview

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多