|
 |
aspx thread: Dynamic Build User Control and Event Handling
Message #1 by "Eric Soonius" <E.Soonius@m...> on Wed, 12 Jun 2002 09:11:59 +0200
|
|
This is a multi-part message in MIME format.
------_=_NextPart_001_01C211E0.717B74D8
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I have build an user control (ascx with VB) that creates a table with
linkbuttons based on data from an XML file.
The table rows, cells and linkbuttons are added programmatically.
Hereunder an example of the (reduced and modified) code:
menu.ascx
...
<ASP:TABLE id=3D"Menu" runat=3D"server" />
...
menu.ascx.vb
...
Protected WithEvents Menu As Table
...
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostback() Then
Dim MenuItems As New TableRow()
'--Needed data for this example
Dim MyList As New ArrayList()
MyList.Insert(0, "Link 1")
MyList.Insert(1, "Link 2")
MyList.Insert(2, "Link 3")
MyList.Insert(3, "Link 4")
Dim myEnumerator As System.Collections.IEnumerator =3D
MyList.GetEnumerator()
Dim myCounter As Integer =3D 0
While myEnumerator.MoveNext()
Dim lb As New LinkButton()
Dim c As New TableCell()
'--The clickable linkbutton
With lb
.ID =3D "lb_" & myCounter.ToString
.Text =3D Convert.ToString(myEnumerator.Current)
End With
'-- Add the linkbutton to the table cell
With c
.Controls.Add(lb)
.Wrap =3D False
End With
'--Add the cell to the tabel row
MenuItems.Cells.Add(c)
myCounter +=3D 1
End While
Menu.Rows.Add(MenuItems)
End Sub
...
Question: How can I program the click event of the linkbuttons ?
Message #2 by "Eric Soonius" <e.soonius@m...> on Wed, 12 Jun 2002 08:35:41
|
|
[EDIT] Sorry, did not know HTML messages were not allowed [/EDIT]
I have build an user control (ascx with VB) that creates a table with
linkbuttons based on data from an XML file.
The table rows, cells and linkbuttons are added programmatically.
Hereunder an example of the (reduced and modified) code:
menu.ascx
...
<ASP:TABLE id=3D"Menu" runat=3D"server" />
...
menu.ascx.vb
...
Protected WithEvents Menu As Table
...
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostback() Then
Dim MenuItems As New TableRow()
'--Needed data for this example
Dim MyList As New ArrayList()
MyList.Insert(0, "Link 1")
MyList.Insert(1, "Link 2")
MyList.Insert(2, "Link 3")
MyList.Insert(3, "Link 4")
Dim myEnumerator As System.Collections.IEnumerator
MyList.GetEnumerator()
Dim myCounter As Integer =3D 0
While myEnumerator.MoveNext()
Dim lb As New LinkButton()
Dim c As New TableCell()
'--The clickable linkbutton
With lb
.ID =3D "lb_" & myCounter.ToString
.Text =3D Convert.ToString(myEnumerator.Current)
End With
'-- Add the linkbutton to the table cell
With c
.Controls.Add(lb)
.Wrap =3D False
End With
'--Add the cell to the tabel row
MenuItems.Cells.Add(c)
myCounter +=3D 1
End While
Menu.Rows.Add(MenuItems)
End Sub
...
Question: How can I program the click event of the linkbuttons ?
Message #3 by Neeta.Ambekar@l... on Wed, 12 Jun 2002 14:09:22 +0530
|
|
hi
attach is sample code for adding control dynamically and handling it's
event . Hope it will help you
'This program adds the server control dynamically and handles
'the events of the added control
Public Class DynamicControl
Inherits System.Web.UI.Page
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim l As New LinkButton()
Dim cn As System.Web.UI.Control
l.Text = "hello"
AddHandler l.Click, AddressOf show
Label1.Controls.Add(l)
If IsPostBack Then
If Request.Form("__EVENTTARGET") = "btn" Then
Check()
End If
End If
End Sub
Public Sub show(ByVal s As Object, ByVal e As EventArgs)
Response.Write("Dynamically Added Link Button is Clicked")
End Sub
Public Sub Check()
CreateTable()
End Sub
Public Sub clk(ByVal s As Object, ByVal e As EventArgs)
Response.Write("Dynamically Added Button is Clicked")
End Sub
Public Sub clkBtn(ByVal s As Object, ByVal e As EventArgs)
'CreateTable()
Response.Write("Dynamically Created Button in Table is Clicked")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CreateTable()
End Sub
Private Sub CreateTable()
Dim tbl As New System.Web.UI.WebControls.Table()
Dim rw As New System.Web.UI.WebControls.TableRow()
Dim cl As New System.Web.UI.WebControls.TableCell()
Dim btnCell As New System.Web.UI.HtmlControls.HtmlButton()
btnCell.InnerText = "Btn In Table"
btnCell.ID = "btn"
AddHandler btnCell.ServerClick, New EventHandler(AddressOf clkBtn)
cl.Controls.Add(btnCell)
rw.Controls.Add(cl)
tbl.Controls.Add(rw)
tbl.ID = "tbl"
Form1.Controls.AddAt(4, tbl)
End Sub
End Class
- Neeta
|
|
 |