【问题标题】:Best way to persist data bound GridView inside UpdatePanel在 UpdatePanel 中持久化数据绑定 GridView 的最佳方法
【发布时间】:2013-12-04 14:16:33
【问题描述】:

我在UpdatePanel 中有一个GridView 控件和一个简单的TextBox

TextBox 中输入文本(并单击按钮)后,从数据库中检索结果列表并绑定到GridView

        grd.DataSource = DataManager.GetFilteredList(txt.Text);
        grd.DataBind();

返回的每一行都有一个带有CommandArgumentCommandName 参数的LinkButton TemplateField 列。

我想在单击该按钮时运行一些代码,但是因为即使在部分回发时也会重新创建整个 Page,所以 GridView 会丢失其数据。通常我会在查询字符串中传递必要的参数,但因为这是在 UpdatePanel 内部,所以我不能这样做。

对此有什么好的和干净的方法?我唯一想到的就是,不用RowCommand,而是用一个简单的HTML <a> 元素创建GridView 按钮,在其HRef 参数中传递一个查询字符串参数,然后触发一个完整的回发最后在Page_Load 事件中进行操作,但这看起来很笨重。

这是请求的 ASP 标记:

    <asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <div class="input-group">
                <asp:TextBox ID="txt" runat="server" CssClass="form-control"></asp:TextBox>

                <div class="input-group-btn">
                    <asp:LinkButton ID="btn" CssClass="btn btn-default" runat="server" OnClick="btn_Click">Search</asp:LinkButton>
                </div>
            </div>

            <hr />
            <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"  ItemType="xxx.Policy" AllowPaging="True" PageSize="10" OnRowCommand="grd_RowCommand">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btn" CommandName="Connect" CommandArgument='<%#: Item.Id %>' runat="server">Connect</asp:LinkButton> 
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="grd" EventName="RowCommand" />
        </Triggers>
    </asp:UpdatePanel>

谢谢。

【问题讨论】:

  • 你能添加一些你的 ASP 标记
  • 我已经添加了标记。这没什么花哨的。

标签: c# asp.net ajax gridview updatepanel


【解决方案1】:

使用RowCommand 事件

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Connect")
    {
        //Add your code to connect using Item.Id
        //get Item.Id value using e.CommandArgument.ToString()
    }
}

【讨论】:

  • 我做了,它不起作用。调用 RowCommand 时,GridView 会丢失其数据。
  • 尝试替换为&lt;%# Bind("ItemId") %&gt;ItemId 是您数据库中的一个字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 2011-07-29
  • 2016-04-01
  • 1970-01-01
相关资源
最近更新 更多