将 URL 绑定到网格视图



我想将url绑定到GridView但我不知道怎么做。

例如,当我在url中键入http://localhost:12345/example.aspx?FirstName=John时,它会在GridView中给我结果,该结果仅以名字"John"显示。

这是我的当前代码:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable
' Stablish ODBC Connection
Dim con As New OdbcConnection("DRIVER={SQL Server};Server=WJNJPHR8TCX8PSQLEXPRESS;Database=Fabrics;Integrated Security=True;")
' Query Command
Dim cmd As New OdbcCommand("SELECT * FROM [Client] WHERE [FirstName] = ?", con)
con.Open()
' Gets the path (Example.aspx)
Dim path As String = HttpContext.Current.Request.Url.AbsolutePath
' Gets the host (localhost)
Dim host As String = HttpContext.Current.Request.Url.Host
' Gets the whole url (localhost:24124/Example.aspx)
Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
' Parse the query string variables into a NameValueCollection
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(url)
' Iterate through the collection and shows the result in MsgBox
Dim sb As New StringBuilder()
For Each s As String In qscoll.AllKeys
sb.Append(s & " = " & qscoll(s) & vbCrLf)
Next s
MsgBox(sb.ToString)
' Gets all keys and values in query string and shows it on MsgBox
For Each key As String In HttpContext.Current.Request.QueryString.AllKeys
MsgBox("Key: " + key + "  Value: " + Request.QueryString(key))
Next key
Dim FName As String = Request.QueryString("FirstName")
Dim par1 As New OdbcParameter
par1.OdbcType = OdbcType.NVarChar
par1.Value = FName
par1.ParameterName = "@FirtName"
cmd.Parameters.Add(par1)

'Shows the result in Data Grid
dt.Load(cmd.ExecuteReader()) '==> Error: Invalid use of default parameter
GridView1.DataSource = dt
GridView1.DataBind()
End Sub

任何帮助都可以!

您可以使用

Dim firstName as string = Request.QueryString("firstName")

然后,在执行之前,必须将其作为查询的参数传递

Dim par1 As New OdbcParameter
par1.DbType = DbType.String
par1.Value = firstName
par1.ParameterName = "@FirstName"
cmd.Parameters(1) = par1

希望对你有帮助

回答了我的问题!

这是我的代码:

For Each key As String In HttpContext.Current.Request.QueryString.AllKeys
Dim FName As String = Request.QueryString("FirstName")
Dim par1 As New OdbcParameter With {
.OdbcType = OdbcType.NVarChar,
.Value = FName,
.ParameterName = "@FirstName"
}
cmd.Parameters.Add(par1)
MsgBox("Key: " + key + "  Value: " + Request.QueryString(key))
dt.Load(cmd.ExecuteReader())
GridView1.DataSource = dt
GridView1.DataBind()
Next key

@isol 感谢您的帮助! :)

最新更新