根据数据表选择复选框列表中的复选框 [英] Select checkboxes in checkboxlist based on datatable
本文介绍了根据数据表选择复选框列表中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在edititemtemplate中有一个带有复选框列表的formview。我想预先选择数据表中的项目。我该怎么做
我尝试过:
我我已经尝试将checkboxlist项设置为后面代码中的数据表但是我找不到控件。
I have a formview with a checkboxlist in edititemtemplate. I want to preselect the items that are in a datatable. How do I do this
What I have tried:
I have tried setting the checkboxlist items to the datatable in the code behind but I cant find the control.
推荐答案
你可以尝试使用rowdatabound事件找到控件,如下所示:
You can try the rowdatabound event to find the control as shown below:
protected void formview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Checkboxlist ck= e.Row.FindControl("id_of_checkboxlist") as checkboxlist;
}
}
如果您的数据库存储一个标志来确定所选的CheckBox
项目,然后您可以简单地执行以下操作:
If your database stores a flag to determine the selectedCheckBox
items, then you could simply do something like:
for (int i = 0; i < CheckBoxList1.Items.Count; i++){
CheckBoxList1.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]);
}
IsSelected
作为数据库的布尔标志。
这是一个简单的例子:
IsSelected
as boolean flag from your database.
Here's a quick example:
protected void FormView1_DataBound(object sender, EventArgs e)
{
//Check for its current mode
if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{
//Check the RowType to where the Control is placed
if (FormView1.Row.RowType == DataControlRowType.DataRow)
{
//Just Changed the index of cells based on your requirement
CheckBoxList cal = (CheckBoxList)FormView1.Row.Cells[0].FindControl("YourCheckBoxControlIDHere");
if (cbl= !null)
{
DataTable dt = ???; //Set the datasource
//loop thru the items of checkboxlist and then preselect
for (int i = 0; i < cbl.Items.Count; i++){
cbl.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]);
}
}
}
}
}
这篇关于根据数据表选择复选框列表中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文