微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# GridViewRow FindControl 获取/传递值

如何解决C# GridViewRow FindControl 获取/传递值

我有一个 asp:linkbutton。当它被按下时,我想根据我的 GridView 中的哪些复选框被选中来获取 DetailID 的列表。我有一个 lblTesting 来查看我正在生成的 DetailID 列表。我的目标是获取我传递给编辑页面的 DetailID 列表。

我查看了 Stack Overflow 上的几个示例,但除了选中复选框之外,我无法获得任何值。

这是我页面顶部的链接

<span style="float:right;"><asp:Linkbutton ID="getURLLink" runat="server" Text="Edit Die Detail" OnClick="GetLink"></asp:Linkbutton></span>
<asp:Label runat="server" ID="lblTesting"></asp:Label>

这是我的 GridView:

 <asp:GridView ID="DieListDetailGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="DieListDetailGridView_RowDataBound" DataKeyNames="DieID" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" ForeColor="#333333" GridLines="Both">
        <RowStyle BackColor="#F5F5DC" />
        <AlternatingRowStyle BackColor = "White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="chkBox" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField datafield="DieNum" HeaderText="Die Number"  />
            <asp:BoundField datafield="DieID" HeaderText="Die ID" Visible="false" />
            <asp:BoundField datafield="DetailID" HeaderText="Detail ID" Visible="false" />
            <asp:HyperLinkField DataTextField="DetailNum" HeaderText="Detail Number" DatanavigateUrlFields="DetailID" DatanavigateUrlFormatString="~/Tooling/DieDetail.aspx?DetailID={0}" />
            <asp:BoundField datafield="Description" HeaderText="Description" />
            <asp:BoundField datafield="MaterialType" HeaderText="Material Type" />
            <asp:BoundField datafield="Hardness" HeaderText="Hardness" />
            <asp:BoundField datafield="MinSpares" HeaderText="Min Spares" />
            <asp:BoundField datafield="MaxSpares" HeaderText="Max Spares" />
            <asp:BoundField datafield="CurrentSpares" HeaderText="Current Spares" />
            <asp:BoundField datafield="DetailNotes" HeaderText="Detail Notes" />
        </Columns>
    </asp:GridView>

还有我的 GetLink 函数

 protected void GetLink(object sender,EventArgs e) 
        {

            // start off empty
            string DetailIDList = "";
            
            foreach (GridViewRow row in DieListDetailGridView.Rows) {

                if (row.RowType == DataControlRowType.DaTarow)
                {
                    CheckBox chkBox = (row.Cells[0].FindControl("chkBox") as CheckBox);

                    string DetailID = row.Cells[3].Text;

                    if (chkBox.Checked)
                    {
                       DetailIDList = DetailIDList + DetailID + ",";
                    }
                }
            }

            // Remove the last,from the list
            DetailIDList = DetailIDList.Remove(DetailIDList.Length - 1,1);
            // comma delimeted list of checked Detail ID's
            lblTesting.Text = DetailIDList;

            //Response.Redirect("~/Tooling/DieDetail.aspx?DetailIDList=");
        }

我不知道为什么我的 DetailID 是空的。

我尝试过 row.Cells[0].FindControl("DetailID") 但这也不起作用。

我期望从 DetailID 得到的是整数,例如“28925”、“16423”等。

我被要求不要在页面显示该列,这就是为什么visible = false。我仍然需要引用 ID 才能将其传递到编辑页面。我需要对多个详细信息 ID 进行相同的编辑。

我做错了什么?

解决方法

在另一个 StackOverflow 问题中找到了即使数据字段设置为不可见也有 ID 列表的答案:ASP.NET: GridView value set to null when column is not visible

答案是从 GridView 中删除“Visible=false”,并在创建的每一行上(在列添加了该行的数据之后)使其不可见。

此代码解决了我的问题:

 protected void DieListDetailGridView_RowCreated(object sender,GridViewRowEventArgs e)
        {
            e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。