如何使用asp.net和vb.net从一个页面重定向到另一个页面 [英] How to redirect from one page to another using asp.net and vb.net

查看:110
本文介绍了如何使用asp.net和vb.net从一个页面重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是asp.net的新手。所以一步一步地学习所有内容。



我创建了一个登录页面,现在按钮点击登录,我希望页面将其重定向到下一页更改密码页面。



我已经写了代码,但它没有重定向到下一页。



我会在这里发布代码。

任何人都可以帮助我,我错了。我可能错了,因为我是这门语言的新手。



等待你的帮助

谢谢

< pre lang =text> Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data


公共类Login1
继承系统.Web.UI.Page

受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)Handles Me.Load


End Sub

受保护的子Button1_Click(发送者作为对象,e作为EventArgs)处理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)
如果dt.Rows.Count> 0然后
Response.Redirect(ChangePassword.aspx)
Else
ClientScript.RegisterStartupScript(Page。[GetType](),validation,< script language ='javascript' >提醒('无效的用户名和密码')< / script>)
结束如果

结束次级


结束类





这是我的HTML代码

<%@ Page Language = vbAutoEventWireup =falseCodeBehind =Login.aspx.vbInherits =WebApplication2.Login1%> 

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://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
{
宽度:55%;
身高:69px;
margin-top:33px;
}
.style4
{
宽度:152px;
}
.style5
{
宽度:190px;
}
< / style>
< / head>
< body style =height:226px>
< form id =form1runat =server>
< div class =style1>


< span class =style2>登录页面< br />
< / span>


< / div>
< table class =style3>
< tr>
< td class =style4>
用户名< / td>
< td class =style5>
< asp:TextBox ID =TextBox1runat =serverWidth =180px>< / asp:TextBox>
< / td>
< td>
< asp:RequiredFieldValidator ID =RequiredFieldValidator1runat =server
ControlToValidate =TextBox1ErrorMessage =您必须输入用户名.. !!!
ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =style4>
密码< / td>
< td class =style5>
< asp:TextBox ID =TextBox2runat =serverTextMode =PasswordWidth =180px>< / asp:TextBox>
< / td>
< td>
< asp:RequiredFieldValidator ID =RequiredFieldValidator2runat =server
ControlToValidate =TextBox2ErrorMessage =你必须输入密码...... !!
ForeColor =Red>< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td class =style4>
< / td>
< td class =style5>
< asp:Button ID =Button1runat =serverBackColor =BlueForeColor =White
Text =LoginWidth =100px/>
< / td>
< td>
< / td>
< / tr>
< tr>
< td class =style4>
< / td>
< td class =style5>
< / td>
< td>
< asp:HyperLink ID =HyperLink1runat =server
NavigateUrl =〜/ Account / Register.aspx>新用户注册在这里< / asp:HyperLink>
< / td>
< / tr>
< / table>
< / form>
< / body>
< / html>

解决方案

使用Response.Redirect(〜/ ChangePassword.aspx),如果是在根目录中。如果它在任何目录中,那么使用目录名称,如Response.Redirect(〜/ Account / ChangePassword.aspx)


来自曼谷的sudeshna,



如果要重定向到同一网站中的任何页面,我们可以使用两种方式。 

1. Response.Redirect(这是目的地页面的完整路径);
2. Server.Transfer(这是目标页面的完整路径);

Response.Redirect Server.Transfer 之间存在细微差别。


这是在c#中,但我就是这样做的。它是一个充满基于Web的实用函数的类中的静态方法

 public static void OpenNewWindow(string url)
{
string cmd = @ <脚本> window.open( '{0}', '_空白');和LT; /脚本>中;
HttpContext.Current.Response.Write(string.Format(cmd,url));
}


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>

解决方案

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")


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.


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


这篇关于如何使用asp.net和vb.net从一个页面重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆