Solved

how to set button click if id is set dynamicly

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now