如何在gridview中查找控件自定义模板

如何在gridview中查找控件自定义模板

问题描述:

我在GridView中动态创建按钮,按钮点击后你想要动态删除按钮,但我的问题似乎是我找不到GrideViw中的控件。

下面是一些我的代码:



1.GridView创建按钮的Rowdatabond



public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)



{



if(e.Row.RowType == DataControlRowType.DataRow)



{



PlaceHolder ph =(PlaceHolder)e.Row.FindControl(ph1);

按钮bt1 =新按钮();



bt1.ID =GameCall;



bt1.Text =游戏电话;



bt1.CommandName =GameCall;



bt1.EnableViewState = true;



ph.Controls.Clear();


ph.Controls.Add(bt1);







}



2.执行click事件的GridView RowCommand

public void GridViewUserLots_RowCommand(object sender,GridViewCommandEventArgs e )

{

if(e.CommandName ==GameCall)

{







按钮bt1 =(按钮)e.CommandSource;

GridViewRow row =(GridViewRow)bt1.NamingContainer;

PlaceHolder ph =(PlaceHolder)row.FindControl(ph1);



TextBox PlayerID =(TextBox)row.FindControl( PlayerId);

string PlayerId = PlayerID.Text;



if(bt1!= null)

{

事件发生在这里

}



ph1.Controls.Remove(bt1);



我尝试过:



我试图使用以下代码删除按钮bt1



ph1.Controls.Remove(bt1);



但按钮仍然存在。

I dynamically created button in GridView, after the button click you want the button to be removed dynamically, but my problem seems that I could not find the control in the GrideViw.
Below is some of my codes:

1.GridView Rowdatabond that created the button

public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

PlaceHolder ph = (PlaceHolder)e.Row.FindControl("ph1");
Button bt1 = new Button();

bt1.ID = "GameCall" ;

bt1.Text = "Game Call";

bt1.CommandName = "GameCall";

bt1.EnableViewState = true;

ph.Controls.Clear();

ph.Controls.Add(bt1);



}

2.The GridView RowCommand that execute the click event
public void GridViewUserLots_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GameCall")
{



Button bt1 = (Button)e.CommandSource;
GridViewRow row = (GridViewRow)bt1.NamingContainer;
PlaceHolder ph = (PlaceHolder)row.FindControl("ph1");

TextBox PlayerID = (TextBox)row.FindControl("PlayerId");
string PlayerId = PlayerID.Text;

if (bt1 != null)
{
the event happened here
}

ph1.Controls.Remove(bt1);

What I have tried:

I tried to use the following code to remove the button bt1

ph1.Controls.Remove(bt1);

but the button still remain.

您是否尝试过调用ph1.UpdateLayout();删除后?
Have you tried calling ph1.UpdateLayout(); after removing?