for (int i = 0; i < n; i++)
{
DropDownList2.Items.Add("第"+(i+1)+"题");
Label lb = new Label();
lb.Text =Convert.ToString(i + 1);
Panel1.Controls.Add(lb);
//1、如何给lb指定name属性,在winform可以,asp.net不可以
//2、这里如何换行?asp.net里label没有position属性
}
以上两个问题如何解决?
9 个解决方案
#1
可以,我建议用JS实现。
用.net的话,请搜索"动态添加服务器控件"
用.net的话,请搜索"动态添加服务器控件"
#2
要怎么换行呢?而且我发现一旦刷新页面后新建的动态按钮全部消失了
#3
我是放在Table中创建的
#4
1、服务器控件没有Name属性,只有ID属性
2、换行你可以这样:
Literal lt = new Literal();
lt.Text = "<br />";
Panel1.Controls.Add(lt);
2、换行你可以这样:
Literal lt = new Literal();
lt.Text = "<br />";
Panel1.Controls.Add(lt);
#5
提供思路
<asp:Table ID="showdiv" runat="server">
</asp:Table>
protected void Page_Load(object sender, EventArgs e)
{
List<string> a = new List<string>() { "a1", "a2","3a","4a","5a",};
TableRow trs = new TableRow();
TableCell tc1s = new TableCell();
TableCell tc2s = new TableCell();
tc1s.Width = 118;
tc2s.Width = 527;
tc1s.VerticalAlign = VerticalAlign.Middle;
tc2s.VerticalAlign = VerticalAlign.Middle;
tc1s.Style.Add("text-align", "left");
tc2s.Style.Add("text-align", "left");
Label l = new Label();
l.Text = "部门名";
tc1s.Controls.Add(l);
Label l1 = new Label();
l1.Text = "当前序号";
tc2s.Controls.Add(l1);
trs.Cells.Add(tc1s);
trs.Cells.Add(tc2s);
showdiv.Rows.Add(trs);
foreach (var b in a)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Width = 118;
tc2.Width = 527;
tc1.VerticalAlign = VerticalAlign.Middle;
tc2.VerticalAlign = VerticalAlign.Middle;
Label l2 = new Label();
l2.Text ="aaa";
tc1.Controls.Add(l2);
TextBox tb = new TextBox();
// tb.ID = b.tex
// tb.Text = b.ordinal.ToString();
tc2.Controls.Add(tb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
showdiv.Rows.Add(tr);
}
}
<asp:Table ID="showdiv" runat="server">
</asp:Table>
protected void Page_Load(object sender, EventArgs e)
{
List<string> a = new List<string>() { "a1", "a2","3a","4a","5a",};
TableRow trs = new TableRow();
TableCell tc1s = new TableCell();
TableCell tc2s = new TableCell();
tc1s.Width = 118;
tc2s.Width = 527;
tc1s.VerticalAlign = VerticalAlign.Middle;
tc2s.VerticalAlign = VerticalAlign.Middle;
tc1s.Style.Add("text-align", "left");
tc2s.Style.Add("text-align", "left");
Label l = new Label();
l.Text = "部门名";
tc1s.Controls.Add(l);
Label l1 = new Label();
l1.Text = "当前序号";
tc2s.Controls.Add(l1);
trs.Cells.Add(tc1s);
trs.Cells.Add(tc2s);
showdiv.Rows.Add(trs);
foreach (var b in a)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Width = 118;
tc2.Width = 527;
tc1.VerticalAlign = VerticalAlign.Middle;
tc2.VerticalAlign = VerticalAlign.Middle;
Label l2 = new Label();
l2.Text ="aaa";
tc1.Controls.Add(l2);
TextBox tb = new TextBox();
// tb.ID = b.tex
// tb.Text = b.ordinal.ToString();
tc2.Controls.Add(tb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
showdiv.Rows.Add(tr);
}
}
#6
为什么每次posback后生成的动态label就消失了呢?有法子解决吗?
#7
在Page_Load中把动态添加控件的代码再写一次,而且不能写在if(!IsPostBack)里
#8
可以动态为服务器端的空间添加自定义的属性
Label lb = new Label();
lb.Text = "label1";
lb.Attributes.Add("name", "lblName");
#9
动态生成的控件每次页面刷新或者是PostBack的时候都要从新初始化并添加到控件树种
#1
可以,我建议用JS实现。
用.net的话,请搜索"动态添加服务器控件"
用.net的话,请搜索"动态添加服务器控件"
#2
要怎么换行呢?而且我发现一旦刷新页面后新建的动态按钮全部消失了
#3
我是放在Table中创建的
#4
1、服务器控件没有Name属性,只有ID属性
2、换行你可以这样:
Literal lt = new Literal();
lt.Text = "<br />";
Panel1.Controls.Add(lt);
2、换行你可以这样:
Literal lt = new Literal();
lt.Text = "<br />";
Panel1.Controls.Add(lt);
#5
提供思路
<asp:Table ID="showdiv" runat="server">
</asp:Table>
protected void Page_Load(object sender, EventArgs e)
{
List<string> a = new List<string>() { "a1", "a2","3a","4a","5a",};
TableRow trs = new TableRow();
TableCell tc1s = new TableCell();
TableCell tc2s = new TableCell();
tc1s.Width = 118;
tc2s.Width = 527;
tc1s.VerticalAlign = VerticalAlign.Middle;
tc2s.VerticalAlign = VerticalAlign.Middle;
tc1s.Style.Add("text-align", "left");
tc2s.Style.Add("text-align", "left");
Label l = new Label();
l.Text = "部门名";
tc1s.Controls.Add(l);
Label l1 = new Label();
l1.Text = "当前序号";
tc2s.Controls.Add(l1);
trs.Cells.Add(tc1s);
trs.Cells.Add(tc2s);
showdiv.Rows.Add(trs);
foreach (var b in a)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Width = 118;
tc2.Width = 527;
tc1.VerticalAlign = VerticalAlign.Middle;
tc2.VerticalAlign = VerticalAlign.Middle;
Label l2 = new Label();
l2.Text ="aaa";
tc1.Controls.Add(l2);
TextBox tb = new TextBox();
// tb.ID = b.tex
// tb.Text = b.ordinal.ToString();
tc2.Controls.Add(tb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
showdiv.Rows.Add(tr);
}
}
<asp:Table ID="showdiv" runat="server">
</asp:Table>
protected void Page_Load(object sender, EventArgs e)
{
List<string> a = new List<string>() { "a1", "a2","3a","4a","5a",};
TableRow trs = new TableRow();
TableCell tc1s = new TableCell();
TableCell tc2s = new TableCell();
tc1s.Width = 118;
tc2s.Width = 527;
tc1s.VerticalAlign = VerticalAlign.Middle;
tc2s.VerticalAlign = VerticalAlign.Middle;
tc1s.Style.Add("text-align", "left");
tc2s.Style.Add("text-align", "left");
Label l = new Label();
l.Text = "部门名";
tc1s.Controls.Add(l);
Label l1 = new Label();
l1.Text = "当前序号";
tc2s.Controls.Add(l1);
trs.Cells.Add(tc1s);
trs.Cells.Add(tc2s);
showdiv.Rows.Add(trs);
foreach (var b in a)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Width = 118;
tc2.Width = 527;
tc1.VerticalAlign = VerticalAlign.Middle;
tc2.VerticalAlign = VerticalAlign.Middle;
Label l2 = new Label();
l2.Text ="aaa";
tc1.Controls.Add(l2);
TextBox tb = new TextBox();
// tb.ID = b.tex
// tb.Text = b.ordinal.ToString();
tc2.Controls.Add(tb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
showdiv.Rows.Add(tr);
}
}
#6
为什么每次posback后生成的动态label就消失了呢?有法子解决吗?
#7
在Page_Load中把动态添加控件的代码再写一次,而且不能写在if(!IsPostBack)里
#8
可以动态为服务器端的空间添加自定义的属性
Label lb = new Label();
lb.Text = "label1";
lb.Attributes.Add("name", "lblName");
#9
动态生成的控件每次页面刷新或者是PostBack的时候都要从新初始化并添加到控件树种