2011-11-30 103 views
1

我的问题如下:你可以清除Page.Request.QueryString?

我正在添加一个变量到应该在页面加载时触发搜索的URL,具体取决于变量中的内容。如果您导航到没有变量的同一页面,那么在页面加载时它不应该做任何特殊的处理。我盘算了一下,下面会做的伎俩:

protected void Page_Load(object sender, EventArgs e) 
{ 

    if (Page.Request.QueryString["cell"] != null) 
    { 
     txtCell.Text = Page.Request.QueryString["cell"]; 
     Lookup_Cell(Page.Request.QueryString["cell"]); 

     //BUGGED, this keeps running when i try a new search 
     //Page.Request.QueryString["cell"] = null; 
    }else{ 
     //do nothing, empty string 
    } 

} 

这工作就像一个魅力,但我有一个应该叫你在TextBox指定单元格的Lookup_Cell方法形式的搜索按钮。我需要让Page.Request.QueryString为空,所以下次加载页面时不会触发这个特殊的OnLoad。我尝试过:

Page.Request.QueryString["cell"] = null; 

但是没有奏效。我寻找其他方法,但找不到明确的答案。

回答

3

您可以做一个简单的回发检查,当有回帖时,从您的文本框中获取字符串。

string cFinalQueryString = ""; 

if(!IsPostBack) 
{ 
    if (Page.Request.QueryString["cell"] != null) 
    { 
     cFinalQueryString = Page.Request.QueryString["cell"]; 
    }else{ 
     //do nothing, empty string 
    } 
} 
else 
{ 
    cFinalQueryString = txtCell.Text; 
} 

txtCell.Text = cFinalQueryString; 
Lookup_Cell(cFinalQueryString); 

或替代的,当你有回传,重定向到新的页面,新的“细胞”查询

if(IsPostBack && Page.Request.QueryString["cell"] != txtCell.Text) 
{ 
    Responce.Redirect("CurrentPage.aspx?cell=" + UrlEncode(txtCell.Text), true); 
    return ; 
} 
2

查询字符串由浏览器发送,每个请求都发送到该URL。

这听起来像你想重定向到一个没有查询字符串的URL。

+0

其实没关系我想我找到了一点搜索做这件事的不同方式。如果(Page.Request.QueryString [“cell”]!= null &&!Page.IsPostBack) 并且这似乎正在工作,则将页面加载的if语句更改为 。实际上并没有改变网址,但它完成了我想要的... – chilleo