odella
odella

Reputation: 11

Clicking button won't redirect to another page

For school project, i am assigned to make a simple login form with c# or asp.net. I use Response.Redirect() but whenever i click the button, it only refreshes the login page and does not direct to another page. Can someone help me?

This is the source code for the login button in login_page.aspx:

<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" OnServerClick="btnLogin_Click"/> 

and this is the code behind:

using System;
using System.Web;
using System.Data;
using System.Text;

namespace WebApplication1
{
    public partial class LoginPage : System.Web.UI.Page
    {
        public string txtUserName { get; private set; }
        public string txtPassword { get; private set; }
        public object lblPassword { get; private set; }
        public object lblUsername { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName == "admin" && txtPassword == "password")
                {
                Response.Redirect(url: "admin_page.aspx");
                }
                else
                {
                Response.Redirect(url: "not_page.aspx");
                }
            }
    }
}

Upvotes: 0

Views: 4380

Answers (3)

Shreyas Pednekar
Shreyas Pednekar

Reputation: 1305

Try this. It should be onClick event.

<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" onClick="btnLogin_Click"/>



protected void btnLogin_Click(object sender, EventArgs e)
{
    if (txtUserName == "admin" && txtPassword == "password")
    {
        Response.Redirect("~/admin_page.aspx");
    }
    else
    {
        Response.Redirect("~/not_page.aspx");
    }
}

Upvotes: 1

Ratheesh
Ratheesh

Reputation: 569

If you are using asp:Button control, it does not have any event called OnServerClick. It is associated with HTML Button

asp:Button control has OnClick for handling server side click event and OnClientClick for handing client side click event.

Hope this clarifies.

Upvotes: 2

Vishwa Ratna
Vishwa Ratna

Reputation: 6390

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

remove: url: and add relative path ~/

Or use PostBackUrl in .aspx file.

<asp:Button ID="btnLogin" runat="server" Height="34px" Text="Login" Width="102px" OnServerClick="btnLogin_Click" PostBackUrl="~/admin_page.aspx" /> 

Upvotes: 0

Related Questions