为什么我的不提交(HtmlButton)仍然提交? [英] Why does my no-submit (HtmlButton) still submit?
问题描述
我想动态地创建行,这些行一直存在,但最初是隐藏的.
I want to dynamically make rows, which exist all along but are at first hidden, visible.
我尝试了客户端(jQuery)路由,但是遇到了问题.
I've tried the client-side (jQuery) route, but have had problems with that.
I would prefer going down the server-side (C#) road, and I thought I had found the way to accomplish it based on this thread and this code:
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混搭不会使第三行可见.然后逐步执行代码,我明白了为什么:每次我按下按钮时都会提交页面 ,因此初始代码再次运行,将可见的行数设置回两,并且总是第3行可见(从不显示第4行).
...but no go - the first time the second row is made visible, but a subsequent mashing of the "+" HtmlButton does not make the third row visible. And stepping through the code, I see why: the page is being submitted each time I mash the button, adn thus the initial code runs again, setting the number of rows visible back to two, and always making row3 visible (never row4).
这是将按钮捣碎后的样子,无论我将按钮捣碎多少次(应该再添加一行,但永远不会这样):
This is what it looks like after mashing the button, no matter how many times I mash the button (one more row should be added, but it never is):
行1,BTW,是列标题行;第2行是默认情况下可见的单行; row3和row4存在,但一开始不可见.
Row 1, BTW, is the column caption row; row 2 is the single row that is visible by default; row3 and row4 exist, but are not visible at first.
推荐答案
尝试使用 btnAddFoapalRow.Attributes.Add("onclick","return false;");
Try using btnAddFoapalRow.Attributes.Add("onclick", "return false;");
这篇关于为什么我的不提交(HtmlButton)仍然提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!