【问题标题】:UpdatePanel doesn't allow to fire button click inside gridviewUpdatePanel 不允许在 gridview 内触发按钮单击
【发布时间】:2014-05-06 13:42:45
【问题描述】:

我有一个gridview,里面有下面的代码。

<asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <!-- only your content that needs refreshing goes here -->

                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                        onclick="ButtonAdd_Click" />
                        </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
                </FooterTemplate>
            </asp:TemplateField>

我添加了更新面板,因为当我单击 ButtonAdd 时,gridview 中的按钮数据已被删除。但是在我添加下面的代码按钮后,单击事件不会触发。请提出解决方案。

【问题讨论】:

  • 如果我是你,我会重新考虑整个解决方案。 GridViews 不能很好地与 UpdatePanels 配合使用。 UpdatePanels 不能很好地处理任何东西,而且 GridViews 在客户端比普通的 HTML 表格更难处理。您是否考虑过弹出一个对话框来获取新行的值,然后用新数据更新数据源,然后重新绑定 GridView,而不是向 GridView 添加一行?
  • 如果您删除整个 &lt;Triggers&gt; 部分会发生什么。按钮已经在 UpdatePanel 内,所以不需要
  • 你为什么用UpdatePanel 来代替Button?
  • 如果我删除 ,则完全没有变化。我需要更新面板以防止单击按钮时数据丢失,它在 gridview 内。

标签: asp.net gridview


【解决方案1】:

请将您的整个gridview 放在更新面板中,然后删除更新面板并尝试..

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
     <asp:GridView ID="gridview1" runat="server"  >
     </asp:GridView>
    </ContentTemplate>
    <Triggers>

    </Triggers>
</asp:UpdatePanel>

【讨论】:

  • 获取运行时错误“在 UpdatePanel 'UpdatePanel1' 中找不到触发器的 ID 为 'ButtonAdd' 的控件”。因为按钮在 gridview 作为页脚模板。
  • 尝试不设置异步触发器并检查?
  • 尝试不设置异步触发器不起作用。单击按钮时丢失数据。
  • 我输入到gridview的所有数据都丢失了。
  • 在哪种情况下您要绑定 Gridview?在页面加载中?
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多