如何在asp.net中检查gridview中复选框列的状态

cha*_*ura 1 asp.net checkbox gridview

我正在尝试实现更新主题功能.当我想添加一个新主题时,它正在工作.但是当我取消选中一个复选框(如果我想从程序中删除现有主题)时,它就不起作用了.

我调试程序,它显示未选中的复选框也被选中.

在此输入图像描述

例如:如果我取消选中IT102并单击更新按钮,则所有3个主题都将保存在数据库中.

这是aspx代码

<asp:GridView ID="gridview_modules" runat="server" AutoGenerateColumns="False"          
    GridLines="None">
                        <HeaderStyle Width="30%" />
                        <RowStyle Width="30%" />
                        <FooterStyle Width="30%" />

                        <Columns>
                            <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="checkbox_select" />
                            </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="courseNo"  HeaderStyle-Width="20%" 
                                ItemStyle-Width="10%" FooterStyle-Width="10%" >
                            <FooterStyle Width="10%" />
                            <HeaderStyle Width="20%" />
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                            <asp:BoundField DataField="title"/>
                        </Columns>
                    </asp:GridView>
Run Code Online (Sandbox Code Playgroud)

这是更新按钮中的代码(Inside foreach循环)

  System.Web.UI.WebControls.CheckBox chk =        (System.Web.UI.WebControls.CheckBox)rowItem.Cells[0].FindControl("checkbox_select");
        if (chk.Checked)
        {
            all++; //no of checked subjects when the button is clicked
            if (con.saveCourseForProgram(SiteVariables.ProgramName, rowItem.Cells[1].Text.ToString(), year, sem, SiteVariables.Specialization))
            {
                success++;//try to insert in the db
            }
            else
            {
                //subject that didn't save in the db goes to courseList
                courseList.Add(rowItem.Cells[1].Text.ToString());

            }
        }
Run Code Online (Sandbox Code Playgroud)

page_load中的代码段

if (!Page.IsPostBack)
    {


        SiteVariables.ProgramName = null;
        SiteVariables.Year = null;
        SiteVariables.Semester = null;
        SiteVariables.Specialization = null;


        if (radioAll.Checked)
        {
            SqlDataSource DataSource2 = new SqlDataSource();
            DataSource2.ID = "SqlDataSource2";
            this.Page.Controls.Add(DataSource2);
            DataSource2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SEP_Project_NewConnectionString2"].ConnectionString;
            DataSource2.SelectCommand = "SELECT courseNo,title from Course";
            gridview_modules.DataSource = DataSource2;
            gridview_modules.DataBind();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我第一次检查复选框的方式.此代码也在page_load.course中,是一个包含特定程序主题的列表.

 for (int i = 0; i < courses.Count; i++)
        {

            String courseNo = courses[i].Trim();
            //System.Diagnostics.Debug.Print("Course No :"+courseNo+"\n");

            for (int j = 0; j < gridview_modules.Rows.Count; j++)
            {
                //System.Diagnostics.Debug.Print("Row Value = " + gridview_modules.Rows[j].Cells[1].ToString() + "List value = " + courseNo + "\n");
                if (gridview_modules.Rows[j].Cells[1].Text == courseNo)
                {
                    var chk = (System.Web.UI.WebControls.CheckBox)(gridview_modules.Rows[j].Cells[0].FindControl("checkbox_select"));
                    chk.Checked = true;
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

谢谢

Sid*_*d M 5

如果您已经使用了checkboxfield类型列,那么我建议您在网格中使用itemtemplate列

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

并从后面的代码中获取checkbox的值,如下所示

foreach(GridViewRow  gvrow in myGrid.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk.Checked)
{
//your code here when checkbox is checked
}
else
{
//else part
}
Run Code Online (Sandbox Code Playgroud)