日期:2014-05-18  浏览次数:20685 次

GridView获取控件。


希望有2种方法。

一种是用for循环找。

一种是用foreach循环找。。

C# code
<asp:GridView ID="gv_LoanList" runat="server" AllowPaging="True" PageSize="10" CssClass="desktable"
                    AllowSorting="True" EnableModelValidation="True" AutoGenerateColumns="False"
                    OnPageIndexChanging="gv_LoanList_PageIndexChanging" 
                    ondatabound="gv_LoanList_DataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="选择" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:CheckBox ID="cbo_checked" runat="server" ToolTip='<%#Eval("LoanHeadID") %>' />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                                            </Columns>
                    <PagerStyle CssClass="pager noborder" />
                    <HeaderStyle CssClass="header" />
                </asp:GridView>


例如上面的控件的Checkbox。

------解决方案--------------------
C# code
for(int i=0;i<gv_LoanList.Rows.Count();i++)
{
  CheckBox cb=gv_LoanList.Rows[i].FindControl("cbo_checked") as CheckBox ;
}

foreach(GridViewRow row in gv_LoanList.Rows)
{
  CheckBox cb=row .FindControl("cbo_checked") as CheckBox ;
}

------解决方案--------------------
C# code


foreach (GridViewRow gvrow in this.grid.Rows)
            {
                CheckBox chkId = (CheckBox)gvrow.FindControl("chkId");
                if (chkId.Checked == true)
                {
                    //复选框选中
                }

            }