簡體   English   中英

單擊按鈕即可在gridview內進行復選框驗證

[英]checkbox validation inside gridview on button click

我在網格視圖內有復選框列,在頁面上有一個按鈕和一個向導控件。.我需要針對按鈕單擊事件中的復選框進行驗證,如....如果未選中任何復選框,則需要防止加載向導步驟2...。但是我做不到

我將所有選中的行添加到在向導中設置的另一個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>

如果未選中此復選框,則需要停止以轉到下一頁..我該如何解決此問題....

任何人都可以對此提出建議嗎

添加一個計數並在內部遞增

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

and

if(count>0)
{
//load step 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;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM