Click here to Skip to main content
14,304,866 members
Rate this:
Please Sign up or sign in to vote.
Hi,

I am new to asp.net. So just learning everything step by step.

I have created a login page and now on button click on login, i want the page to redirect it to next page "change password" page.

I have written the code, but its not redirecting to next page.

I will post the code here.
can anyone help me,where I am wrong. I may be wrong as I am new to this language.

Waiting for your help
Thanks
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data


Public Class Login1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("loginConnectionString").ConnectionString)
        con.Open()
        Dim cmd As New SqlCommand("select * from log where username =@username and Pass=@password", con)
        cmd.Parameters.AddWithValue("@username", TextBox1.Text)
        cmd.Parameters.AddWithValue("@password", TextBox2.Text)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            Response.Redirect("ChangePassword.aspx")
        Else
            ClientScript.RegisterStartupScript(Page.[GetType](), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>")
        End If

    End Sub

   
End Class


this is my html code
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Login.aspx.vb" Inherits="WebApplication2.Login1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: medium;
            font-family: Dotum;
        }
        .style2
        {
            color: #FFFFFF;
            background-color: #3333FF;
        }
        .style3
        {
            width: 55%;
            height: 69px;
            margin-top: 33px;
        }
        .style4
        {
            width: 152px;
        }
        .style5
        {
            width: 190px;
        }
    </style>
</head>
<body style="height: 226px">
    <form id="form1" runat="server">
    <div class="style1">
    
                                 
        <span class="style2">Login Page<br />
        </span>
    
    </div>
    <table class="style3">
        <tr>
            <td class="style4">
                UserName</td>
            <td class="style5">
                <asp:TextBox ID="TextBox1" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="TextBox1" ErrorMessage="You must enter Username..!!!" 
                    ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Password</td>
            <td class="style5">
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="TextBox2" ErrorMessage="You must enter the password ...!!" 
                    ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style4">
                 </td>
            <td class="style5">
                <asp:Button ID="Button1" runat="server" BackColor="Blue" ForeColor="White" 
                    Text="Login" Width="100px" />
            </td>
            <td>
                 </td>
        </tr>
        <tr>
            <td class="style4">
                 </td>
            <td class="style5">
                 </td>
            <td>
                <asp:HyperLink ID="HyperLink1" runat="server" 
                    NavigateUrl="~/Account/Register.aspx">New User Register Here</asp:HyperLink>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Posted
Updated 26-Feb-19 3:53am
v2
Comments
Yuvaraj Arasu 25-Sep-13 23:52pm
   
This is not redirecting-OK, But what's happenning ? like Anything displayed on screen as error or the same page still reloads.. etc.,
Check the ChangePassword.aspx page also resides in the same directory in which your login1.aspx page resides.
sudeshna from bangkok 25-Sep-13 23:55pm
   
Yes the ChangePassword.aspx and login.aspx are in same account directory
sudeshna from bangkok 25-Sep-13 23:57pm
   
sorry this url is showing but the page is not getting redirected

http://localhost:13523/Account/Login.aspx?ReturnUrl=%2fAccount%2fChangePassword.aspx
Rate this:
Please Sign up or sign in to vote.

Solution 1

use Response.Redirect("~/ChangePassword.aspx"),if it is in root directory. if it is inside any directory, then use directory name like Response.Redirect("~/Account/ChangePassword.aspx")
   
Comments
sudeshna from bangkok 26-Sep-13 0:39am
   
I did but same.no change. is anything wrong with my code? in the if statement? please can you re check it.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi sudeshna from bangkok,

If you want to redirect to any page in the same web site we can use two ways.

1. Response.Redirect(" Here is the complete path of the destination page");
2. Server.Transfer("Here is the complete path of the destination page");

There is slight difference in between Response.Redirect and  Server.Transfer.
   
Comments
sudeshna from bangkok 26-Sep-13 0:40am
   
I used response.redirect with full path, but no change,page is not getting redirected to other page
Ram424 26-Sep-13 0:53am
   
what you are getting after executing Response.Redirect statement.
sudeshna from bangkok 26-Sep-13 0:59am
   
same login page is displaying
Ram424 26-Sep-13 1:13am
   
first debug your code and send me the same redirecting method code.
Ram424 26-Sep-13 1:04am
   
Could you post your c# Redirecting code now?
Prince Tegaton 26-Sep-13 1:08am
   
Use breakpoint to see if that line of code is being executed.
Ram424 26-Sep-13 1:12am
   
Actually after execution of this statement he will get an exception and redirect to destination page.
sudeshna from bangkok 26-Sep-13 1:27am
   
i deleted that project and created a new one,
I will post the code
sudeshna from bangkok 26-Sep-13 1:27am
   
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class log
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("loginConnectionString").ConnectionString)
con.Open()
Dim cmd As New SqlCommand("select * from log where username =@username and Pass=@password", con)
cmd.Parameters.AddWithValue("@username", TextBox1.Text)
cmd.Parameters.AddWithValue("@password", TextBox2.Text)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Response.Redirect("Account/ChangePassword.aspx")
Else
ClientScript.RegisterStartupScript(Page.[GetType](), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>")
End If
End Sub
End Class
Ram424 26-Sep-13 1:35am
   
k finally you didn't debug your code.
One more thing is it's VB.net not asp.net.
My guess is <pre>dt.Rows.Count</pre> getting '0' so debugger is going to else part and still you have a problem with ClientScript.RegisterStartupScript
sudeshna from bangkok 26-Sep-13 1:28am
   
now the page is being redirected to login.aspx which is under account/login.aspx
and I have created a separate log.aspx which is outside the directory
Ram424 26-Sep-13 1:37am
   
FIrst confirm one thing your debug is going to if part or else part.
sudeshna from bangkok 26-Sep-13 1:38am
   
its working now.Thanks
Ram424 26-Sep-13 1:41am
   
what's the problem man
discussion with me is useful or not?
sudeshna from bangkok 26-Sep-13 2:07am
   
No,i did debug the code and its not going to else part unless i give wrong password.
Its in asp.net with vb code
sudeshna from bangkok 26-Sep-13 2:08am
   
Yes it was useful.Thanks a lot.Thats why i already gave thanks
Ram424 26-Sep-13 6:20am
   
use error messages properly to avoid these type of problems in the code or maintain Log for the application, upvote my answer
Rate this:
Please Sign up or sign in to vote.

Solution 4

This is in c# but this is how I do it. It is a static method in a class full of web based utility functions
public static void OpenNewWindow(string url)
{
      string cmd = @"<script>window.open('{0}','_blank');</script>";
      HttpContext.Current.Response.Write(string.Format(cmd,url));
}
   
Comments
Richard Deeming 26-Feb-19 10:42am
   
That is not a solution to the OP's problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100