当前位置: 代码迷 >> ASP.NET >> 关于GridView控件的有关问题
  详细解决方案

关于GridView控件的有关问题

热度:492   发布时间:2013-02-25 00:00:00.0
关于GridView控件的问题!
请问如何在页面加载的时候为GridView的模板列动态绑定一个LinkButton控件。注:不是直接拖控件到模板列内。

------解决方案--------------------------------------------------------
在RowDataBound事件里可以做。
------解决方案--------------------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Cells[0];//第1列不太明白楼主究竟要得到什么,
//查找控件
//LinkButton lb = (LinkButton)e.Row.Cells[0].FindControl("控件ID");
}
}
------解决方案--------------------------------------------------------
RowDataBound是绑定事件,每帮顶一行,触发一次
------解决方案--------------------------------------------------------
这就需要用到GrivView的RowDataBound事件了: 
C# code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {     if (e.Row.RowType == DataControlRowType.DataRow)    {            LinkButton lb=e.Row.Cells.FindControl("LinkButton1") as LinkButton;        Label label=e.Row.Cells.FindControl("Label1") as Label;        if(你的逻辑判断)         {             lb.Visible=true;            label.Visible=false;        }         else         {             lb.Visible=false;            label.Visible=true;        }    } }
------解决方案--------------------------------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton linkb = new LinkButton();
linkb.ID = "LinkButton1";
linkb.Text = "haha";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[CellIndex].Controls.Add(linkb);
}
}
------解决方案--------------------------------------------------------
顶6楼
------解决方案--------------------------------------------------------
探讨
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton linkb = new LinkButton();
linkb.ID = "LinkButton1";
linkb.Text = "haha";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[CellIndex].Controls.Add(linkb);
}
}
  相关解决方案