为GRIDVIEW中的控件动态添加事件的步骤
最近开始用asp.net来开发一个网站,其中用到了gridview控件.要实现根据不同的查询方式,使gridview绑定到不同的存储过程中,能动态的改变gridview列的个数,并添加一列 ButtonField.
但是在给 ButtonField中的button动态绑定事件的时候出现问题,事件根本绑定不上.
希望各位仁兄为小弟我解决一下这个问题啊
代码如下(下面只是实现按拼音方式查询时的数据绑定)
publicpartialclassSearchResult:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
SqlDataAdapteradapter=newSqlDataAdapter();
DataSetdataset=newDataSet();
SqlCommandcommand=newSqlCommand();
stringconstr=ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
using(SqlConnectionconnection=newSqlConnection(constr))
{
command.CommandText="按拼音姓名查询";
command.CommandType=CommandType.StoredProcedure;
command.Connection=connection;
SqlParameterpinyin=command.Parameters.Add("@pinyinNAME",SqlDbType.NVarChar);
pinyin.Direction=ParameterDirection.Input;
pinyin.Value="ls";
//command.CommandText="SELECT*From患者信息";
//command.CommandType=CommandType.Text;
//command.Connection=connection;
connection.Open();
adapter.SelectCommand=command;
adapter.Fill(dataset);
connection.Close();
}
foreach(DataColumncolindataset.Tables[0].Columns)
{
BoundFieldbfield=newBoundField();
bfield.DataField=col.ColumnName;
bfield.HeaderText=col.ColumnName;
GridView1.Columns.Add(bfield);
}
//Controlcontainer=newControl();
//Buttonbutton=newButton();
//button.Text="查看";
//container.Controls.Add((Control)button);
//TemplateFieldtp=newTemplateField();
//tp.ItemTemplate.InstantiateIn(button);
//tp.ItemTemplate.InstantiateIn(;
ButtonFieldHFiled=newButtonField();
HFiled.Text="查看";
HFiled.ButtonType=ButtonType.Button;
HFiled.ItemStyle.ForeColor=Color.Blue;
HFiled.ItemStyle.Wrap=false;
HFiled.HeaderText="查看";
this.GridView1.Columns.Insert(GridView1.Columns.Count,HFiled);
this.GridView1.DataSource=dataset.Tables[0];
GridView1.AutoGenerateColumns=false;
GridView1.AllowSorting=true;
GridView1.BorderWidth=5;
GridView1.CellSpacing=2;
GridView1.DataBind();
}
//;
}
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
{
e.Row.Cells[0].Visible=false;
if(e.Row.RowType==DataControlRowType.DataRow)
{
Buttonbtn=(Button)e.Row.Cells[e.Row.Cells.Count-1].Controls[0];
//btn.CommandArgument=Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
btn.Click+=newEventHandler(this.Button_Click);(放在这里也加不上)
}
//stringm=(e.Row.DataItemIndex).ToString();
//DataBinder.Eval(e.Row.DataItem,"HFiled");
}
protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
Buttonbtn=(Button)e.Row.Cells[e.Row.Cells.Count-1].Controls[0];
btn.CommandArgument=Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
btn.Click+=newEventHandler(this.Button_Click);(这事件为什么加不上啊)
}
}
protectedvoidButton_Click(objectsender,EventArgse)
{
Session.Add("ID",(object)((Button)sender).CommandArgument);
if((int)Session["authentication"]==1)
Response.Redirect("MainPageAdmin.aspx");
elseif((int)Session["authentication"]==2)
Response.Redirect("MainPageChina.aspx");
elseif((int)Session["authentication"]==3)
Response.Redirect("MainPageClinic.aspx");
elseif((int)Session["authentication"]==4)
Response.Redirect("MainPageNurse.aspx");
else
Response.Redirect("error.aspx");
}
}
本文地址:http://www.45fan.com/a/question/69278.html