日期:2014-05-18  浏览次数:20535 次

关于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);
}
}