如果Request.QueryString不存在,则为ASP.NET Set变量

时间:2011-09-26 13:39:17

标签: c# if-statement query-string

如果Request.QueryString ['EQCN']不存在,我想将textbox.text设置为某个值。如果确实存在,我想将其设置为Requestion.QueryString ['EQCN']的值。

似乎如果该值不存在,则默认值为“”。

有什么想法吗?

非常感谢!!!

2 个答案:

答案 0 :(得分:4)

如果参数不在查询字符串中,则QueryString索引器将返回null引用,因此您可以使用??运算符作为默认值:

textbox.text = Request.QueryString['EQCN'] ?? "default text";

答案 1 :(得分:3)

textbox.text = string.IsNullOrEmpty(Request.QueryString["EQCN"]) ? "my value" : Request.QueryString["EQCN"];