asp.net事件处理程序,点击链接按钮动态 [英] asp.net event handler when click on link button dynamically

查看:135
本文介绍了asp.net事件处理程序,点击链接按钮动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我刚开始编程在asp.net我挣扎动态添加事件处理程序。这里是这种情况下,当aspx页面加载是一个链接按钮上添加一个事件处理程序

hello everyone i just start programming in asp.net i struggling to add event handler dynamically. here is the scenario when the aspx page is load a link button is add with an event handler

页面加载添加链接按钮,事件处理程序链接按钮

page load add link button and event handler with link button

昏暗的产品作为新的LinkBut​​ton

Dim products As New LinkButton

products.Text =产品

products.Text = "Products"

testPanel.Controls.Add(产品)

testPanel.Controls.Add(products)

AddHandler的products.Click,AddressOf的getProducts

AddHandler products.Click, AddressOf getProducts

在数据库中的getProducts功能将循环得到的所有产品

the getProducts function will loop in the database get all the product

    Dim testDb As New Product
    Dim arr As ArrayList = testDb.DbLoop()
    Dim ObjList As ProductBo
    Dim ID As Integer
    Dim link As LinkButton



    For Each ObjtList In arr

        ID= ObjtList.C_Id
        link = New LinkButton
        testPanel.Controls.Add(New LiteralControl("<br />"))
        link.ID = ID
        link.Text = ObjList.Name
        link.CommandArgument = CustInt
        Me.testPanel.Controls.Add(link)
        AddHandler link.Click, AddressOf getProductsDetails
    Next ObjList

下面是循环就会显示数据库中的产品列表,我想要做的添加的每个产品名称的链接,当我在一个产品应该调用另一个事件处理程序后单击我的问题,我尝试这一个,但它不工作。

HERE IS MY problem after looping it will display a list of product in the database, what i want to do add a link of each product name when i click on one product it should invoke another event handler i try this one but it not working.

AddHandler的link.Click,AddressOf getProductsDetails

AddHandler link.Click, AddressOf getProductsDetails

感谢您

推荐答案

不要忘了,你必须重新创建回发所有动态控件

Don't forget you must re-create all dynamic controls on postback

就是一个类记得,它是每个请求实例化一次,如果它不重新创建这些控件以及相关的处理上回发请求,则你不会得到任何事情发生。

Your Page is just a class remember and it is instantiated once per request, if it doesn't recreate these controls as well as the associated handlers on the postback request then you won't get anything happen..

您需要将之前的的Page_Load 重新创建这些控件,可以在 Page_Init 做到这一点,或覆盖的CreateChildControls 方法。

You need to recreate these controls prior to Page_Load, you can do this in Page_Init or override the CreateChildControls method.

这篇关于asp.net事件处理程序,点击链接按钮动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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