我正在尝试实现更新主题功能。当我想添加一个新主题时,它就可以工作了。但是当我取消选中一个复选框(如果我想从程序中删除现有主题)时,它就不起作用了。
我调试了程序,它显示未选中的复选框也被选中。
例如:如果我取消选中 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;
}
}
}
如何解决这个问题?
谢谢