不存在映射错误
当您正在更新
List
(
studentsubjects
sublist
在以下代码中已经是字符串:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int studentid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
CheckBoxList subjects = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList1");
List<string> studentsubjects = new List<string>();
string sublist = "";
foreach (ListItem item in subjects.Items)
{
if (item.Selected)
{
studentsubjects.Add(item.Text);
}
}
sublist = string.Join(",", studentsubjects); // add , inside subjects names
SqlConnection conn = new SqlConnection("Data Source=WINCTRL-0938L38; Database=dbUni; Integrated Security=true");
conn.Open();
SqlCommand cmd = new SqlCommand("StudentUpdate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@student_id ", studentid);
cmd.Parameters.AddWithValue("@subjects ", sublist);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
FillGrid();
conn.Close();
}
编辑2:
删除此属性
SelectedValue='<%# Eval("SUbjects") %>'
从…起
checkboxlist
Label
<EditItemTemplate>
<asp:Label ID="lblSubjects" Visible="false" runat="server" Text='<%# Eval("Subjects") %>'></asp:Label>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
<%--OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged--%>
<asp:ListItem Value="Physics">Physics</asp:ListItem>
<asp:ListItem Value="Chemistry">Chemistry</asp:ListItem>
<asp:ListItem Value="Biology">Biology</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
RowDataBound事件:
这约束了你的
checkboslist
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
CheckBoxList chklist = ((CheckBoxList)e.Row.FindControl("CheckBoxList1"));
string subjects = ((Label)e.Row.FindControl("lblSubjects")).Text;
List<string> studentsubjects = subjects.Split(',').ToList();
foreach (string item in studentsubjects)
{
if (item == "Physics")
chklist.Items.FindByText("Physics").Selected = true;
else if (item == "Chemistry")
chklist.Items.FindByText("Chemistry").Selected = true;
else
chklist.Items.FindByText("Biology").Selected = true;
}
}
}
}
注:
OnRowDataBound
GrindView中的事件
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >