注册 登录
编程论坛 ASP.NET技术论坛

未将对象引用设置到对象的实例

liaohongchu 发布于 2010-04-14 17:11, 627 次点击
cid = Int32.Parse(Request.Params["cid"].ToString());

cid 没有传参数时候 就错误.,传了cid参数 就正常了  为什么呢
我想不传CID参数和  传都可以这样的

public partial class standards_index : System.Web.UI.Page
{
    public int cid = 2;
    int teid;
    DataCommonProvider dp = DataCommonProvider.Instance();
    protected void Page_Load(object sender, EventArgs e)
    {
            cid = Int32.Parse(Request.Params["cid"].ToString());
        if (!IsPostBack)
        {
            DataBind();
            StandsDataBind();
        }
        if (cid ==2)
        {
            teid = 43;
        }
        else
        {
            teid = 44;
        }
    }
3 回复
#2
saitor2010-04-14 20:02
Params["cid"].ToString()
NULL强转string类型所以报错了
#3
猫色色2010-04-17 13:16
先判断一下取的值是否为空,然后在其他动作!
 public int cid ;
string shifouweikong;
shifouweikong = Request.QueryString["cid"];
if(shifouweikong != null)
{
cid = Int32.Parse(Request.Params["cid"].ToString());
        if (!IsPostBack)
        {
            DataBind();
            StandsDataBind();
        }
        if (cid ==2)
        {
            teid = 43;
        }
        else
        {
            teid = 44;
        }

}
#4
visolleon2010-04-25 20:27
程序代码:
protected int CID
{
    get
    {
        int temp = 0;
        if(!int.TryParse(Request.Params["cid"], out temp))
        {
            //做一些操作,提示输入的数据不正确
        }
        return temp;
    }
}

如果多次用到CID的话,建议这样:

程序代码:
private int _CID = 0;
protected int CID
{
    get
    {
        if(_CID == 0 && !int.TryParse(Request.Params["cid"], out _CID))
        {
            //做一些操作,提示输入的数据不正确
        }
        return _CID;
    }
}
1