简体   繁体   English

带有令人困惑行为的.NET方法

[英].NET method with puzzling behaviour

I have a method that works in mysterious ways. 我有一种以神秘方式起作用的方法。

Running this works fine; 运行此程序效果很好;

FindConcept("Product");

Running this produces an (unwanted) different result, even though the value of SelCatID is "Product"; 即使SelCatID的值为“ Product”,运行此命令也会产生(不需要的)不同的结果。

FindConcept(SelectedCategoryID);

Where the SelectedCategoryID looks like: SelectedCategoryID如下所示:

 protected string SelectedCategoryID
 {
     get
     {
         if (Request["c"] != null)
         {
             string c = Request["c"];
             ViewState["SelectedCategoryID"] = c;
             return c;
         }

         if (ViewState["SelectedCategoryID"] != null)
         {
             string cid = ViewState["SelectedCategoryID"] as string;
             if (!string.IsNullOrEmpty(cid))
             {
                 return cid;
             }
             return "";
         }
         else
             return "";
     }
}

This leads me to believe that there must be some weird side-effect in the get-method of the SelectedCategoryID object. 这使我相信,SelectedCategoryID对象的get方法中肯定会有一些怪异的副作用。 I am new to .NET , so I don't have a good idea of how ViewState works. 我是.NET的新手,所以我对ViewState的工作原理并不了解。

I've tried debugging this in numerous way, for example I've tried this: 我尝试了多种调试方式,例如,我尝试了以下方式:

System.Diagnostics.Debug.WriteLine(SelectedCategoryID);
FindConcept(SelectedCategoryID);

Where the debug.writeline will produce "Product", which should mean FindConcept behaves as expected. debug.writeline将在其中产生“产品”的位置,这应意味着FindConcept的行为符合预期。

This has left me completely puzzled. 这让我完全不解。 How should I continue to tackle this problem? 我应该如何继续解决这个问题?

ViewState in ASP.NET webforms is written out to the html as a hidden form field (encoded, and typically encrypted). ASP.NET Webforms中的ViewState作为隐藏的表单字段(已编码,通常是加密的)写到html中。

This means that viewstate will be available on postback requests, but not be available across an http redirect. 这意味着viewstate将对回发请求可用,但在HTTP重定向中不可用。

In the code above, it looks like the code is trying to handle both post backs and other types of requests - because it uses the indexer for the Response property which 在上面的代码中,看起来该代码正在尝试处理回发和其他类型的请求-因为它对Response属性使用了索引器,

Gets the specified object in the Cookies, Form, QueryString or ServerVariables http://msdn.microsoft.com/en-us/library/system.web.httprequest.item%28VS.71%29.aspx 获取Cookie,Form,QueryString或ServerVariables中的指定对象http://msdn.microsoft.com/zh-cn/library/system.web.httprequest.item%28VS.71%29.aspx

One likely scenario is that you are redirecting without passing along the information using one of the above methods, which means view state is not available, and the information is also not available in the url query string or cookie etc. 一种可能的情况是,使用上述方法之一进行重定向时没有传递信息,这意味着视图状态不可用,并且该信息在url查询字符串或cookie等中也不可用。

Edited - Example 编辑-示例

There are many ways this could look, here is a simple example: For example, suppose on one page you retrieve the "SelectedCategoryID" from the database in a GridView. 看起来有很多方法,下面是一个简单的示例:例如,假设您在一个页面上从GridView中的数据库中检索“ SelectedCategoryID”。 Under default scenarios, this would be stored in the Gridview of the page, and would be available in the viewstate when posting back to the same page (see http://www.xefteri.com/articles/show.cfm?id=18 and http://msdn.microsoft.com/en-us/library/ms972976.aspx ). 在默认情况下,此内容将存储在页面的Gridview中,并且在发布回同一页面时将在viewstate中可用(请参阅http://www.xefteri.com/articles/show.cfm?id=18http://msdn.microsoft.com/en-us/library/ms972976.aspx )。

However, suppose on a button click you did something in response to a button click such as 但是,假设您在单击按钮时对按钮单击做出了响应,例如

Response.Redirect("SomeOtherPage.aspx")

In this case, the ViewState on "SomeOtherPage.aspx" will not hold the value, so your getter is going to look for the value in other places, such as the url. 在这种情况下,“ SomeOtherPage.aspx”上的ViewState将不保存该值,因此您的getter将在其他位置(例如url)中查找该值。 In this case, you could pass the "SelectedCategorID" along with something such as 在这种情况下,您可以将“ SelectedCategorID”以及诸如

Response.Redirect("SomeOtherPage.aspx?c=" + HttpUtility.UrlEncode(selectedCategoryId));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM