我想动态地使一直存在但起初隐藏的行变得可见。
我已经尝试过客户端(jQuery)路由,但遇到了问题。
我更喜欢走服务器端(C#)的道路,我想我已经找到了基于这个线程和这个代码的方法来完成它:
HtmlButton btnAddFoapalRow = null;
. . .
btnAddFoapalRow = new HtmlButton();
btnAddFoapalRow.Attributes["type"] = "button";
btnAddFoapalRow.InnerHtml = "+";
btnAddFoapalRow.ID = "btnAddFoapalRow";
btnAddFoapalRow.ServerClick += new EventHandler(btnAddFoapalRow_Click);
this.Controls.Add(btnAddFoapalRow);
private void btnAddFoapalRow_Click(object sender, EventArgs e)
{
try
{
ShowNextFoapalRow();
}
catch (Exception ex)
{
String s = String.Format("Exception occurred: {0}", ex.Message); // TODO: Log this somewhere
}
}
//// This only works the first time, because it causes the page to be reloaded, setting foapalRowsShowing back to 2
private void ShowNextFoapalRow()
{
switch (foapalRowsShowing)
{
case 2:
foapalrow3.Visible = true;
foapalRowsShowing = 3;
break;
case 3:
foapalrow4.Visible = true;
foapalRowsShowing = 4;
btnAddFoapalRow.Disabled = true;
break;
}
}
foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";
foapalrow3.Visible = false;
. . .
foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";
foapalrow3.Visible = false;
...但不行-第二行第一次可见,但随后对“+”HtmlButton 的捣碎不会使第三行可见。并单步执行代码,我明白了原因:每次我混合按钮时都会提交页面,因此初始代码再次运行,将可见行数设置回两,并始终使 row3 可见(从不 row4)。
这是捣碎按钮后的样子,无论我捣碎按钮多少次(应该再添加一行,但它永远不会):
第 1 行,顺便说一句,是列标题行;第 2 行是默认可见的单行;row3 和 row4 存在,但起初不可见。