将控件添加到Gridview单元格

Sha*_*nna 3 c# asp.net gridview dynamic-controls rowdatabound

我想在某些条件下在GridView单元格上添加一个按钮.我在RowDatabound事件中执行了以下操作

if( i==0)
{
   Button btn= new Button();
   btn.Text = "view more";        
   e.Row.Cells[7].Controls.Add(btn);
}
Run Code Online (Sandbox Code Playgroud)

执行此操作时,绑定的单元格中的文本将丢失,并且仅显示按钮.我需要有按钮以及已经存在的单元格文本.

有人可以帮我这样做吗?提前致谢

Hug*_*tes 5

当您向单元格中添加控件时,它将胜过单元格中的文本,并且只想显示控件.

但是,您可以同时使用文本和按钮,同时将它们分开.为此,您需要以标签的形式添加另一个控件:

Label myLabel = new Label();
myLabel.Text = e.Row.Cells[7].Text; //might want to add a space on the end of the Text
e.Row.Cells[7].Controls.Add(myLabel);

LinkButton myLinkButton = new LinkButton();
myLinkButton.Text = "view more";
e.Row.Cells[7].Controls.Add(myLinkButton);
Run Code Online (Sandbox Code Playgroud)