FindControl能不能不捆绑到row事件里?

来源:百度知道 编辑:UC知道 时间:2024/05/16 13:00:39
我现在想知道用数据源绑定的一个girdview里的某个字段值
拿 Label lb1 = (Label)e.Row.Cells[2].FindControl("label1");
if (lb1 != null)
{ psid = Convert.ToInt32(lb1.Text); }
这个成功了,可是是在GridView1_RowDataBound里的

不知道怎么传递到LinkButton1_Click能继续使用???
到这里psid就没值了 成0了。。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label lb1 = (Label)e.Row.Cells[2].FindControl("label1");
if (lb1 != null)
{ psid = Convert.ToInt32(lb1.Text); }
}
在这里 psid有值`可以用

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Write(psid);
}
在这里就psid没用了。不行了。

用viewstate传啊

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label lb1 = (Label)e.Row.Cells[2].FindControl("label1");
if (lb1 != null)
{
ViewState["psid"] = Convert.ToInt32(lb1.Text);
}
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Write(ViewState["psid"].ToString());
}

如果是你自定义的Linkbutton,你对LinkButton的属性CommandName赋值:
比如你赋的是"MyCommand"
在GridView上的RowCommand事件中处理你的事件LinkButton1_Click,LinkButton1_Click就可以删除了,如:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName ="MyCommand")
{
Label lb1 = (Label)e.Row.Cells[2].FindControl("label1");

}
}

不知道怎么传递到LinkButton1_Click能继续使用???

能说具体点吗?
==========================================
不知道你的问题是不是ychs55 讲的这种,如果是