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

时间:2013-09-25 09:03:59

标签: asp.net gridview checkbox

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

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

enter image description here

例如:如果我取消选中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>

这是更新按钮(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());

            }
        }

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

这是我第一次检查复选框的方式。此代码也在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;
                }
            }
        }

如何解决这个问题?

由于

2 个答案:

答案 0 :(得分:5)

如果您选择了checkboxfield类型列,那么我建议您在网格中选择itemtemplate列

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>

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

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
}

答案 1 :(得分:0)

在html aspx中

Protected Sub chk_Todos_CheckedChanged(sender As Object, e As EventArgs)
    Dim Check_All As CheckBox = grid_view.HeaderRow.FindControl("chk_Todos")
    For Each row As GridViewRow In Me.grid_view.Rows
        Dim Check As CheckBox = row.FindControl("chk_Seleccionar")
        If Check_All.Checked = True And Check.Enabled = True Then
            Check.Checked = True
        Else
            Check.Checked = False
        End If
    Next
End Sub

对于aspx vb.net

{{1}}