C# 如何将数据库中插入的复选框列表值提取回复选框列表
我构建了一个演示应用程序,其中我使用了一个复选框列表来获取爱好,并在数据库中输入这些值,如下所示:C# 如何将数据库中插入的复选框列表值提取回复选框列表,c#,asp.net,gridview,ado.net,C#,Asp.net,Gridview,Ado.net,我构建了一个演示应用程序,其中我使用了一个复选框列表来获取爱好,并在数据库中输入这些值,如下所示: protected void Button1_Click(object sender, EventArgs e) { string str = string.Empty; foreach (ListItem item in chkboxlsthobbies.Items) { if (item.Selected)
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
foreach (ListItem item in chkboxlsthobbies.Items)
{
if (item.Selected)
{
str += string.Format("{0}, ", item.Text);
}
}
SqlCommand cmd = new SqlCommand("insert into [CheckboxTable] values('" + str + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindgrid();
}
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="gvtxtedit" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="gvtxtdelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hobbies">
<ItemTemplate>
<asp:Label ID="gvlblfirstname" runat="server" Text='<%#Eval("Hobbies") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void bindgrid()
{
SqlCommand cmd = new SqlCommand("select * from [CheckboxTable]", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
复选框列表的ASP代码为:
<asp:CheckBoxList ID="chkboxlsthobbies" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>cricket</asp:ListItem>
<asp:ListItem>football</asp:ListItem>
<asp:ListItem>pool</asp:ListItem>
<asp:ListItem>basketball</asp:ListItem>
<asp:ListItem>rugby</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" />
现在,我使用Gridview绑定并显示该表数据,其中有一个onRowEditing和onRowDelete事件
Gridview代码如下所示:
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
foreach (ListItem item in chkboxlsthobbies.Items)
{
if (item.Selected)
{
str += string.Format("{0}, ", item.Text);
}
}
SqlCommand cmd = new SqlCommand("insert into [CheckboxTable] values('" + str + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindgrid();
}
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="gvtxtedit" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="gvtxtdelete" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hobbies">
<ItemTemplate>
<asp:Label ID="gvlblfirstname" runat="server" Text='<%#Eval("Hobbies") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void bindgrid()
{
SqlCommand cmd = new SqlCommand("select * from [CheckboxTable]", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
现在我完全陷入了这个问题中,通常我能够将任何字段的值返回到TextBox或表单上的下拉列表
例如:
txtfirstname.Text = dt.Rows[0]["firstname"].ToString();
在onRowEditing事件中,将字段值取回表单上的控件,我可以对textbox和dropdown执行此操作
问题是我不知道如何获取由“,”分隔的多个值,返回到复选框列表,因为有多个值,我已经尝试了很长时间
最后一个问题是将数据库中的值提取回GridView的onRowEditing事件内的复选框列表。我编写此onRowEditing GridView函数是为了实现上述功能,即:将保存在数据库中的复选框列表值提取回GridView的onRowEditing事件内表单上的复选框列表
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
chkboxlsthobbies.ClearSelection();
//Get the ID
int id = Int32.Parse(GridView1.DataKeys[Int32.Parse(e.NewEditIndex.ToString())].Values["ID"].ToString());
SqlCommand cmd = new SqlCommand("select * from CheckboxTable where ID = '"+id+"'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
// Get the exact column and save it in a string.
string str = dt.Rows[0]["Hobbies"].ToString();
string[] strlist = str.Split(',').Select(t => t.Trim()).ToArray(); //Split into array and trim it.
//Finally the main functionality to check the values in checkbox list.
foreach (string s in strlist)
{
foreach (ListItem item in chkboxlsthobbies.Items)
{
if (item.Value == s)
{
item.Selected = true;
break;
}
}
}
谢谢。看一看。它涵盖了GridView编辑和更新的所有基础知识。感谢分享,但这些是GridView中的基本插入更新删除操作,非常简单,我正在寻找上面提到的具体问题。无论如何,谢谢你@VDWWDIn本教程编辑仅在Gridview中完成,希望将当前行的值提取回窗体上的控件,我可以对所有控件执行此操作,但复选框列表中有多个值除外@VDWWDI找到了解决方案,我已经添加了它。。无论如何,谢谢你。。