按钮单击时gridview内的复选框验证

时间:2014-10-31 12:32:57

标签: c# asp.net .net checkbox

我在网格视图中有一个复选框列,页面上有一个按钮和一个向导控件..我需要对按钮点击事件中的复选框进行验证,如下所示....如果未选中任何复选框,我需要阻止加载向导步骤2 ....但我没有这样做

我将所有选中的行添加到我在wizardStep中设置的另一个gridview:1

这是我的代码......

protected void Update_Onclick(object sender, EventArgs args)
{
    DataTable dt = null;
    CheckBox chk;
    foreach(GridViewRow gvrow in gvPR.Rows)
    {
        //chk = (CheckBox)(gvPR.Rows[i].Cells[0].FindControl("chkFru"));
        chk = (CheckBox)gvrow.FindControl("chkFru");
        if (chk.Checked == true)
        {
            dt = objCert.BuildCertInfo();
            DataRow dr = dt.NewRow();
            // HtmlInputHidden hdn = (HtmlInputHidden)(gvPR.Rows[i].Cells[0].FindControl("hdnFruId"));
            HtmlInputHidden hdn = (HtmlInputHidden)gvrow.FindControl("hdnFruId");
            string strFru = hdn.Value;
            dr[Certificate.SYS_SERIAL_NUMBER] = strFru;

            //get Fru info

            dsInfo = objCert.GetFruInfo(strFru);
            if (dsInfo == null)
            {
                setError(lblCertErr, Certificate.NO_SYS_INFO);
                return;
            }
            dr[Certificate.SYS_PART_ID] = dsInfo.Tables[0].Rows[0]["part_id"].ToString();
            dr[Certificate.SYS_PART_DESC] = dsInfo.Tables[0].Rows[0]["Part_desc"].ToString();
            dr[Certificate.SYS_SERIAL_NUMBER] = dsInfo.Tables[0].Rows[0]["Serial_Number"].ToString();
            dr[Certificate.LOC] = dsInfo.Tables[0].Rows[0]["Location"].ToString();
            dt.Rows.Add(dr);

        }

    }
   LoadWizStep2(dt); 
}

和LoadWizStep2(dt)的方法

private void LoadWizStep2(DataTable dt)
{
    try
    {
        wizController.ActiveStepIndex = 1;
        GridView2.DataSource = dt;
        GridView2.DataBind();
        Session["FRU_INFO"] = dt;
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

这是aspx页面的代码

 <table width="100%" cellpadding="0" cellspacing="0" style="background-color: White">
                <tr style="width: 100%">
                    <td colspan="2" align="left">
                        <asp:GridView ID="gvPR" runat="server" AutoGenerateColumns="False" GridLines="None"
                            CellSpacing="1" CellPadding="1"
                            Width="100%" BorderWidth="0px"
                            AllowSorting="True"
                            PageSize="30"
                            CssClass="data responsive"
                            OnSorting="gvPR_Sort" OnRowDataBound="ItemCellsUpdate"
                            EmptyDataText="No Certificates found" SortedAscendingHeaderStyle-CssClass="tableHeaderLink">
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                         <asp:CheckBox ID="chkCerts" OnCheckedChanged="chkCerts_CheckedChanged"
                                                   AutoPostBack="true" runat="server" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkFru" OnCheckedChanged="chkFru_CheckedChanged" AutoPostBack="true" runat="server" /><input type="hidden" id="hdnFruId" runat="server"
                                            value='<%# DataBinder.Eval(Container.DataItem, "Fru") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="Fru" HeaderText="System Serial Number" SortExpression="Fru" />
                                <asp:BoundField DataField="SystemPartId" HeaderText="System Part Number" SortExpression="SystemPartId" />
                                <asp:BoundField DataField="SystemDesc" HeaderText="System Description" SortExpression="SystemDesc" />
                                <asp:BoundField DataField="Location" HeaderText="System Location" SortExpression="Location" />
                            </Columns>
                            <HeaderStyle Height="30px" HorizontalAlign="Center" />
                            <PagerSettings Visible="False" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>

如果未选中复选框,我需要停下来转到下一页..我怎么能解决这个问题....

任何人都可以就此提出建议

2 个答案:

答案 0 :(得分:0)

添加一个计数并在

中增加它
int count=0
If(chk.checked)
{
count++
}

and

if(count>0)
{
//load step 2
}

答案 1 :(得分:0)

你可以在这里使用更简单的代码

int count=0;
            //ImageButton lbtn = (ImageButton)sender;
            DoctorDAO objDoctorDAO = new DoctorDAO();
            foreach (GridViewRow  row in grd_Data.Rows)
            {
                CheckBox chk = (CheckBox)grd_Data.Rows[row.RowIndex].FindControl("Chklist");
                if (chk.Checked == true)
                {

                    count++;

                }
            }
            if (count > 0)
            {
                foreach (GridViewRow row in grd_Data.Rows)
                {
                    CheckBox chk = (CheckBox)grd_Data.Rows[row.RowIndex].FindControl("Chklist");
                    if (chk.Checked == true)
                    {
                        ImageButton lbtn = (ImageButton)grd_Data.Rows[row.RowIndex].FindControl("btn_Delete");

                         objDoctorDAO.deleteDoctor(Convert.ToInt32(lbtn.CommandArgument));
                        lbl_Msg.Text = " Doctor Deleted Successfully!!";
                    }
                }
            }
            else
            {
                lbl_msg3.Text = "Please Check at least one record to Delete";
                return;
            }