0

我试图从请求页面的查询字符串中获取参数“id”,如下所示

If Request.QueryString IsNot Nothing AndAlso _
            Request.QueryString.GetKey("id") IsNot Nothing Then
            DeleteVehicle(Request.QueryString.GetKey("id"))
 End If

但我得到这个错误

System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 16:         If Request.QueryString IsNot Nothing AndAlso _
Line 17:             Request.QueryString.GetKey("id") IsNot Nothing Then

Source File: G:\projects_backup\Toaab\Toaa\admin\vehicle\view.aspx.vb    Line: 16 

请你帮帮我

编辑

我在 page_load 事件中调用它

通过超链接调用同一页面(具有自动生成的链接)

我也将代码更改为

If Request.QueryString("id") IsNot Nothing OrElse Request.QueryString("id") IsNot String.Empty Then
 DeleteVehicle(Request.QueryString("id").ToString)
End If
4

3 回答 3

4

要查看 QueryString 中是否存在值,请检查该值是否等于空字符串而不是 null:

试试这个:

If String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id"))
End If
于 2012-04-16T15:52:52.267 回答
1
  1. 没有在函数C#/VB.Net中提供字符串参数的重载。GetKey
  2. 如果您检查 的所有事件 Page Life Cycle,则Request.QueryString值永远不会为空(此外,它包含一些非空值)。
  3. 尝试更改您的代码,如下所示。

If Request.QueryString("id") IsNot Nothing 
                 AndAlso String.IsNullOrEmpty(Request.QueryString("id")) = False Then
    DeleteVehicle(Request.QueryString("id").ToString)
End If
于 2012-04-16T15:59:52.217 回答
0

我建议使用 Params 集合。它适用于 Querystring 以及基于表单的参数。您可以像这样对“id”进行简单的测试。

Request.Params.AllKeys.Contains("id")
于 2019-02-18T23:33:36.890 回答