QueryString验证问题
本文关键字:问题 验证 QueryString | 更新日期: 2023-09-27 18:05:32
每当我直接访问我的页面(mypage.aspx)时,它返回一个错误:
Object not set为Object not set为Object的实例。
如果我添加一个查询字符串(mypage.aspx?sr=true),它的工作,但我正在检查,以确保在它评估之前,它有一个值,它不应该有一个值。那么,当我直接访问页面时,为什么会出现错误呢?
if (!IsPostBack)
{
string qu1 = "";
string qu2 = "";
string qu3 = "";
if (Request.QueryString["qu1"] != null)
{
qu1 = Request.QueryString["qu1"].ToString();
if (qu1 != "")
{
qu1DropDownList.SelectedValue = industry;
}
}
if (Request.QueryString["qu2"] != null)
{
qu2 = Request.QueryString["qu2"].ToString();
if (qu2 != "")
{
qu2DropDownList.SelectedValue = category;
}
}
fillDropDownList();
if (Request.QueryString["qu3"] != null)
{
qu3 = Request.QueryString["qu3"].ToString();
if (qu3 != "")
{
qu3tDropDownList.SelectedValue = product;
}
}
}
string search = "";
string qu1value = IndustryDropDownList.SelectedValue;
string qu2value = ProductCategoryDropDownList.SelectedValue;
string qu3value = ProductDropDownList.SelectedValue;
using (SPSite site = new SPSite("SITE"))
using (SPWeb oWebsiteRoot = site.OpenWeb())
{
SPList oList = oWebsiteRoot.Lists["SpacesInfo"];
SPListItemCollection items = null;
if (Request.QueryString["sr"] != "" && Request.QueryString["sr"] != null)
{
search = Request.QueryString["sr"].ToString();
if (search == "true")
{
if (indvalue == "select" & catvalue == "select")
{
items = oList.Items;
}
else if (indvalue != "select" & catvalue != "select" & provalue != "select")
{
SPQuery query = new SPQuery();
query.Query = "MYQUERY";
items = oList.GetItems(query);
}
else if (indvalue != "select" & catvalue != "select" & provalue == "select")
{
SPQuery query = new SPQuery();
query.Query = "MYQUERY";
items = oList.GetItems(query);
}
else if (indvalue != "select" & catvalue == "select")
{
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Industry' /><Value Type='Choice'>" + indvalue +
"</Value></Eq></Where>";
items = oList.GetItems(query);
}
else if (indvalue == "select" & catvalue != "select" & provalue == "select")
{
SPQuery query = new SPQuery();
query.Query = "MYQUERY";
items = oList.GetItems(query);
}
else if (indvalue == "select" & catvalue != "select" & provalue != "select")
{
SPQuery query = new SPQuery();
query.Query = "MYQUERY";
items = oList.GetItems(query);
}
else
{
errorLabel.Text = "Please contact the administrator.";
items = oList.Items;
}
}
else
{
items = oList.Items;
}
}
DataTable table = new System.Data.DataTable();
table = items.GetDataTable();
spacerepeater.DataSource = table;
spacerepeater.DataBind();
}
改变if语句的顺序:
if (Request.QueryString["sr"] != "" && Request.QueryString["sr"] != null)
应该if (Request.QueryString["sr"] != null && Request.QueryString["sr"] != "")
问题是这样的,在你的代码中,它寻找空字符串(")和失败,因为对象是空的。在我的版本中,它将看到值为null,并且在不检查第二个if语句的情况下失败。
这通常称为短路求值。
ref http://en.wikipedia.org/wiki/Short-circuit_evaluation
使用说明:
if (!String.IsNullOrEmpty(Request.QueryString["sr"]))
{
//Do stuff
}