我有一个学生表格,存储学生的信息。 Form Looks Like This
以下是代码。请告诉我如何通过Checkboxlist更新多个复选框
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
button.setImage(UIImage(named: "ur_image.png"), for: .normal)
self.navigationItem.titleView = button
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" SelectedValue='<%# Eval("SUbjects") %>'>
<%--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>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" onCheckedChanged="onChackedChange" Text='<%# Eval("SUbjects") %>'></asp:Label>
</ItemTemplate>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int studentid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());//Get Each Row unique value from DataKeyNames
string studentname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;//get TextBox Value in EditItemTemplet that row is clicked
string studentage = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string studentdepartment = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).Text;
string studentgender = ((RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1")).Text;
CheckBoxList studentsubjects = ((CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList1"));
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("@name ", studentname);
cmd.Parameters.AddWithValue("@age ", int.Parse(studentage));
cmd.Parameters.AddWithValue("@department ", studentdepartment);
cmd.Parameters.AddWithValue("@gender ", studentgender);
cmd.Parameters.AddWithValue("@subjects ", studentsubjects);
}
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;// no row in edit mode
FillGrid();
conn.Close();
}
CREATE PROCEDURE [dbo].[StudentUpdate]
(
@student_id int,
@name varchar(50),
@age int,
@gender nvarchar(50),
@department nvarchar(50),
@subjects nvarchar(50)
)
AS UPDATE tblStudents set
NAME=@name,
AGE=@age,
DEPARTMENT=@department,
GENDER=@gender,
SUbjects=@subjects
where Student_ID=@student_id
In This Image I have applied changes but I am having this warning.
答案 0 :(得分:0)
您必须在CheckBoxList中循环所有ListItem。根据您现在所做的事情,您只获得列表中的第一个已检查项目,而不是所有项目。
//create a list to store all the subjects
List<string> studentsubjects = new List<string>();
//find the control in the GridView and cast it back to a CheckBoxList
CheckBoxList subjects = GridView1.Rows[e.RowIndex].FindControl("CheckBoxList1") as CheckBoxList;
//loop all the items and see if they are checked
foreach (ListItem item in subjects.Items)
{
if (item.Selected)
{
//add the checked value to the list
studentsubjects.Add(item.Text);
}
}
//display results
Label1.Text = string.Join(",", studentsubjects);
答案 1 :(得分:0)
试试这个:
.Aspx代码:
<EditItemTemplate>
<asp:Label ID="Label1" Visible="false" runat="server" Text='<%# Eval("SUbjects") %>'></asp:Label>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" SelectMethod="Multi" RepeatDirection="Horizontal" SelectedValue='<%# Eval("SUbjects") %>'>
<%--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>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" onCheckedChanged="onChackedChange" Text='<%# Eval("SUbjects") %>'></asp:Label>
</ItemTemplate>
.CS代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || (e.Row.RowState & DataControlRowState.Edit) > 0)
{
CheckBoxList chklist = ((CheckBoxList)e.Row.FindControl("CheckBoxList1"));
string subjects = ((Label)e.Row.FindControl("Label1")).Text;
string[] subjectslist = subjects.Split(',');
foreach (string item in subjectslist)
{
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;
}
}
}
注意:不要忘记在GrindView OnRowDataBound
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
事件