首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

new的用法解决办法

2013-10-03 
new的用法本帖最后由 seandro 于 2013-10-01 00:15:47 编辑Driver的CS的代码public class Driver{public i

new的用法
本帖最后由 seandro 于 2013-10-01 00:15:47 编辑 Driver的CS的代码

public class Driver
{
    public int StrToInt32(string e)
    {
        if (!string.IsNullOrEmpty(e))
        {
            try
            {
                return Convert.ToInt32(e);
            }
            catch (Exception ex) { }
        }
        return 0;
    }
    public string GetUrlStr(string e) 
    { 
        if (!string.IsNullOrEmpty(Request.QueryString[e]))
            return Request.QueryString[e]; 
        return string.Empty; 
    }
}


web页面A:
public partial class index : System.Web.UI.Page
{
    private Driver Driver = new Driver();

    public string GetUrlStr(string e) 
    { 
        if (!string.IsNullOrEmpty(Request.QueryString[e]))
            return Request.QueryString[e]; 
        return string.Empty; 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        userid=  Driver.StrToInt32(GetUrlStr("userid")); 
    }

}


web页面B:
public partial class index : System.Web.UI.Page
{
    private Driver Driver = new Driver();

    public string GetUrlStr(string e) 
    { 
        if (!string.IsNullOrEmpty(Request.QueryString[e]))
            return Request.QueryString[e]; 
        return string.Empty; 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        userid=  Driver.StrToInt32(Driver.GetUrlStr("userid")); 
    }

}


web页面A和B只是在Driver.GetUrlStr("userid")这里,一个用的GetUrlStr("userid"),一个用的Driver.GetUrlStr("userid")
为什么用Driver.GetUrlStr("userid")的,当userid变,返回的值不变呢,都是第一次请求的值


new?class?c#?.net
[解决办法]
引用:
private static HttpRequest Request = HttpContext.Current.Request;


问题就在static了 static在整个进程内只有一份,因此,你取得的只是第一次的值。

把Request传进函数。或者在函数内部用HttpContext.Current.Request取得。

热点排行