C# 如何使用数据库中的数据将值绑定到复选框?
我有一个带有几个复选框的界面我希望检查数据库中是否有可用的相关值C# 如何使用数据库中的数据将值绑定到复选框?,c#,asp.net,checkbox,C#,Asp.net,Checkbox,我有一个带有几个复选框的界面我希望检查数据库中是否有可用的相关值 这是我拥有的界面 首先从不同的表中加载for复选框,然后使用另一个表加载框中的其他复选框。 如果你能帮我做这件事 List<int> list = new List<int>(); foreach (DataRow dr in ds.Tables[0].Rows) { list.Add(Convert.
这是我拥有的界面
首先从不同的表中加载for复选框,然后使用另一个表加载框中的其他复选框。
如果你能帮我做这件事
List<int> list = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(Convert.ToInt32(dr["Qulif_code"]));
}
int[] arr = list.ToArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
ck_ol.Checked = true;
}
else if (arr[i] == 2)
{
ck_al.Checked = true;
}
else if (arr[i] == 3)
{
ck_dip.Checked = true;
}
else if (arr[i] == 4)
{
ck_deg.Checked = true;
}
}
这是前4个复选框的表格这是下复选框的表格
我已经创建了查询,并将数据传输到数据集。
这就是我到目前为止所做的代码
List<int> list = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(Convert.ToInt32(dr["Qulif_code"]));
}
int[] arr = list.ToArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
ck_ol.Checked==true ;
}
}
List List=新列表();
foreach(ds.Tables[0].行中的数据行dr)
{
列表添加(转换为32(dr[“Qulif_代码”);
}
int[]arr=list.ToArray();
对于(int i=0;i
我已经解决了这个问题,下面是代码
List<int> list = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(Convert.ToInt32(dr["Qulif_code"]));
}
int[] arr = list.ToArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
ck_ol.Checked = true;
}
else if (arr[i] == 2)
{
ck_al.Checked = true;
}
else if (arr[i] == 3)
{
ck_dip.Checked = true;
}
else if (arr[i] == 4)
{
ck_deg.Checked = true;
}
}
List List=新列表();
foreach(ds.Tables[0].行中的数据行dr)
{
列表添加(转换为32(dr[“Qulif_代码”);
}
int[]arr=list.ToArray();
对于(int i=0;i
我希望这可能对将来的人有所帮助。我已经解决了这个问题,下面是代码
List<int> list = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(Convert.ToInt32(dr["Qulif_code"]));
}
int[] arr = list.ToArray();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 1)
{
ck_ol.Checked = true;
}
else if (arr[i] == 2)
{
ck_al.Checked = true;
}
else if (arr[i] == 3)
{
ck_dip.Checked = true;
}
else if (arr[i] == 4)
{
ck_deg.Checked = true;
}
}
List List=新列表();
foreach(ds.Tables[0].行中的数据行dr)
{
列表添加(转换为32(dr[“Qulif_代码”);
}
int[]arr=list.ToArray();
对于(int i=0;i
我希望这可能对将来的人有所帮助。数据库
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
- 我使用了下表,模式如下所示 跟随。在ASP.Net的GridView中启用禁用的CheckBoxField列
- 我已经在表中插入了几条记录 ASP.Net中GridView中的CheckBoxField列
- HTML标记
下面是ASP.net网页的HTML标记。 如您所见,我添加了一个复选框列表和一个按钮,允许用户更新数据库中的选择
<asp:CheckBoxList ID="chkHobbies" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick = "UpdateHobbies" />
上述方法在页面加载事件中以以下方式调用:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PopulateHobbies();
}
}
将选择保存到数据库中
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
以下方法在提交按钮单击事件中调用,用于将用户选择保存到数据库中
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
当程序第一次运行时
更新复选框中的值后,
数据库
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
- 我使用了下表,模式如下所示 跟随。在ASP.Net的GridView中启用禁用的CheckBoxField列
- 我已经在表中插入了几条记录 ASP.Net中GridView中的CheckBoxField列
- HTML标记
下面是ASP.net网页的HTML标记。 如您所见,我添加了一个复选框列表和一个按钮,允许用户更新数据库中的选择
<asp:CheckBoxList ID="chkHobbies" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick = "UpdateHobbies" />
上述方法在页面加载事件中以以下方式调用:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PopulateHobbies();
}
}
将选择保存到数据库中
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
以下方法在提交按钮单击事件中调用,用于将用户选择保存到数据库中
protected void UpdateHobbies(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "update hobbies set IsSelected = @IsSelected" +
" where HobbyId=@HobbyId";
cmd.Connection = conn;
conn.Open();
foreach (ListItem item in chkHobbies.Items)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
cmd.Parameters.AddWithValue("@HobbyId", item.Value);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
当程序第一次运行时
更新复选框中的值后,
您尝试过的代码在哪里?你能详细说明一下吗?因为很难根据什么条件和值来确定复选框将被选中,它们之间的映射是什么等等。@Manish我已经添加了一个代码,我已经使用了复选框类型,数据库中的数据应该是布尔值。通常它会自动检查value=true。好的,所以我可以从代码中看出for循环是问题所在。因为您正在迭代数组,并且基于条件设置checked属性为true。所以它将迭代数组中的所有项,并根据最后一项的值进行检查。方法是正确的,但您的代码不是。谢谢您。您尝试过的代码在哪里?你能详细说明一下吗?因为很难根据什么条件和值来确定复选框是否会被选中,它们之间的映射是什么等等。@Manish我已经添加了一个代码,我已经完成了