Adding click event to linkbutton

Adding click event to linkbutton

am 28.12.2007 12:16:18 von Fabri

Hi,

I am new to .NET and need help with adding click event on LinkButton
programatically.

Please see the code below. I would like to add a click event on LinkButton
returned from "Function EditLink". This LinkButton should fire "Sub EditNav"
when clicked.
======================================

Function GetNavTable(ByVal NavType As Integer) As Table
Dim NavTable As New Table
NavTable.CellSpacing = 1
NavTable.CssClass = "contenttable"
NavTable.Width = Unit.Percentage(100)

Dim DataTable As AppDataSet.NavigationDataTable =
BLL.GetNavigationByType(NavType)
Dim RSRow As AppDataSet.NavigationRow

For Each RSRow In DataTable
Dim Row As New TableRow
Dim Header As New TableCell
Dim LinkText As New TableCell
Dim LinkUrl As New TableCell
Dim Sorting As New TableCell
Dim EditCell As New TableCell
Dim EditLink As New LinkButton
EditLink = GetEditLink(RSRow.NavId)
Header.Text = RSRow.NavId
LinkText.Text = RSRow.LinkText
LinkUrl.Text = RSRow.LinkUrl
Sorting.Text = RSRow.Sorting
EditCell.Controls.AddAt(0, EditLink)
Row.Cells.Add(Header)
Row.Cells.Add(LinkText)
Row.Cells.Add(LinkUrl)
Row.Cells.Add(Sorting)
Row.Cells.Add(EditCell)
NavTable.Rows.Add(Row)
Next
Return NavTable
End Function

Function GetEditLink(ByVal NavID As Integer) As LinkButton
Dim EditLink As New LinkButton
'How to make this LinkButton fire the "Sub EditNav" and pass
parameter NavID?
Return EditLink
End Function

Sub EditNav(ByVal NavIID As Integer)
'Code to execute when EditLink is clicked on.
End Sub

=======================================
Any help will be appriciated.

Tnx.

Re: Adding click event to linkbutton

am 28.12.2007 15:42:59 von Fabri

After searching through the net and pulling my hair for a couple of hours, i
have now figured out how to do this. :-)

In case anyone is interested in knowing, here is how we add an event handler
on a dynamic button/linkbutton:

Public Shadows Function GetEditLink(ByVal NavID As Integer) As
LinkButton
Dim EditUrl As New LinkButton
EditUrl.CommandName = "EditCommand"
EditUrl.CommandArgument = NavID.ToString()
AddHandler EditUrl.Command, AddressOf EditNav
EditUrl.Text = "Edit"
Return EditUrl
End Function

Sub EditNav(ByVal sender As Object, ByVal e As CommandEventArgs)
If e.CommandName.CompareTo("EditCommand") = "0" Then
Label1.Text = "Arg: " & e.CommandArgument.ToString()
End If
End Sub


"Jack" wrote in message
news:uOXzTMUSIHA.4584@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> I am new to .NET and need help with adding click event on LinkButton
> programatically.
>
> Please see the code below. I would like to add a click event on LinkButton
> returned from "Function EditLink". This LinkButton should fire "Sub
> EditNav" when clicked.
> ======================================
>
> Function GetNavTable(ByVal NavType As Integer) As Table
> Dim NavTable As New Table
> NavTable.CellSpacing = 1
> NavTable.CssClass = "contenttable"
> NavTable.Width = Unit.Percentage(100)
>
> Dim DataTable As AppDataSet.NavigationDataTable =
> BLL.GetNavigationByType(NavType)
> Dim RSRow As AppDataSet.NavigationRow
>
> For Each RSRow In DataTable
> Dim Row As New TableRow
> Dim Header As New TableCell
> Dim LinkText As New TableCell
> Dim LinkUrl As New TableCell
> Dim Sorting As New TableCell
> Dim EditCell As New TableCell
> Dim EditLink As New LinkButton
> EditLink = GetEditLink(RSRow.NavId)
> Header.Text = RSRow.NavId
> LinkText.Text = RSRow.LinkText
> LinkUrl.Text = RSRow.LinkUrl
> Sorting.Text = RSRow.Sorting
> EditCell.Controls.AddAt(0, EditLink)
> Row.Cells.Add(Header)
> Row.Cells.Add(LinkText)
> Row.Cells.Add(LinkUrl)
> Row.Cells.Add(Sorting)
> Row.Cells.Add(EditCell)
> NavTable.Rows.Add(Row)
> Next
> Return NavTable
> End Function
>
> Function GetEditLink(ByVal NavID As Integer) As LinkButton
> Dim EditLink As New LinkButton
> 'How to make this LinkButton fire the "Sub EditNav" and pass
> parameter NavID?
> Return EditLink
> End Function
>
> Sub EditNav(ByVal NavIID As Integer)
> 'Code to execute when EditLink is clicked on.
> End Sub
>
> =======================================
> Any help will be appriciated.
>
> Tnx.
>