如何使用其数据加载控件? [英] How can I load Controls With its data?
本文介绍了如何使用其数据加载控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我有两张asp桌子。我在第一个表中根据数据库以编程方式创建了一些复选框,第二个表也根据从第一个表中选择的复选框添加了复选框,当我单击按钮时,所有复选框都随其数据消失。
Hi guys
I have two asp table. I create in the first table some checkboxes according to database programmatically and the second table also I add checkboxes according to checkboxes that selected from the first table, when I click button all of checkboxes disappear with its data.
void Page_Load(object sender, ArgEvent e)
{
DataView DV = Db.Qury("Select Id, Name from tblInputs");
for(int i = 0; i < DV.Count; i++)
{
Table1.Rows.Add(new TableRow());
TableCell cell = new TableCell();
CheckBox chkbox = new ChackBox();
chkbox.ID = DV[i][0].ToString();
chkbox.Text = DV[i][1].ToString();
cell.Controls.Add(chkbox);
Table1.Rows[Table1.Rows.Count - 1].Cell.Add(cell);
}
}
void Button_Click(object sender, ArgEvent e)
{
for(int i = 0; i < Table1.Rows.Count; i++)
{
foreach(Control item in Table1.Rows[i].Controls)
{
Checkbox chk = (Checkbox)item[0];
if(chk.Checked)
{
DataView DV = Db.Qury("Select Id, Name from Tranfer where InputId = " + chk.ID);
for(int i = 0; i < DV.Count; i++)
{
Table2.Rows.Add(new TableRow());
TableCell cell = new TableCell();
RadioButton Rdbtn = new RadioButton();
Rdbtn.ID = DV[i][0].ToString();
Rdbtn.Text = DV[i][1].ToString();
cell.Controls.Add(Rdbtn);
Table2.Rows[Table2.Rows.Count - 1].Cell.Add(cell);
}
}
}
}
}
问题:如何保存选中的复选框并在回发后显示?
对不起英语
The question: How can I save selected checkboxes and appear them after postback?
Sorry for English
推荐答案
正如Abdul Samad KP在page_Load中回答
然后将你的表添加到asession
As Abdul Samad KP answered in page_Load
Then add your table to asession
void Page_Load(object sender, ArgEvent e)
{
if (!IsPostBack)
{
DataView DV = Db.Qury("Select Id, Name from tblInputs");
for(int i = 0; i < DV.Count; i++)
{
Table1.Rows.Add(new TableRow());
TableCell cell = new TableCell();
CheckBox chkbox = new ChackBox();
chkbox.ID = DV[i][0].ToString();
chkbox.Text = DV[i][1].ToString();
cell.Controls.Add(chkbox);
Table1.Rows[Table1.Rows.Count - 1].Cell.Add(cell);
}
Session["mytb"]=Table1;
}
}
protected void btn_Submit_Click(object sender, EventArgs e)
{
//Here assign DataTable to the Session, modify/update the DataTable
DataTable mytb = (DataTable)Session["mytb"];
//after updating/modifying datatable assign it back to your table. Something like:
Table1=mytb;
}
我没有测试代码,但逻辑应该很好
希望它有所帮助!
注意,
Samira
I didn't test the code but the logic should be good
Hope it helps!
Regard,
Samira
这篇关于如何使用其数据加载控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文