Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Dynamically created controls which can respond to client events?


Message #1 by "Richard McKinley" <r.mckinley@k...> on Mon, 17 Dec 2001 18:07:55
Hi,



I need to be able to dynamically create a series of buttons at run-time 

(preferrably within a custom ASP.NET server control), and setup an 

eventhandler to respond to any one of these buttons being clicked.  These 

buttons will actually represent diary text entries which have been loaded 

from a database, and clicking on an entry will allow the user to edit it. 



I have been experimenting using the ASP:Literal control, which allows you 

to dynamically alter HTML code from the server side.  But, my problem is 

after I have created a dynamic link (button) to a diary entry eg:



.ASPX file:

<asp:Literal Id="TestLiteral" RunAt="server" />



.cs (code-behind file):

public Literal   TestLiteral    



TestLiteral.Text ="<asp:Button Id='TestEntry1' Text ='12pm Team Meeting' 

Runat='Server' OnClick='HandleEdit'/>"



public void HandleEdit( Object Source, EventArgs E )

{

  /* process edit */

}



that the Click event does not fire as I can't dynamically create a server 

side variable to represent the button.  Can anyone point me in the right 

direction to creating some controls dynamically, which can respond to 

events from the client.  



Many thanks,

Richard.
Message #2 by Neeta.Ambekar@l... on Wed, 19 Dec 2001 15:56:34 +0530

Hi

      Please go through attach code . This  code will generate linkbutton,

command button control dynamically , also handles click event of that

control.

      Let me know if you need further elobration .Run this code & see the

o/p .



Regards,

Neeta



'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






  Return to Index