如何在单个页面中使用查询字符串值? [英] how to use the query string values with in single page?
问题描述
hai,
我在同一页面中传递查询字符串值.以下是我使用的代码,但是我将对象实例引用设置为null
< pre>
if(Request.QueryString [& quot; product_type& quot;].ToString()== null)
{
if(Request.QueryString [& quot; product_type& quot;].ToString()== String.Empty)
{
this.product_type =&"Acer& quot ;;
}
}
其他
{
this.product_type = Request.QueryString [& quot; product_type& quot;].ToString();
}
DisplayContents();</pre>
hai,
i am passing the query string value in the same page..the following is the code i used but i am getting object instance reference set to null
<pre>
if (Request.QueryString["product_type"].ToString()== null)
{
if (Request.QueryString["product_type"].ToString() == String.Empty)
{
this.product_type = "Acer";
}
}
else
{
this.product_type = Request.QueryString["product_type"].ToString();
}
DisplayContents();</pre>
推荐答案
if (Request.QueryString["product_type"] != null)
{
this.product_type = Request.QueryString["product_type"];
}
else
{
this.product_type = "Acer";
}
创建一个受保护的属性(受保护,以防万一您需要从页面中引用它),并使用它来获取您的产品类型.
Create a protected property (protected, just in case you need to reference it from the page), and use that to get your product type.
protected string _productType
{
get
{
if (!String.IsNullOrEmpty(Request.QueryString["product_type"])
return Request.QueryString["product_type"]
else
return "Acer";
}
}
现在,只要在您需要的地方引用它即可.请记住,Request.QueryString返回一个字符串,因此您无需将其强制转换为字符串!
Now just reference that where ever you need it. Remember, Request.QueryString returns a string, so you dont need to cast it to a string!
这篇关于如何在单个页面中使用查询字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!