如何从数据库中生成dynemic复选框? [英] how to generate dynemic check box from the database?
问题描述
我的数据库中有一个表格,根据表格中的记录数量创建的复选框。
示例: - 我在表中有5条记录,所以我想要5自动创建一个复选框。
我发送给你的上一个代码是在pageload事件中写的。
问题出现在每次页面加载事件触发时创建的复选框中。
我也想在复选框事件中使用该复选框ID。
如果你知道答案,请提供帮助。
i have a table in my Database and according to number of record in table the checkbox created.
Example:- i have 5 record in table so i want 5 checkbox created automatically.
the previous code that i send you is written in the pageload event.
the problem is in the checkbox that are created everytime when pageload event fire.
I also want to use that checkbox ID in the checkbox event.
Please help if you know the answer.
推荐答案
我想你应该使用checkboxlist ........ .....
它会绑定您的记录并生成多个复选框...
例如
i think u should use checkboxlist .............
it bind your record and generate number of checkboxes...
for example
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection con=new SqlConnection ())
{
con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
con.Open ();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from [Table]";
cmd.Connection = con;
using (DataSet ds = new DataSet())
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "name";
CheckBoxList1.DataValueField = "Employeeid";
CheckBoxList1.DataBind();
}
}
}
或试试这个链接
http: //www.aspsnippets.com/Articles/Bind-CheckBoxList-from-Database-in-ASPNet.aspx [ ^ ]
您需要在限制条件下编写代码
Hi,
You need to write the code in a condition that will restrict
public void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
using (SqlConnection con=new SqlConnection ())
{
//your connection string
con.Open ();
using (SqlCommand cmd = new SqlCommand())
{
// your sql command
using (SQLDataReader rd = cmd.ExecuteReader())
{
while(rd.Read())
{
// suppose you have a panel in your page as panel1
CheckBox chk = new CheckBox();
chk.ID=string.Fromat("CheckBox_{0}", rd["PrimaryKey/AnyValue"]);
// bind event to checkbox
chk.Checked+= new EventHandler(checkbox_checked);
}
}
}
}
}
}
public void checkbox_checked(object sender, EventArgs e)
{
//your code----------------
}
您还需要在面板中添加此复选框
所以这是代码。
You also need to add this check box in panel
so here is the code.
while(rd.Read())
{
// suppose you have a panel in your page as panel1
CheckBox chk = new CheckBox();
chk.ID=string.Fromat("CheckBox_{0}", rd["PrimaryKey/AnyValue"]);
// bind event to checkbox
chk.Checked+= new EventHandler(checkbox_checked);
panel1.Controls.Add(chk);
}
这篇关于如何从数据库中生成dynemic复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!