gridview链接按钮CommandArgument值未更改 [英] gridview linkbutton CommandArgument value not changing

查看:58
本文介绍了gridview链接按钮CommandArgument值未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据列表的网格视图和一个用于删除该行的LinkBut​​ton.

I have a grid view with a list of data and a LinkButton to delete the row.

<asp:GridView ID="gridApartment" EmptyDataText="No Records Found" runat="server" AutoGenerateColumns="False" BorderWidth="0"
                            AllowPaging="true" ShowFooter="false" PageSize="15" Width="100%" OnPageIndexChanging="gridApartment_PageIndexChanging"
                            CssClass="mGrid"
                            OnRowDeleting="gridApartment_RowDeleting" OnRowCommand="gridApartment_RowCommand">
                            <AlternatingRowStyle CssClass="alt" />
                            <PagerStyle CssClass="pgr" />
                            <Columns>
                                <asp:TemplateField HeaderText="Building">
                                    <ItemTemplate>
                                        <asp:Label ID="BuildingName" runat="server" Text='<%#Eval("BuildingName") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle Width="25%"></HeaderStyle>
                                </asp:TemplateField>


                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkDelete" CssClass="aDelete" ToolTip="Delete" runat="server" CommandName="delete" OnClientClick=' javascript:return confirm("Are you sure you want to delete?"); '
                                            CommandArgument='<%# Eval("RoomDetailsId") %>'>Delete</asp:LinkButton>
                                    </ItemTemplate>
                                    <HeaderStyle Width="8%"></HeaderStyle>
                                </asp:TemplateField>

                            </Columns>

                        </asp:GridView>

加载此代码填充的数据

gridApartment.DataSource = masterManager.GetAllRooms();
gridApartment.DataBind();

现在我有一个搜索框,当我搜索时,过滤器结果将被此代码绑定.

now i have a search box, and when i search, the filters result will get bind by this code.

gridApartment.DataSource = conobj.GetSearchDetails("usp_RoomDetailsSearch", "@SearchName", txtSearchterm.Text.Trim());
gridApartment.DataBind();

用于删除的代码是

protected void gridApartment_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            var masterManager = new MasterEntryManager();

            int res = masterManager.DeletRoom(e.CommandArgument.ToString());

            Search();
        }
    }

第一次加载网格时,e.CommandArgument具有正确的ID,我可以删除正确的行.但是,当我搜索并重新绑定gridview时,不会使用新ID更新e.CommandArgument值.它仍然返回在页面加载时首先加载的ID.

when the first time the grid get loaded e.CommandArgument has the right ID and i can delete the right row. But when i search and re bind the gridview, the e.CommandArgument value is not updated with new ID. It still return the same ID which is loaded first on page load.

例如:

  1. 第一次加载网格时,当我尝试删除第一行时,e.CommandArgument的ID为1001,我删除了ID为1001的记录.
  2. 现在我第二次加载gridview了.现在,第一行链接按钮e.CommandArgument的ID为1500.
  3. 我执行搜索,结果网格只有5行
  4. 现在我尝试删除第一行,预期的链接按钮e.CommandArgument ID为2001,但我将链接按钮e.CommandArgument ID设置为1500,即第一次加载gridview时第一行ID的值

Id未更新.如何获取更新的e.CommandArgument值.

Id is not get updated. How can I get the updated e.CommandArgument value.?

推荐答案

与预期相反,CommandArgument与执行回发的控件没有关联.而是,DataSource作为ViewState的一部分返回,并且CommandArgument由控件的索引引用.如果GridView或DataSource发生更改,则索引将不再正确.

Counter to expectations, the CommandArgument is not associated with the control that does the post back. Rather, the DataSource is returned as part of the ViewState, and the CommandArgument is referenced by the index of the control. If either the GridView or DataSource change, the index will no longer be correct.

您需要确保在引用CommandArgument之前未更新GridView和DataSource,但在删除后再进行更新(看起来像这部分一样).请记住,回发事件是在页面加载之后发生的.

You need to make sure that the GridView and DataSource are not updated prior referencing the CommandArgument, but then updated after the delete (it looks like you have this part). Remember that the post back event occurs after the page load.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridApartment.DataSource = masterManager.GetAllRooms();
        gridApartment.DataBind();
    }
}

这篇关于gridview链接按钮CommandArgument值未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆