Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to set button click if id is set dynamicly

Posted on 2013-01-04
1
Medium Priority
?
232 Views
Last Modified: 2013-01-22
Hello,

I have set a list of link buttons dynamically (shown below)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtholidays As New DataTable()
        dtholidays = dateallreadybooked.getusers()
        Dim myhols As DataView
        myhols = New DataView(dtholidays, "", "", DataViewRowState.CurrentRows)

        For Each Dayitis In myhols

            Dim linkbutton As New LinkButton
            linkbutton.Text = Dayitis.Item("FirstName") & " " & Dayitis.Item("Surename")
            linkbutton.ID = Dayitis.Item("Surename")
            Dim newline As String = Environment.NewLine
            holder.Controls.Add(linkbutton)
            Dim spacer As New System.Web.UI.WebControls.Label
            spacer.Text = "<br />"
            holder.Controls.Add(spacer)
        Next
    End Sub

Open in new window


how could i set the onlcick method so its basiclylike the below


  Protected Sub Dayitis.Item("FirstName")_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dayitis.Item("FirstName").Click

    End Sub

Open in new window

0
Comment
Question by:runnerjp2005
1 Comment
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 2000 total points
ID: 38744373
Try this,
Imports System.Data

Partial Class dynamicControl
    Inherits System.Web.UI.Page
    Private Shared Function GetTable() As DataTable
        Dim table As New DataTable()
        table.Columns.Add("id", GetType(Integer))
        table.Columns.Add("name", GetType(String))

        table.Rows.Add(25, "Indocin")
        table.Rows.Add(50, "Enebrel")
        table.Rows.Add(10, "Hydralazine")
        table.Rows.Add(21, "Combivent")
        table.Rows.Add(100, "Dilantin")
        Return table
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtholidays As New DataTable()
        dtholidays = GetTable()
        Dim myhols As DataView
        myhols = New DataView(dtholidays, "", "", DataViewRowState.CurrentRows)
        For Each Dayitis In myhols
            Dim linkbutton As New LinkButton
            linkbutton.Text = Dayitis.Item("name") & " " & Dayitis.Item("id")
            linkbutton.CommandName = Dayitis.Item("name")
            linkbutton.ID = Dayitis.Item("name") & Dayitis.Item("id")
            AddHandler linkbutton.Click, AddressOf dynamic_Link_Click
            Dim newline As String = Environment.NewLine
            form1.Controls.Add(linkbutton)
            Dim spacer As New System.Web.UI.WebControls.Label
            spacer.Text = "<br />"
            form1.Controls.Add(spacer)
        Next
    End Sub

    Protected Sub dynamic_Link_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkBtn As LinkButton = TryCast(sender, LinkButton)
        Response.Write("You have clicked " & lnkBtn.ID & " Button")
    End Sub
End Class

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Screencast - Getting to Know the Pipeline
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question