0

所有,我CheckBoxGridView. 但是我很难CheckBoxGridView回发时找到。

这是我所做的代码。请审查它。谢谢。

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 += " />";


            }
        }

中的代码Aspx如下。

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

我想在按钮单击事件中获取所有选择值。谢谢。

我已经习惯Request.Form["FinalActivity"]了。但我在想有没有其他方法可以CheckBox通过迭代Rows来检索控件GridView?我已经尝试了代码this.GridViewThirAct.Rows[i].Cells[0].FindControl(controlId),但它不起作用。谢谢。

4

1 回答 1

1

试试看:
.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>

后面的代码:

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

        }
于 2013-07-18T06:28:15.597 回答