ASP.NET提交按钮事件未执行

时间:2014-01-17 08:32:39

标签: c# asp.net form-submit code-behind

表单提交按钮的代码隐藏事件未执行。

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true" Inherits="_Default" %>

<form id="form1" runat="server">
<input id="Submit1" type="submit" value="Update" runat="server" onclick="btnclick_Click" />
<asp:TextBox ID="tbStatus" enableViewState="true" runat="server" TextMode="MultiLine" 
    Width="296px" Height="67px" ReadOnly="True"></asp:TextBox>
 </form>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
          tbStatus.Text = "Status: Page loaded";
    }
}

protected void btnclick_Click(object sender, EventArgs e)
{
   tbStatus.Text += "\nButton clicked. ";
}

btnclick_Click永远不会执行。如果我将Page_Load更改为:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
          tbStatus.Text = "Status: Page loaded";
    }
} else {
      tbStatus.Text += "\nUpdated. (Page_Load PostBack). ";
}

然后textarea更新两次,在第三次发布时我遇到了错误消息:

The state information is invalid for this page and might be corrupted.

我只想要执行按钮提交事件。提前感谢有关我做错的任何信息!

2 个答案:

答案 0 :(得分:3)

我认为您应该使用服务器控件本身来调用页面方法。如果您仍想继续使用html input,请尝试

<input id="Submit1 "runat="server" type="button" onserverclick="btnclick_Click" />

有关HtmlButton.OnServerClick

的详情

答案 1 :(得分:1)

尝试更改为asp.net按钮

<asp:Button ID="Submit1" runat="server" Text="Update" OnClick="btnClick_Click"/>

代码背后:

protected void btnclick_Click(object sender, EventArgs e)
{
   tbStatus.Text += Environment.NewLine + "Button clicked. ";
}