【问题标题】:ASP.NET Button to redirect to another pageASP.NET 按钮重定向到另一个页面
【发布时间】:2014-07-21 12:24:51
【问题描述】:

如何对按钮进行编码,以便当我单击该按钮时,它会将我带到另一个 Web 表单?假设按钮名称是 Confirm 并且 wed 表单是 confirm.aspx ?

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        (guessing that there should be an input here)
    }

【问题讨论】:

    标签: asp.net button webforms visual-studio-2013


    【解决方案1】:

    你可以使用PostBackUrl="~/Confirm.aspx"

    例如:

    在您的 .aspx 文件中

    <asp:Button ID="btnConfirm" runat="server" Text="Confirm" PostBackUrl="~/Confirm.aspx" />

    或在您的 .cs 文件中

    btnConfirm.PostBackUrl="~/Confirm.aspx"

    【讨论】:

    • 使用 btn.PostBackUrl 与 Response.Redirect 相比有什么优势?
    • 检查这个daniweb.com/web-development/aspnet/threads/69851/… 基本上 PostBackUrl 是为了在同一个服务器中使用,因为它会保存 viewState。 Response.Redirect 可以将您带到一个全新的服务器/网站,全新的会话将在该处开始
    【解决方案2】:

    你可以用这个:

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
      Response.Redirect("Confirm.aspx");
    }
    

    【讨论】:

      【解决方案3】:

      您可以在按钮单击事件上执行Response.Redirect("YourPage.aspx");Server.Transfer("YourPage.aspx");。 所以它会像下面这样:

      protected void btnConfirm_Click(object sender, EventArgs e)
      {
          Response.Redirect("YourPage.aspx");
          //or
          Server.Transfer("YourPage.aspx");
      }
      

      【讨论】:

        【解决方案4】:
        <button type ="button" onclick="location.href='@Url.Action("viewname","Controllername")'"> Button name</button>
        

        例如,

        <button type="button" onclick="location.href='@Url.Action("register","Home")'">Register</button>
        

        【讨论】:

          猜你喜欢
          • 2020-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-07
          • 2019-09-01
          • 2013-01-17
          • 2019-09-15
          相关资源
          最近更新 更多