为什么 点击Button会将 动态生成的DropDownList中不绑定的数据清空?

M057612 2013-01-16 04:03:48
为什么 点击Button会将 动态生成的DropDownList中不绑定的数据清空?


for (int i = 0; i < temp; ++i)
{
this.NewTableRow(i);
}

protected void NewTableRow(int i)
{
TableRow tr = new TableRow();

TableCell tc;
TextBox tb;
Label td;
DropDownList tl;

int rowqty = i + 1;

tc = new TableCell();
td = new Label();
td.CssClass = "FieldStyle";
td.ID = "txtDel" + i;

td.Text = "" + rowqty;
tc.Controls.Add(td);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtItemno" + i;
tb.Attributes.Add("ondblclick", "getItem('" + i + "');");
tb.Attributes.Add("onclick", "getSelect('txtItemno"+ i + "');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();

tb.CssClass = "FieldStyle";
tb.TextMode = TextBoxMode.MultiLine;
tb.Rows = 2;
tb.ID = "txtDes" + i;
tb.Attributes.Add("onblur", "getfu('txtDes" + i + "');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtModel" + i;
tb.Attributes.Add("onblur", "getfu('txtModel" + i + "');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtQty" + i;
tb.Attributes.Add("onblur", "getsum('txtQty" + i + "');");
tb.Attributes.Add("onclick", "getSelect('txtQty" + i + "');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtUOM" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
//tb.ReadOnly = true;
tb.ID = "txtDate" + i;
tb.Attributes.Add("onclick","SelectDate(this,'yyyy-MM-dd')");
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtUPrice" + i;
tb.Attributes.Add("onblur", "getsum('txtUprice" + i + "');");
tb.Attributes.Add("onclick", "getSelect('txtUprice" + i + "');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);


tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtAccount" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);

tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtQuote" + i;
tc.Controls.Add(tb);
tr.Cells.Add(tc);

/*tc = new TableCell();
tb = new TextBox();
tb.CssClass = "FieldStyle";
tb.ID = "txtBuyer" + i;
tb.ReadOnly();
tc.Controls.Add(tb);
tr.Cells.Add(tc);*/

tc = new TableCell();

tl = new DropDownList();
tl.CssClass = "FieldStyle";
tl.ID = "txtBuyer" + i;
// tl.Style.Add("height", "21px");
for (int j = 0; j < tBuyer.Length; j++)
{
if (tBuyer[j]=="" | tBuyer[j]==null)
break;
tl.Items.Add(new ListItem(tBuyer[j],tBuyer[j]));
}
tr.Cells.Add(tc);
tc.Controls.Add(tl);



//DropDownList("txtBuyer" + i).DataSource = tBuyer;
//DropDownList("txtBuyer" + i).DataBind();
// Enumerable.Range(1, 10).Select("txtBuyer" + i).ToList().ForEach(tl.Items.Add(i));



this.m_table.Rows.Add(tr);

}


protected void btnAddDetail_Click(object sender, EventArgs e)
{

this.NewTableRow(this.Num++);
Label1.Text = this.Num.ToString();

}
...全文
58 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
M057612 2013-01-16
  • 打赏
  • 举报
回复
我是点击按钮动态添加的,不能放在page_load中,怎么办啊
M057612 2013-01-16
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
引发了页面重新加载,因为是服务端组件,将数据绑定放到pageload中,加上判断 ispostback

流浪的麻雀 2013-01-16
  • 打赏
  • 举报
回复
Button会是服务器控件,引发了页面回发,要么在pageload加上if(!ispostBack){绑定数据},要么,点击Button后,重新绑定。
  • 打赏
  • 举报
回复
重新绑定一下dropdownlist
bdmh 2013-01-16
  • 打赏
  • 举报
回复
引发了页面重新加载,因为是服务端组件,将数据绑定放到pageload中,加上判断 ispostback

62,047

社区成员

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

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

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

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