您的位置:首页 > 其它

探讨为GRIDVIEW中的控件动态添加事件

2006-12-16 04:47 489 查看
最近开始用asp.net来开发一个网站,其中用到了gridview控件.要实现根据不同的查询方式,使gridview绑定到不同的存储过程中,能动态的改变gridview列的个数,并添加一列 ButtonField.
但是在给 ButtonField中的button动态绑定事件的时候出现问题,事件根本绑定不上.
希望各位仁兄为小弟我解决一下这个问题啊
代码如下(下面只是实现按拼音方式查询时的数据绑定)






public partial class SearchResult : System.Web.UI.Page




...{


protected void Page_Load(object sender, EventArgs e)




...{


if (!IsPostBack)




...{


SqlDataAdapter adapter = new SqlDataAdapter();


DataSet dataset = new DataSet();


SqlCommand command = new SqlCommand();


string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();


using (SqlConnection connection = new SqlConnection(constr))




...{


command.CommandText = "按拼音姓名查询";


command.CommandType = CommandType.StoredProcedure;


command.Connection = connection;


SqlParameter pinyin = 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 (DataColumn col in dataset.Tables[0].Columns)




...{


BoundField bfield = new BoundField();




bfield.DataField = col.ColumnName;




bfield.HeaderText = col.ColumnName;




GridView1.Columns.Add(bfield);




}


//Control container = new Control();


//Button button = new Button();


//button.Text = "查看";


//container.Controls.Add((Control)button);


//TemplateField tp = new TemplateField();


//tp.ItemTemplate.InstantiateIn(button);


//tp.ItemTemplate.InstantiateIn(;


ButtonField HFiled = new ButtonField();


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();


}


// ;




}




protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)




...{


e.Row.Cells[0].Visible = false;


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




...{


Button btn = (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 +=new EventHandler(this.Button_Click);(放在这里也加不上)


}


//string m=(e.Row.DataItemIndex).ToString();


//DataBinder.Eval(e.Row.DataItem, "HFiled");


}


protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)




...{


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




...{


Button btn = (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 += new EventHandler(this.Button_Click);(这事件为什么加不上啊)


}


}


protected void Button_Click(object sender, EventArgs e)




...{


Session.Add("ID", (object)((Button)sender).CommandArgument);


if ((int)Session["authentication"] == 1)


Response.Redirect("MainPageAdmin.aspx");


else if ((int)Session["authentication"] == 2)


Response.Redirect("MainPageChina.aspx");


else if ((int)Session["authentication"] == 3)


Response.Redirect("MainPageClinic.aspx");


else if ((int)Session["authentication"] == 4)


Response.Redirect("MainPageNurse.aspx");


else


Response.Redirect("error.aspx");




}


}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: