简体   繁体   中英

C# GridViewRow FindControl get/pass values

I have an asp:linkbutton. When it's pressed I want to get a list of DetailID's based on which of the checkboxes in my GridView are checked. I have a lblTesting to see the list of DetailID's that I'm generating. My goal is to get a list of DetailID's that I pass to an edit page.

I have looked at several examples on Stack Overflow but I cannot get any value other than the checkbox is checked.

Here is the link I have at the top of my page:

<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>

Here is my 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>

And my GetLink function:

 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=");
        }

I can't figure out why my DetailID is empty.

I've tried doing row.Cells[0].FindControl("DetailID") but that also didn't work.

What I am expecting to get from the DetailID are integers like "28925", "16423" etc.

I was asked to not show the column on the page, which is why visible = false. I still need to reference the ID to pass it to the edit page. I need to do the same edit to multiple Detail ID's.

What am I doing wrong?

The answer to having a list of ID's even though the datafield is set to not be visible was found on another StackOverflow question: ASP.NET: GridView value set to null when column is not visible

The answer was to remove the "Visible=false" from the GridView and on each row created (after the column has the data added for the row) make it not visible.

This code fixed my issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM