【问题标题】:Response.Redirect("Default.aspx"); not working [closed]Response.Redirect("Default.aspx");不工作[关闭]
【发布时间】:2013-04-29 11:21:34
【问题描述】:

在用户注册后,我试图将客户端重定向到默认页面,最简单的方法似乎是在提交按钮的末尾添加这一行:

Response.Redirect("Default.aspx");

但它不起作用,它停留在同一页面中而没有显示任何错误。

我也试过:

Server.Transfer("Default.aspx");

运气不好。

这是完整的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Carnisoftix
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            carniuser oUsr = new carniuser();
            oUsr.Email = txtEmail.Text;
            oUsr.Name = txtName.Text;
            oUsr.Pass = txtPassword.Text;
            oUsr.Phone = txtPhone.Text;
            oUsr.Usrname = txtUsername.Text;
            oUsr.Address = txtAddress.Text;
            oUsr.SpecialK = Convert.ToInt16(txtSpecial.Text);
            oUsr.Auth = 1;
            regUsr(oUsr);

            Response.Redirect("Default.aspx");
        }
        public static int regUsr(carniuser oUsr)
        {
            int oNum;
            SqlConnection oConnection = new SqlConnection("Server=.\\SQLExpress;AttachDbFilename=L:\\Apps\\VS Projects\\Carnisoftix\\CarniDb.mdf;Database=CarniDb;Trusted_Connection=Yes;");
            string oSql = "ADDUSR";
            SqlCommand oCom = new SqlCommand(oSql, oConnection);
            oCom.CommandType = CommandType.StoredProcedure;
            oCom.Parameters.AddWithValue("@useremail", oUsr.Email);
            oCom.Parameters.AddWithValue("@userpass", oUsr.Pass);
            oCom.Parameters.AddWithValue("@name", oUsr.Name);
            oCom.Parameters.AddWithValue("@phone", oUsr.Phone);
            oCom.Parameters.AddWithValue("@address", oUsr.Address);
            oCom.Parameters.AddWithValue("@username", oUsr.Usrname);
            oCom.Parameters.AddWithValue("@authority", oUsr.Auth);
            oCom.Parameters.AddWithValue("@special", oUsr.SpecialK);
            SqlParameter oReturn = new SqlParameter("@out", SqlDbType.Int);
            oReturn.Direction = ParameterDirection.ReturnValue;
            oCom.Parameters.Add(oReturn);
            oConnection.Open();
            oCom.ExecuteNonQuery();
            oNum = (int)oCom.Parameters["@out"].Value;
            oConnection.Close();
            return oNum;
        }
    }
    /*@useremail,
    @username,
    @userpass,
    @name,
    @phone,
    @address,
    @authority,
    @special*/
    public class carniuser
    {
        private string email, usrname, pass, name, phone, address;
        private int authority, specialK;
        public carniuser()
        {
            email = "";
            usrname = "";
            pass = "";
            name = "";
            phone = "";
            address = "";
            authority = 1;
            specialK = 1;
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public string Pass
        {
            get { return pass; }
            set { pass = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }
        public string Address
        {
            get { return address; }
            set { address = value; }
        }
        public string Usrname
        {
            get { return usrname; }
            set { usrname = value; }
        }
        public int Auth
        {
            get { return authority; }
            set { authority = value; }
        }
        public int SpecialK
        {
            get { return specialK; }
            set { specialK = value; }
        }

        public carniuser(string email, string usrname, string pass, string name, string phone, string address, int authority, int specialK)
        {
            //string _email = email;
            Email = email;
            Usrname = usrname;
            Pass = pass;
            Name = name;
            Phone = phone;
            Address = address;
            Auth = authority;
            SpecialK = specialK;
        }
    }
}

【问题讨论】:

  • 您确定您确实在提交数据吗?您是否设置了断点并看到事件处理程序已触发?
  • 试试这个->Response.Redirect("~/Default.aspx",true);
  • 您确定路径正确吗? Default.aspx 必须与当前页面位于同一文件夹中。
  • 或者你的Default.aspx文件夹服务器路径。Response.Redirect(Server.MapPath("~/path/Default.aspx"),true)
  • 请问您的默认页面是放在根文件夹还是放在任何文件夹中。

标签: c# asp.net redirect button


【解决方案1】:

我必须知道注册页面在哪里? Default.aspx 在不同的文件夹中?

但是试试这个

Response.Redirect("../Default.aspx");

或

Response.Redirect("~/Default.aspx");

我希望这会有所帮助。

【讨论】:

  • 两个都试过了,都在同一个目录下。我开始认为我应该重新启动 VS。
  • 您尝试追踪代码?也许有错误?
  • 在解决方案资源管理器中右键单击项目文件夹,然后单击清理
  • 我尝试清理项目但没有成功,重启后VS 2010现在可以正常工作了,耐人寻味。
  • 您的问题现在解决了吗?
【解决方案2】:

据我记得,提交按钮会导致回发,这将执行您的代码,然后重新加载页面。尝试将此添加到您的 Page_Load:

Response.Redirect("Default.aspx");

【讨论】:

  • 重定向已经在按钮点击内部(仅在 PostBack 上调用)
  • IsPostBack 在这种情况下没有任何作用......
  • 如果一个按钮被点击,它已经在回发中。删除 IsPostBack
  • 我只是尝试将其添加到 page_load 中,但没有任何运气
  • 不会错吧!因为表格每次都是 4-5 :) 转到“Default.apx”页面?
猜你喜欢
  • 1970-01-01
  • 2013-02-26
  • 2013-02-04
  • 2014-05-04
  • 2016-04-26
  • 2012-10-08
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多