嗨,在這裏我試圖通過在gridview中使用BoundField類後面的代碼實現 gridview中的linkButton單擊事件。 當我嘗試將單個BoundField值直接添加到網格 作爲page_Load中的一列,並綁定鏈接按鈕 RowbackBoundEvent與單擊鏈接按鈕單擊事件,它 正在觸發linkButton Click事件以下代碼。linkButton單擊GridView中的事件時通過GridView中的循環添加綁定字段時未觸發
protected void Page_Load(object sender,EventArgs e)
{
BoundField bfield = new BoundField();
bfield.HeaderText = "EmpName";
bfield.DataField = "EmpName";
gridView.Columns.Add(bfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "Monday";
bfield1.DataField = "Monday";
gridView.Columns.Add(bfield1);
}
,並在RowDataBound事件我已經寫了
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkViews = new LinkButton();
lnkViews.ID = "lnkViews";
lnkViews.Text = (e.Row.DataItem as DataRowView).Row["Monday"].ToString();
lnkViews.Click += new EventArgs(linkButton_ClickAction);
e.Row.Cells[2].Controls.Add(lnkViews);
}
}
protected void linkButton_ClickAction(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = row.Cells[0].Text; this.ModalPopupExtender1.Show();
}
但是,當我試圖通過基於 表列循環添加上述綁定列數這樣的,在Page_Load事件,該事件是不是 射擊。
protected void Page_Load(object sender,EventArgs e)
{
DataTable dtTable = new DataTable();
dtTable= BindGrid();
BoundField boundField;
for (int i = 0; i < dtTable.Columns.Count; i++)
{
string ColumnValue = dtTable.Columns[i].ColumnName;
boundField = new BoundField();
boundField.HeaderText = ColumnValue;
boundField.DataField = ColumnValue;
gridView.Columns.Add(boundField);
}
}
當我們在基於數據源列的循環使用 上面的代碼創建BoudnField事件
算,它不發射LinkButton的事件。爲什麼?
你試過行創建的事件,而不是的RowDataBound – 2013-12-17 12:18:25