无法触发asp.net中动态添加的Linkbutton点击事件? [英] Not able to fire Linkbutton click event added dynamically in asp.net?

查看:193
本文介绍了无法触发asp.net中动态添加的Linkbutton点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将字段列绑定到gridview并在运行时添加了linkbutton.当我单击链接按钮时发生页面加载,但未调用Linkbutton事件.
以下是我的代码示例.

I have bound fields columns to gridview and added linkbutton at runtime.When i click to linkbutton Page load happen but Linkbutton event not invoked.
Below are my code sample.

<asp:GridView ID="StoresGridView"    OnDataBound="StoresGridView_DataBound" 

          Runat="server"          

          AutoGenerateColumns="False" DataKeyNames="Store" 

          BorderWidth="1px" BackColor="LightGoldenrodYellow" 

          GridLines="None" CellPadding="2" BorderColor="Tan" 

          ForeColor="Black">
            <footerstyle backcolor="Tan"></footerstyle>
            <pagerstyle forecolor="DarkSlateBlue">
              HorizontalAlign="Center" BackColor="PaleGoldenrod">
            </pagerstyle>
            <HeaderStyle Font-Bold="True" 

              BackColor="Tan"></HeaderStyle>
            <alternatingrowstyle>
              BackColor="PaleGoldenrod"></alternatingrowstyle>
            <columns>         
                <asp:BoundField HeaderText="Store" 

                  InsertVisible="False" DataField="Store"

                    SortExpression="Store">
                    <itemstyle horizontalalign="Center"></itemstyle>
</columns>
            <SelectedRowStyle ForeColor="GhostWhite" 

                BackColor="DarkSlateBlue"></SelectedRowStyle>



在我的代码下方-



Below my Code-

protected override void OnInit(EventArgs e) 
        { 
            base.OnInit(e);
            AddLinkButton();
        }
        protected void StoresGridView_DataBound(object sender, EventArgs e)
        {
            AddLinkButton();
        }
        /// <summary> 
        /// Add a LinkButton To GridView Row. 
        /// </summary> 
        private void AddLinkButton()
        {
            foreach (GridViewRow row in StoresGridView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {                   
                    LinkButton lb = new LinkButton();
                    lb.ID = "lnk";
                    lb.Text=row.Cells[0].Text; 
                    lb.CommandName = "StoreDetail";
                    lb.Command += lnk_Command;
                    row.Cells[0].Controls.Add(lb);
                }
            }
        }


在这里,我希望应该激活linkbutton


Here i am expecting linkbutton should invoked

protected void lnk_Command(object sender, CommandEventArgs e)
 {
            if (e.CommandName == "StoreDetail")
            {
                //This is to test  
                Response.Write("You Press Link Button!");
            }
        }


有人知道吗?或任何关于此的建议.

[Aman.A]请始终格式化您的代码段[/Aman.A]


Is anyone Know about this ? or any suggestion on the same.

[Aman.A] Please always format your code snippets [/Aman.A]

推荐答案

很简单.在Page_Init上添加动态控件.之后,事件将不起作用.
阅读此内容:
http://msdn.microsoft.com/en-us/library/aa479330.aspx [ ^ ]

"
It''s simple. Add Your dynamic controls on Page_Init. After that Events just won''t work.
Read this:
http://msdn.microsoft.com/en-us/library/aa479330.aspx[^]

"
报价:

在使用动态控件时使用的模式如下:

在初始化阶段,我将动态控件添加到控件层次结构中并设置ID属性
在Load阶段,我在If Not Page.IsPostback条件语句中为动态控件分配任何所需的初始值.

The pattern I use when working with dynamic controls is as follows:

In the Initialization stage I add the dynamic controls to the control hierarchy and set the ID property
In the Load stage, I assign any needed initial values to the dynamic controls within an If Not Page.IsPostback conditional statement.


这篇关于无法触发asp.net中动态添加的Linkbutton点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆