Solved

how to set button click if id is set dynamicly

Posted on 2013-01-04
1
208 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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