【问题标题】:checkbox validation inside gridview on button click按钮单击时gridview内的复选框验证
【发布时间】:2017-09-21 17:50:03
【问题描述】:

我在网格视图中有复选框列,页面上有一个按钮和一个向导控件。我需要像这样在按钮单击事件中针对复选框进行验证...。如果未选中任何复选框,我需要防止加载向导第 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>

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

有没有人可以提出建议

【问题讨论】:

  • 你为什么总是调用 LoadWizStep2(dt);为什么不能通过标志添加条件来检查是否选中了这些复选框?

标签: c# asp.net .net checkbox


【解决方案1】:

添加一个计数并在内部增加它

int count=0
If(chk.checked)
{
count++
}

and

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

【讨论】:

    【解决方案2】:

    你会在这里罚款更简单的代码

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

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 2011-11-04
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2012-05-13
      • 2018-02-19
      相关资源
      最近更新 更多