【问题标题】:Can I use more than One ASP.NET Button in a web page?我可以在网页中使用多个 ASP.NET 按钮吗?
【发布时间】:2014-03-04 00:46:00
【问题描述】:

我在母版页中使用了多个按钮。我的登录页面中还有一个按钮。问题是在使用登录按钮提交登录页面的内容时,它可以工作,但是当我在登录页面中单击主页按钮(主页面按钮)时,它不起作用,它作为提交按钮工作。

我的主页代码:

<body style="margin:0px;">
<form id="form1" runat="server">
<div>
    <div class="auto-style1" style="background-color: #3399FF; height: 42px;">
        <asp:Button ID="homeButton" runat="server" CssClass="auto-style2" Text="Home" Width="126px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="homeButton_Click" />
        <asp:Button ID="newsButton" runat="server" CssClass="auto-style3" Text="News" Width="127px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="newsButton_Click" />
        <asp:Button runat="server" CssClass="auto-style4" Text="Shared Files" Width="123px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="Unnamed1_Click" />
        <asp:Button ID="memberButton" runat="server" CssClass="auto-style5" Text="Members" Width="117px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="memberButton_Click" />
        <asp:Button ID="blogButton" runat="server" CssClass="auto-style6" Text="Blogs" Width="103px" BorderStyle="None" BackColor="#3366FF" Height="42px" OnClick="blogButton_Click" />

        <asp:Button ID="loginButton" runat="server" BackColor="#3366FF" BorderStyle="None" CssClass="auto-style8" Height="42px" Text="Log in" Width="82px" OnClick="loginButton_Click" />
        <asp:Button ID="Button1" runat="server" BackColor="#3366FF" BorderStyle="None" CssClass="auto-style9" Height="42px" Text="Register" Width="96px" OnClick="Button1_Click" />

    </div>
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>

</div>
</form>

我的母版页代码后面是:

protected void homeButton_Click(object sender, EventArgs e)
{
   Response.RedirectPermanent("Home.aspx");
}
protected void newsButton_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("News.aspx");
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("Shared_Files.aspx");
}
protected void memberButton_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("Members.aspx");
}
protected void blogButton_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("Blogs.aspx");
}
protected void loginButton_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("Login.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
    Response.RedirectPermanent("Register.aspx");
}

【问题讨论】:

  • 您可以拥有任意数量的按钮。定义“不起作用”。它做什么?是否调用了服务器端处理程序方法?那个处理程序是做什么的? Page_Load 中有什么东西会中断逻辑吗?
  • 不是很清楚,尽量让你的问题更容易理解
  • 如果您不希望它充当提交按钮,请查看此帖子 - stackoverflow.com/questions/4608921/…
  • 请告诉我您的母版页服务器端代码?
  • Ramesh Rajendran,我已经编辑了我的代码

标签: c# html asp.net webforms


【解决方案1】:

建议在母版页中使用菜单控件,而不是使用按钮控件在页面之间导航

【讨论】:

  • 是的,当我使用按钮失败时,我使用文本链接,然后我成功了。
  • 菜单控制总是更好的外观和易于访问。尤其是导航。一旦你开始使用站点地图,那么你就会了解这些事情
【解决方案2】:

ClickEventHandlers 的正文中有问题。您使用RedirectPermanent 方法。
来自msdn:

RedirectPermanent(String) 方法重载在响应中提供 301 HTTP 状态代码,并包含将请求重定向到的 URL。 301 HTTP 状态代码是 HTTP 响应中的标准代码。表示存在永久重定向,并提供重定向位置。

调用 RedirectPermanent(String) 方法重载会终止响应。

所以你需要改用简单的Redirect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多