【解決方法】asp.net と vb.net を使用してあるページから別のページにリダイレクトする方法

プログラミングQA


やあ、

私はasp.netが初めてです。 したがって、すべてを段階的に学習するだけです。

ログインページを作成しましたが、ログイン時にボタンをクリックすると、ページが次のページの「パスワードの変更」ページにリダイレクトされます。

コードを書きましたが、次のページにリダイレクトされません。

ここにコードを掲載します。
私が間違っているところで、誰でも私を助けることができますか。 私はこの言語に慣れていないので、間違っているかもしれません。

あなたの助けを待っています
ありがとう

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

これは私のhtmlコードです

<%@ 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>

解決策 1

ルート ディレクトリにある場合は、Response.Redirect(“~/ChangePassword.aspx”) を使用します。 ディレクトリ内にある場合は、Response.Redirect(“~/Account/ChangePassword.aspx”) のようなディレクトリ名を使用します

解決策 2

こんにちは、バンコクのスデシュナです。

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.

コメント

タイトルとURLをコピーしました