2010年11月15日 星期一

Button Click 進行導向 ( Direct ) 頁面

篇教大家「如何按下按鈕後,查詢所輸入資料,並且導向到詳細資料頁面。」,查詢介面為 Default.aspx,而導向頁面為 Default2.aspx。






Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnConfirm" runat="server" Text="確定" /><br/>
<font color="red"><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></font>
</div>
</form>
</body>
</html>
Default.aspx.vb
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

Dim Name As String = txtName.Text

If String.IsNullOrEmpty(Name) <> True Then

Dim connString As String = ConfigurationManager.ConnectionStrings("dbMember").ConnectionString
Dim connection As New SqlConnection(connString)
Dim command As New SqlCommand("SELECT * FROM Member WHERE Name='" + Name + "'", connection)

connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()

If (reader.Read()) Then
Response.Redirect("Default2.aspx?ID=" + Convert.ToString(reader.GetInt32(0)))
Else
lblMessage.Text = "Not Find!"
End If
connection.Close()

End If

End Sub
End Class


而 Member 資料表內資料如下圖:
Default2.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblID" runat="server" Text="-"></asp:Label> /
<asp:Label ID="lblName" runat="server" Text="-"></asp:Label>
</div>
</form>
</body>
</html>
Default2.aspx.vb
Imports System.Data.SqlClient

Partial Class Default2
Inherits System.Web.UI.Page

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

Dim ID As String = Request.QueryString("ID")
If String.IsNullOrEmpty(ID) <> True Then

Dim connString As String = ConfigurationManager.ConnectionStrings("dbMember").ConnectionString
Dim connection As New SqlConnection(connString)
Dim command As New SqlCommand("SELECT * FROM Member WHERE MemberID=" + ID, connection)

connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()

If (reader.Read()) Then
lblID.Text = Convert.ToString(reader.GetInt32(0))
lblName.Text = Convert.ToString(reader.GetString(1))
End If
connection.Close()

End If
End Sub
End Class
執行後如下兩圖:

回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...