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

时间:2015-06-19 21:59:10

标签: javascript c# jquery sharepoint-2010 htmlbutton

我想动态制作行,这些行一直存在,但最初是隐藏的,可见。

我已尝试过客户端(jQuery)路由,但遇到了问题。

我更倾向于沿着服务器端(C#)走下去,我想我已经找到了基于this thread和以下代码完成它的方法:

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的混搭并没有使第三行可见。然后单步执行代码,我明白为什么:每次我按下按钮时都会提交页面 ,因此初始代码再次运行,将可见行数设置为2,并始终制作row3可见(从不row4)。

这就是在捣碎按钮之后的样子,无论我多少次按下按钮(应该添加一行,但它永远不会):

enter image description here

第1行BTW是列标题行;第2行是默认可见的单行; row3和row4存在,但最初不可见。

1 个答案:

答案 0 :(得分:1)

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