Response.Write在静态方法中不起作用? [英] Response.Write is not working in static method?

查看:129
本文介绍了Response.Write在静态方法中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中实现了以下代码

I implemented the following code in asp.net

 protected void Page_Load(object sender, EventArgs e)
    {      

        TryToParse(null);
        TryToParse("160519");
        TryToParse("9432.0");
        TryToParse("16,667");
        TryToParse("   -322   ");
        TryToParse("+4302");
        TryToParse("(100);");
        TryToParse("01FA");
    }

private static void TryToParse(string value)
    {
        int number;
        bool result = Int32.TryParse(value, out number);
        if (result)
        {
           Response.Write(value+" "+number);// Intelligence window is not showing the Response property. 
        }
        else
        {
            if (value == null) value = "";
            Response.Write("Attempted conversion of '{0}' failed.", value);
        }
    }



Respo nse.Write在静态方法中不起作用..您能告诉我吗..



Response.Write is not working in static method..Can u please tell me..

推荐答案

为什么该方法标记为static?

根据您在其中执行的操作,应该只删除静态变量并将方法签名保留为private void.
Why is the method marked as static?

Based on what you are doing in it, you should just remove static and keep the method signature as private void.


方法为静态时其不起作用的原因是Response是实例对象由asp.net为每个页面创建的请参考此链接


http://www.dotnetspider.com/tutorials/AspNet-Tutorial-36.aspx [ ^ ]


默认情况下,将在所有页面中创建此类的实例,因此您可以使用该对象,而不必每次都在所有页面中再次创建.该对象的名称为Response.

在Page类中使用静态方法时,Page类将没有与之关联的实例对象,因此会出现错误..
The reason its not working when the method is static is Response is instance object created for every page by asp.net plese refer this link


http://www.dotnetspider.com/tutorials/AspNet-Tutorial-36.aspx[^]


An instance of this class is created by default in all the pages, so that you can use this object without creating again each time in all the pages. The name of this object is Response.

When you are using the static method in the Page class, Page class won''t have an instance object associted with it hence the error..


这篇关于Response.Write在静态方法中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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