为什么我的 no-submit (HtmlButton) 仍然提交?

Why does my no-submit (HtmlButton) still submit?

我想动态地使一直存在但最初隐藏的行可见。

我尝试了 client-side (jQuery) 路线,但遇到了问题。

我更愿意走 server-side (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 的混搭不会使第三行可见。通过代码,我明白了原因:每次我按下按钮时都会提交页面 ,因此初始代码再次运行,将可见行数设置回两行,并始终使第 3 行可见(从不使第 4 行可见)。

这是按下按钮后的样子,无论我按下按钮多少次(应该再添加一行,但从来没有):

顺便说一句,第 1 行是列标题行;第 2 行是默认可见的单行; row3 和 row4 存在,但起初不可见。

尝试使用 btnAddFoapalRow.Attributes.Add("onclick", "return false;");