单选按钮动态创建 [英] radio button dynamic creation
问题描述
我正在asp.net网站上工作.我想创建动态表
表格的每一行将包含一个文本框和一个单选按钮.
第一行是设计部分,我已经创建了它.我也给了表格底部的一个添加按钮来添加新行.
我可以在添加按钮单击时动态添加文本框,并保持其viewstate.我想维护viewstate,因为所有行中的数据都需要保存在datatbse中.
但是我没有得到如何动态地添加单选按钮并保持其视图状态的方法.这些所有单选按钮应属于同一组单选按钮.
I m working on asp.net website. i want to create dynamic table
Each row of table will contain one text box and one radiobutton.
The first row is design part and i have created it. Also i have given one add button at bottom of table to add new row.
I m able to add textbox dynamically at add button click and maintain it''s viewstate. I want to maintain the viewstate because data in all rows i need to save in datatbse.
But i not getting how to add radiobutton dynamically add radiobutton and maintain it''s view state. These all radiobuttons should belong to same set of radiobuttons
推荐答案
试试这个将指导您.
Hi ,
Try this will Guide you .
int countTimes = 0;
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["countTimes"] == null)
{
countTimes = 1;
}
else
{
countTimes = Convert.ToInt32(ViewState["countTimes"]);
}
for (int i = 0; i < countTimes; i++)
{
TableRow tr = new TableRow();
TableCell cl = new TableCell();
TableCell cl2 = new TableCell();
RadioButton raid = new RadioButton();
raid.ID = "raid" + i;
TextBox txt = new TextBox();
txt.ID = "txt"+i;
cl.Controls.Add(txt);
cl2.Controls.Add(raid);
tr.Controls.Add(cl);
tr.Controls.Add(cl2);
Table1.Controls.Add(tr);
}
countTimes = countTimes + 1;
ViewState.Add("countTimes", countTimes);
}
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
最好的问候
M.Mitwalli
Best Regards
M.Mitwalli
我们可以使用Request.Form ["control_id");
获得动态控制的价值 重新创建控件对于每个回发,因此无需在此处保持视图状态
We can get value of dynamic control By Using Request.Form["control_id");
Re Create Controls For every post-back so that no need to maintain viewstate here
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList radBtnList = new RadioButtonList();
radBtnList.Items.Add(new ListItem("Delete","1"));
radBtnList.Items.Add(new ListItem("Keep", "0"));
//radBtnList.ID
radBtnList.AutoPostBack = true;
radBtnList.RepeatDirection = RepeatDirection.Horizontal;
Panel1.Controls.Add(radBtnList);
}
}
}
这篇关于单选按钮动态创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!