Response.Redirect(“Default.aspx”); not working [closed]

好久不见. 提交于 2021-02-08 09:01:34

问题


After the user registers im trying to redirect the client to the default page and the easiest way to do it seems to be adding this line at the end of the submit button:

Response.Redirect("Default.aspx");

but its not working, it stays in the same page without showing any errors.

I've also tried with :

Server.Transfer("Default.aspx");

without any luck.

Heres the full code.

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;
        }
    }
}

回答1:


I must know where is the registration page? Default.aspx is in different folder?

But Try this

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

or

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

I hope this helps.




回答2:


As far as i remember, the submit button will cause a postback, which will execute your code, and reload the page afterwards. Try adding this to your Page_Load:

Response.Redirect("Default.aspx");


来源:https://stackoverflow.com/questions/16277376/response-redirectdefault-aspx-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!