我創建了一個帶有動態控件(包括itemtemplates)的GridView。 這裏是html代碼。Gridview動態linkButton事件未被觸發
<asp:GridView ID="gvItems" runat="server"
AutoGenerateColumns="False" CellPadding="4"
GridLines="None"
Width="95%" EmptyDataText="Records Not Found!!!!"
onselectedindexchanged="gvItems_SelectedIndexChanged"
onrowdatabound="gvItems_RowDataBound" onrowcommand="gvItems_RowCommand"
onselectedindexchanging="gvItems_SelectedIndexChanging">
<RowStyle CssClass="GVRowStyle" />
<Columns>
</Columns>
<PagerStyle CssClass="gridPager" />
<SelectedRowStyle BackColor="#DCCDDA" Font-Bold="true" ForeColor="#510030" />
<HeaderStyle CssClass="Gheader" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
<EditRowStyle BackColor="#7C6F57" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
</asp:GridView>
這裏是添加ItemTemplate的類...................................
public class GridViewTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
public GridViewTemplate(ListItemType type, string colname)
{
_templateType = type;
_columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
Label lbl = new Label();
lbl.Text = _columnName;
container.Controls.Add(lbl);
break;
case ListItemType.Item:
LinkButton Lb1 = new LinkButton();
Lb1.CommandName = "Select";
Lb1.DataBinding += new EventHandler(tb1_DataBinding);
container.Controls.Add(Lb1);
break;
case ListItemType.EditItem:
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID = "Chk" + _columnName;
container.Controls.Add(chkColumn);
break;
}
}
void tb1_DataBinding(object sender, EventArgs e)
{
LinkButton LinkData = (LinkButton)sender;
GridViewRow container = (GridViewRow)LinkData.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
LinkData.Text = dataValue.ToString();
LinkData.ForeColor = System.Drawing.Color.Red;
LinkData.CommandName = "Select";
}
}
}
這裏是頁面代碼.................................. .......
現在問題是當我點擊linkbutton它消失,除了rowdatab以外沒有其他事件提高。
問題是我創建基於數據庫的gridcolumns。 –