探讨为GRIDVIEW中的控件动态添加事件
2006-12-16 04:47
489 查看
最近开始用asp.net来开发一个网站,其中用到了gridview控件.要实现根据不同的查询方式,使gridview绑定到不同的存储过程中,能动态的改变gridview列的个数,并添加一列 ButtonField.
但是在给 ButtonField中的button动态绑定事件的时候出现问题,事件根本绑定不上.
希望各位仁兄为小弟我解决一下这个问题啊
代码如下(下面只是实现按拼音方式查询时的数据绑定)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class SearchResult : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlDataAdapter adapter = new SqlDataAdapter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet dataset = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand command = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (SqlConnection connection = new SqlConnection(constr))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.CommandText = "按拼音姓名查询";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.CommandType = CommandType.StoredProcedure;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.Connection = connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlParameter pinyin = command.Parameters.Add("@pinyinNAME", SqlDbType.NVarChar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pinyin.Direction = ParameterDirection.Input;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pinyin.Value = "ls";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.CommandText = "SELECT * From 患者信息";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.CommandType = CommandType.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.Connection = connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
connection.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
adapter.SelectCommand = command;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
adapter.Fill(dataset);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
connection.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (DataColumn col in dataset.Tables[0].Columns)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BoundField bfield = new BoundField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bfield.DataField = col.ColumnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bfield.HeaderText = col.ColumnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.Columns.Add(bfield);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Control container = new Control();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Button button = new Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//button.Text = "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//container.Controls.Add((Control)button);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//TemplateField tp = new TemplateField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tp.ItemTemplate.InstantiateIn(button);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tp.ItemTemplate.InstantiateIn(;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ButtonField HFiled = new ButtonField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.Text= "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ButtonType = ButtonType.Button;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ItemStyle.ForeColor = Color.Blue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ItemStyle.Wrap = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.HeaderText = "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GridView1.Columns.Insert(GridView1.Columns.Count, HFiled);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GridView1.DataSource = dataset.Tables[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.AutoGenerateColumns = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.AllowSorting = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.BorderWidth = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.CellSpacing = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Row.Cells[0].Visible = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Button btn = (Button)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//btn.CommandArgument = Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Click +=new EventHandler(this.Button_Click);(放在这里也加不上)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//string m=(e.Row.DataItemIndex).ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//DataBinder.Eval(e.Row.DataItem, "HFiled");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Button btn = (Button)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.CommandArgument = Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Click += new EventHandler(this.Button_Click);(这事件为什么加不上啊)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Button_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session.Add("ID", (object)((Button)sender).CommandArgument);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if ((int)Session["authentication"] == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageAdmin.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageChina.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 3)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageClinic.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 4)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageNurse.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("error.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
但是在给 ButtonField中的button动态绑定事件的时候出现问题,事件根本绑定不上.
希望各位仁兄为小弟我解决一下这个问题啊
代码如下(下面只是实现按拼音方式查询时的数据绑定)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class SearchResult : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlDataAdapter adapter = new SqlDataAdapter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet dataset = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlCommand command = new SqlCommand();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using (SqlConnection connection = new SqlConnection(constr))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.CommandText = "按拼音姓名查询";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.CommandType = CommandType.StoredProcedure;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
command.Connection = connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlParameter pinyin = command.Parameters.Add("@pinyinNAME", SqlDbType.NVarChar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pinyin.Direction = ParameterDirection.Input;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
pinyin.Value = "ls";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.CommandText = "SELECT * From 患者信息";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.CommandType = CommandType.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//command.Connection = connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
connection.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
adapter.SelectCommand = command;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
adapter.Fill(dataset);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
connection.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (DataColumn col in dataset.Tables[0].Columns)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BoundField bfield = new BoundField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bfield.DataField = col.ColumnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bfield.HeaderText = col.ColumnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.Columns.Add(bfield);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Control container = new Control();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Button button = new Button();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//button.Text = "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//container.Controls.Add((Control)button);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//TemplateField tp = new TemplateField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tp.ItemTemplate.InstantiateIn(button);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//tp.ItemTemplate.InstantiateIn(;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ButtonField HFiled = new ButtonField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.Text= "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ButtonType = ButtonType.Button;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ItemStyle.ForeColor = Color.Blue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.ItemStyle.Wrap = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HFiled.HeaderText = "查看";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GridView1.Columns.Insert(GridView1.Columns.Count, HFiled);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GridView1.DataSource = dataset.Tables[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.AutoGenerateColumns = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.AllowSorting = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.BorderWidth = 5;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.CellSpacing = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.Row.Cells[0].Visible = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Button btn = (Button)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//btn.CommandArgument = Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Click +=new EventHandler(this.Button_Click);(放在这里也加不上)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//string m=(e.Row.DataItemIndex).ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//DataBinder.Eval(e.Row.DataItem, "HFiled");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Button btn = (Button)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.CommandArgument = Convert.ToString(e.Row.Cells[0]);//this.GridView1.Rows[e.Row.RowIndex].Cells[0].ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Click += new EventHandler(this.Button_Click);(这事件为什么加不上啊)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Button_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session.Add("ID", (object)((Button)sender).CommandArgument);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if ((int)Session["authentication"] == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageAdmin.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageChina.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 3)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageClinic.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else if ((int)Session["authentication"] == 4)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("MainPageNurse.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Redirect("error.aspx");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 安卓中通过循环动态添加了一堆自定义的控件,怎么给这些控件绑定事件?
- 动态添加 button 控件及事件
- 控件的动态添加步骤以及动态事件的添加
- C#中控件动态添加事件绑定的时机详解
- Android实战简易教程-第四枪(ScrollView和HorizontalScrollView动态添加控件并提供事件监听)
- 动态添加控件,事件
- 动态添加ASP.NET控件并绑定处理事件一例
- Gridview:在进入编辑模式后动态添加控件
- GridView中动态添加模板列和其子控件集合
- JS脚本动态给元素/控件添加事件
- iOS开发学习笔记 -- (二)动态添加控件和事件处理
- Gridview:在进入编辑模式后动态添加控件
- 给gridview动态生成radiobutton添加OnCheckedChanged事件
- C#为控件动态添加事件绑定的时机
- 将 TemplateField 字段列动态添加到 GridView 控件
- vb.net2005动态添加网页控件的事件
- 在用户控件中动态添加控件及事件
- C#.NET GridView 中如何获取脚中的控件ID并添加客户端事件
- 动态添加dev CheckEdit控件,以及删除。事件处理
- C#动态生成控件以及添加事件处理