在本文中我们将看到如何使用查询字符串将值从一个页面传递到另一个页面。
我们将在SQL SERVER数据库使用UserID,FirstName,LastName,Age字段创建一个表。
create table usertable ( UserId varchar(100), FirstName varchar(50), LastName varchar(30), Age varchar(30) )
现在插入一些数据到表中:
insert into usertable values(5, 'rohatash','Kumar','23') go insert into usertable values(3, 'monu','rathor','20') go insert into usertable values(4, 'rahul','sharma','24') go insert into usertable values(8, 'ram','singh','26')
现在使用SELECT语句。
select * from usertable;
Asp.Net页面1代码:
default1.aspx.
<div>
<a href="showdetail.aspx?userid=5" title="Show records where userId is 5">Show records where userId is 5</a>
<p><asp:HyperLink ID="hyper1" runat="server" Text="Show Record where userID is 4"></asp:HyperLink> </p>
</div>
default1.aspx.cs.
Protected Sub Page_Load(ByVal sender As Object, ByVal eAs System.EventArgs)HandlesMe.Load
If Not IsPostBack Then
hyper1.NavigateUrl = "showdetail.aspx?userid=4&com=show"
End If
End Sub
现在添加新页面。
Asp.Net页面2代码:
showdetail.aspx.
<div>
<asp:DetailsView ID="DetailsView1" runat="server" EnableViewState="False"
Width="146px">
</asp:DetailsView>
</div>
showdetail.aspx.cs.
将QueryString值检索到ShowDetails.aspx的Page_load事件中并传递给GetData方法。
getData方法使用ADO.NET从数据库中检索值并填充到DetailsView。
Protected Sub Page_Load(ByVal sender As Object, ByVal eAs System.EventArgs)HandlesMe.Load
If Not IsPostBack Then
If Not String.IsNullOrWhiteSpace(Request.QueryString("userid"))Then
Dim userId As Integer = 0
Integer.TryParse(Request.QueryString("userid"), userId)
If Not userId.Equals(0) Then
GetData(userId)
End If
End If
Dim command As String = Request.QueryString("com")
End If
End Sub
Private Sub GetData(ByVal userIdAs Integer)
Dim table As New DataTable()
Dim conn As New SqlConnection("Data Source=.;uid=sa;pwd=Password;database=master")
If True Then
Dim sql As String = "SELECT userId, FirstName, LastName, Age FROM usertable WHERE userId = @userId ORDER By userId"
Using cmd As New SqlCommand(sql, conn)
Dim ad As New SqlDataAdapter(cmd)
If True Then
Dim prm As New SqlParameter("@userId",SqlDbType.Int)
prm.Value = userId
cmd.Parameters.Add(prm)
ad.Fill(table)
End If
End Using
End If
DetailsView1.DataSource = table
DetailsView1.DataBind()
End Sub
现在运行应用程序。并测试它。
现在单击default.aspx上的超链接。
这将通过查询值重定向到ShowDetails.aspx页面。
日期:2020-04-11 22:50:12 来源:oir作者:oir
