如何在gridview中查找控件自定义模板 [英] How to find control custom template in gridview

查看:68
本文介绍了如何在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?


这篇关于如何在gridview中查找控件自定义模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆