Gridview 动态 linkBut​​ton 事件未触发

发布于 2024-12-07 07:43:54 字数 3814 浏览 2 评论 0原文

我正在创建一个带有动态控件(包括项目模板)的 GridView。 这是 html 代码。

<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";
    }
}

}

这是页面代码.. ......................................

for (int i = 0; i < dtGrid.Columns.Count; i++)
        {
            string columnName = dtGrid.Columns[i].ColumnName;
            BoundField bField = new BoundField();
            TemplateField tField = new TemplateField();
            if (i == 0)
            {
                tField.HeaderTemplate = new GridViewTemplate(ListItemType.Header,    columnName);
                tField.ItemTemplate = new GridViewTemplate(ListItemType.Item, columnName);
                tField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                tField.ItemStyle.ForeColor = System.Drawing.Color.Red;
                gvItems.Columns.Add(tField);

            }
            else
            {

                bField.DataField = columnName;
                bField.HeaderText = columnName;
                bField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                gvItems.Columns.Add(bField);
            }

现在的问题是,当我单击链接按钮时,它消失了并且除了 rowdatabound 之外,不会引发任何事件。

I'm creating a GridView with dynamic controls(including itemtemplates).
Here is the html code.

<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>

Here is the class for Adding 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";
    }
}

}

Here is the page code.........................................

for (int i = 0; i < dtGrid.Columns.Count; i++)
        {
            string columnName = dtGrid.Columns[i].ColumnName;
            BoundField bField = new BoundField();
            TemplateField tField = new TemplateField();
            if (i == 0)
            {
                tField.HeaderTemplate = new GridViewTemplate(ListItemType.Header,    columnName);
                tField.ItemTemplate = new GridViewTemplate(ListItemType.Item, columnName);
                tField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                tField.ItemStyle.ForeColor = System.Drawing.Color.Red;
                gvItems.Columns.Add(tField);

            }
            else
            {

                bField.DataField = columnName;
                bField.HeaderText = columnName;
                bField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                gvItems.Columns.Add(bField);
            }

Now Problem is when i click on linkbutton it disappear and no event other than rowdatabound is raised.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-12-14 07:43:55

由于您是动态创建这些列,因此您需要在每次回发时重新创建控件。至于事件处理程序未触发,请确保在页面生命周期中尽早创建列。我建议创建动态列 OnInit,并查看是否可以解决问题。

Since you're creating these columns dynamically, you'll need to recreate the controls at every postback. As for the event handler not firing, make sure you create the columns early enough in the page lifecycle. I would suggest creating the dynamic columns OnInit, and see if that fixes the problem.

何以畏孤独 2024-12-14 07:43:55

您是否重新绑定回发数据?
请确保您的数据绑定逻辑被 if(!IsPostback) 包围。
网格的重新绑定意味着重新创建按钮,因此不会触发任何事件。

Are you re-binding the data on postbacks?
Please make sure that your data-binding logic is surrounded with if(!IsPostback).
The re-binding of the grid means re-creation of your button, and therefore no events are fired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文