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

gridview模板列的取值问题
在gridview的第6列为模板列,其中存放Button,现在想在运行时,点击模板列中的Button时,将其Text的值传给画面中的一个TextBox控件,代码如下:
protected   void   Button1_Click(object   sender,   EventArgs   e)-模板列中的button
{         //         Button   a   =   new   Button();
                a   =   (Button)(GridView1.FindControl( "Button1 "));
                TextBox2.Text   =   a.Text;

}
运行在此处出错,说什么应该New一下
---------------------
现不知道在哪个控件的什么事件中怎么写,希望大家帮帮忙,谢谢



------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)-模板列中的button
{ // Button a = new Button();
a = (Button)(GridView1.FindControl( "Button1 "));
TextBox2.Text = a.Text;

}
运行在此处出错,说什么应该New一下
这里不需要通过GridView1.FindControl( "Button1 ")) 来获得button对象,而且这样也是获得不了的,因为模板列生成的button的ID绝对不会再是Button1了
解决的办法是这样:
Button a = (Button)sender;
TextBox2.Text = a.text;
因为触发这个事件的源就是当前的Button对象
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)-模板列中的button
{ // Button a = new Button();
a = (Button)(GridView1.FindControl( "Button1 "));
TextBox2.Text = a.Text;

}

> > > >


protected void Button1_Click(object sender, EventArgs e)-模板列中的button
{
Button a = sender as Button;
TextBox2.Text = a.Text;

}