【问题标题】:ASP.NET Webforms Multiple ButtonsASP.NET Webforms 多个按钮
【发布时间】:2021-08-10 05:55:19
【问题描述】:

我正在制作一个ASP.NET Web 表单应用程序。我正在尝试一种简单的方式来访问表单上的多个控件。

我正在尝试访问 html 按钮的 serverClick 事件,但它没有工作,并且每页只能在一个按钮上工作。

我首先尝试使用此代码来获取单击 HTML 按钮时的单击事件。这是 HTML 按钮的代码。

<form runat="server">
<div class="btn-container">
<a runat="server" onserverclick="infobutton_Click" class="inf-btn" exc-content="inf_button" id="infoButton">Click Here for More Information</a>
</div>
</form>

此代码获取按钮的单击事件并显示示例消息框。

Protected Sub infoButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('" & "The Button Was Clicked" & "')", True) 'Displays a JavaScript Alert
End Sub

此代码适用于每页一个按钮,但是,我想在一页上使用多个按钮。如果我删除 &lt;form&gt; 标签,按钮单击事件不会注册。如果我为每个按钮添加一个&lt;form&gt; 标签,它会报告一个错误,即不能有多个&lt;form&gt; 标签。如果我用&lt;form&gt; 标记将整个页面括起来,则错误报告配置“已损坏”。

如果我有一个 ASP.NET 按钮控件 (&lt;asp:button&gt;),这将很简单,但我无法转换页面上的所有元素。

我不确定哪里出了问题,因为网上的所有信息都说它是正确的。有什么办法可以解决这个问题吗?

【问题讨论】:

  • 你说你想在一页上使用多个按钮。你能告诉我们你尝试了什么吗?无论如何,对我来说还不清楚为什么那不起作用。忘记表单标签,只需添加另一个或两个按钮,然后向我们展示您在页面和代码隐藏中所做的事情。重复您对第一个按钮所做的操作应该是您需要做的所有事情。
  • 是的,需要 (1) 个表单标签,我并不是说不要使用表单标签。但我还是不明白问题出在哪里。
  • 我有两个按钮。它们之间有元素。如果我将这些元素与两个按钮标签一起封装起来,ASP 会报告文件损坏错误。
  • @wazz 例如,body 标签不能在表单中。

标签: c# html asp.net vb.net webforms


【解决方案1】:
<body>
    <form id="form1" runat="server">

        <div class="btn-container">
            <a runat="server" onserverclick="infobutton1_Click" id="infoButton1">
                Click Here for More Information</a>
            <a runat="server" onserverclick="infobutton2_Click" id="infoButton2">
                Click Here for More Information</a>
            <a runat="server" onserverclick="infobutton3_Click" id="infoButton3">
                Click Here for More Information</a>
        </div>

    </form>
</body>

代码隐藏 (C#)

protected void infobutton1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 1 Was Clicked" + "')", true);
}
protected void infobutton2_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 2 Was Clicked" + "')", true);
}
protected void infobutton3_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
        "alertMessage", "alert('" + "Button 3 Was Clicked" + "')", true);
}

【讨论】:

  • 如果我将两个按钮都包含在一个表单中,我会收到此错误:Server Error in '/' Application. The state information is invalid for this page and might be corrupted. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.
  • 这与代码无关。这更多与(类似于)浏览器的缓存不同意您尝试加载的新内容有关。这通常可以通过使用 F5 或 Ctrl+F5 重新加载页面,或清除缓存或更极端的操作(例如重新启动浏览器和/或 IDE,和/或清除缓存的 ASP.NET 文件夹)来消除(应该不是必需的)。确保页面上没有旧 HTML 的残余或应删除的随机内容。
  • 我相信我发现了错误,对不起。有一个随机的
    标签在搜索中隐藏。现在可以了。
  • 酷。这通常会发生。
猜你喜欢
  • 1970-01-01
  • 2020-12-25
  • 2021-12-14
  • 2013-11-15
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
相关资源
最近更新 更多