asp.net 参数传递
通过后台代码设置传递的参数,然后传递给自己,总是提示未设置到对象的实例.
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string str=Request.QueryString["id"].ToString();
Response.Write(str);
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
this.LinkButton1.PostBackUrl = "~/Default.aspx?id=1";
}
请多多指教
------解决方案--------------------protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string str=Request.QueryString["id"].ToString();
Response.Write(str);
}
else
{
this.LinkButton1.PostBackUrl = "~/Default.aspx?id=1";
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
1.this.LinkButton1.PostBackUrl赋值的位置,换下看看
2.把Page.IsPostBack去了试试
------解决方案--------------------Request.QueryString["id"].ToString();
问题在这里,QueryString里没id这个参数,你又调用了tostring,当然会报错
应该改成
if(!string.IsNullOrEmpty(Request.QueryString["id"]))
{
Response.Write(Request.QueryString["id"]);
}
Request.QueryString["id"]本身数据类型已经是string了,没必要再调用tostring方法了