1

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

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

在此处输入图像描述

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

这是更新按钮中的代码(在 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;
                }
            }
        }

如何解决这个问题?

谢谢

4

2 回答 2

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
}
于 2013-09-25T09:31:21.107 回答
0

在 html 中

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <HeaderTemplate><center>TODOS<br /> <asp:CheckBox ID="chk_Todos" runat="server" AutoPostBack="True" OnCheckedChanged="chk_Todos_CheckedChanged"  /></center></HeaderTemplate>
    <ItemTemplate><asp:CheckBox ID="chk_Seleccionar" runat="server"/></ItemTemplate>
</asp:TemplateField>

对于 aspx vb.net

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
于 2018-03-26T20:34:53.260 回答