我可以检查查询字符串是否存在 [英] Can I check query string is exist or not

查看:98
本文介绍了我可以检查查询字符串是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:)



当我提供带查询字符串的链接以访问其他页面时,如果使用不带查询字符串的链接调用目标页面,我将得到一个错误



因为请求的查询字符串



如果存在或不存在,我可以检查查询字符串吗?在页面加载期间根据它做一个动作?



我希望我的问题很明确:)







谢谢大家:)

解决方案

HtmlPage.Document.QueryString 将为您提供一个包含所有查询字符串值列表的字典。

如果计数为0,则没有可用的键。


< blockquote>

你可以试试这个:

 受保护  void  Page_Load( object  sender,EventArgs e)
{
如果(!IsPostBack){
if (Request.QueryString [ param]!= null
{
/ / 如果存在
Response.Write(Request.QueryString [ < span class =code-string> param
])
}
else {
// 如果不存在
}
}
}





一切顺利。

--Amit


是的,你可以。因为查询字符串名称是id,并且您期望的类型是字符串。你可以这样做





  string  s = Request.QueryString [  id]  as   string ; 

if (s!= null
{
// 你有一个名字ID的查询字符串,做点什么
}
else
{
// nope,此处没有查询字符串名称ID,执行其他操作
}


Hello : )

When I provide a link with query string to access another page with it, if the target page called using the link without query string I will get an error

because the query string requested

can I check the query string if exist or not during page load and do an action according to that ?

I hope my question is clear :)



thank you all :)

解决方案

HtmlPage.Document.QueryString will give you a dictionary with a list of all query string values.
If the count is 0, there is no key available.


Hi,
You can try this:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
        if (Request.QueryString["param"] != null)
        {
            //If it exists
            Response.Write(Request.QueryString["param"])
        }
        else{
            //If it is not existing
        }
    }
}



All the best.
--Amit


Yes you can. asuming the query string name is "id", and the type you are expecting is string. you can do this


string s = Request.QueryString["id"] as string;

if(s != null)
{
   //you have a query string with name ID, do something
}
else
{
   //nope, there is no query string names id here, do something else
}


这篇关于我可以检查查询字符串是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆