经典问题:动态生成控件

MasterLRC 2003-09-06 10:21:01
我想实现:

点击一个按钮,则自动在 TABLE中 加入一行,生成并加入一个 DropDownList 控件

可是我每点一次,总是有一个控件,原来加入行的就没有了,这可能是第点一次按钮,则页面刷新一次,把上次动态加入的给刷没了吧。

那么,我怎么保留以前加入的行呢?



...全文
27 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
eagle_hb 2003-09-11
  • 打赏
  • 举报
回复
MARK!!
cnhgj 2003-09-06
  • 打赏
  • 举报
回复
如果你的dropdownlist不用动态绑定与创建事件的话,或者也可以用<select></select>htmlcontrol代替
cnhgj 2003-09-06
  • 打赏
  • 举报
回复
如果你的dropdownlist不用动态绑定与创建事件的话,或者也可以用<select></select>htmlcontrol代替
MasterLRC 2003-09-06
  • 打赏
  • 举报
回复
到了这里我越来越自卑:```

但我会努力的。

cnhgj 2003-09-06
  • 打赏
  • 举报
回复
刚刚测试了一下,这个办法不好!程序无法再次编译webcontrol,用思归大侠的办法
MasterLRC 2003-09-06
  • 打赏
  • 举报
回复
to cnhgj(戏子)

是个好办法!

不知道能不能响应 SelectChanged 事件。
saucer 2003-09-06
  • 打赏
  • 举报
回复
<form runat=server id=form1>
<asp:Table id=tbl runat=server />
<asp:Button id=btn runat=server text=add Onclick="Add" />
<asp:Button id=btn2 runat=server text=submit/>
</form>
saucer 2003-09-06
  • 打赏
  • 举报
回复
you need to re-create the controls created in the previous requests upon postback

for example:

<form runat=server id=form1>
<asp:Table id=tbl runat=server />
<asp:Button id=btn runat=server text=add Onclick="Add" />
</form>

<script language=C# runat=server>
int Count
{
get
{
object o = ViewState["Count"];
if (o==null)
return 0;
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value;
}
}

DropDownList AddDDL()
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();

DropDownList ddl = new DropDownList();
tc.Controls.Add(ddl);
tr.Cells.Add(tc);
tbl.Rows.Add(tr);

return ddl;
}

void PopulateDLL(DropDownList ddl)
{
for (int i=1; i <=10; i++)
ddl.Items.Add(i.ToString());
}


void Page_Load(Object sender, EventArgs e)
{
for (int i=0; i < Count; i++)
{
AddDDL();
}
}

void Add(Object sender, EventArgs e)
{
DropDownList ddl= AddDDL();
PopulateDLL(ddl);
Count++;
}

</script>
windrain2001 2003-09-06
  • 打赏
  • 举报
回复
When you click Button, page will postback, thus origin added info will be lost.
It is the limitation of your used control.

Implement method:
use ViewState to store the grid information.
saucer 2003-09-06
  • 打赏
  • 举报
回复
you need to re-create the controls created in the previous requests upon postback
saucer 2003-09-06
  • 打赏
  • 举报
回复
you need to re-create the controls created in the previous requests upon postback
流梓 2003-09-06
  • 打赏
  • 举报
回复
关注!
cnhgj 2003-09-06
  • 打赏
  • 举报
回复
public static string str = "";

//buttonclick

str += "<tr><td><asp:dropdownlist runat=\"server\" id=\"dp\"></asp:dropdownlist></td></tr>";



在要插入行的地方
<%=str%>
MasterLRC 2003-09-06
  • 打赏
  • 举报
回复
不知能否给一下示例,谢谢!
2002pine 2003-09-06
  • 打赏
  • 举报
回复
我做过象这样的东西,你可以用一个DataGrid ,绑定dropDownList,每按一次,将表的记录加一条就可

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧