Joe.wang
Joe.wang

Reputation: 11793

Get the checkbox or radio in the GridView

All, I added a CheckBox for every row of the GridView. But I am in trouble to find the CheckBox from the GridView when postback.

Here is the code what I have done. please review it. thanks.

protected void GridViewThirAct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SomeObject mapItem = (SomeObject)e.Row.DataItem;
                string itemId = mapItem.ItemId;

                Literal lit = e.Row.FindControl("SelectButtonMarkup") as Literal;

                if (IsThirdSeriesMultipleSelect)
                {
                    lit.Text = String.Format("<input type='checkbox' name='FinalActivity' id='RowSelector{0}' value='{1}'", e.Row.RowIndex, itemId);
                }
                else
                {
                    lit.Text = String.Format("<input type='radio' name='FinalActivity' id='RowSelector{0}' value='{1}'", e.Row.RowIndex, itemId);
                }
                lit.Text += " />";


            }
        }

The code in Aspx is below.

    <asp:GridView ID="GridViewThirAct" runat="server" AutoGenerateColumns="false" Width="100%"
        OnRowCreated="GridViewThirAct_RowCreated" OnRowDataBound="GridViewThirAct_RowDataBound">
        <Columns>
            <asp:TemplateField ItemStyle-Width="5%">
                <ItemTemplate>
                    <asp:Literal ID="SelectButtonMarkup" runat="server"></asp:Literal>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ItemValue" HeaderText="Activity Name" ItemStyle-Width="80%" />
            <asp:BoundField DataField="ItemId" HeaderText="ID" ItemStyle-Width="15%" />
        </Columns>
    </asp:GridView>

<asp:LinkButton ID="btnOk" runat="server" onclick="btnOk_Click"></asp:LinkButton>

I want to get the all the select values in the button click event. Thanks.

I have already used Request.Form["FinalActivity"] to make it .But I was thinking is there any other ways to retrieve the CheckBox control by iterate the Rows of GridView? I have tried the code this.GridViewThirAct.Rows[i].Cells[0].FindControl(controlId), But it doesn't work. thanks.

Upvotes: 0

Views: 515

Answers (1)

Ramin Asadi
Ramin Asadi

Reputation: 281

try it:
.aspx

 <asp:GridView ID="grdFoodList" AutoGenerateColumns="False" runat="server">
        <Columns>
         <asp:TemplateField>
             <HeaderTemplate>
                 Select
             </HeaderTemplate>
             <ItemTemplate>
                <asp:CheckBox runat="server" ID="selectFood" />
             </ItemTemplate>
         </asp:TemplateField>

code behind:

for (int rows = 0; rows < grdFoodList.Rows.Count; rows++)
        {
            if (((CheckBox) grdFoodList.Rows[rows].Cells[0].FindControl("selectFood")).Checked)
            {
               //your code
            }

        }

Upvotes: 1

Related Questions