Wrox Home  
Search P2P Archive for: Go

  Return to Index  

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


Message #1 by david@p... on Mon, 24 Jun 2002 03:42:08
Neeta,

I'm working on my first ASP.NET project and it includes dynamic controls 
and their events.  Since I cannot get a button event to fire I hoped to 
get some ideas from you code.  Unfortunately, I got the following error 
referring to line 31...

Object reference not set to an instance of an object. 

Line 30:         AddHandler l.Click, AddressOf show
Line 31:         Label1.Controls.Add(l)
Line 32:         If IsPostBack Then
Line 33:             If Request.Form("__EVENTTARGET") = "btn" Then
 
I tried debugging it but could not.  Thank you in advance.

David

> 
> 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
> 
> 
Message #2 by david@p... on Mon, 24 Jun 2002 04:59:50
Neeta,

I got the error because I failed to add the controls to the Design.  All 
works well now in your example.  However, my problem still exists.  My 
code is similar to yours in that I use the event of a non-dynamic control 
to call a sub that creates dynamic labels, textboxes and a button and the 
button's event.  The button's event is never accessed.  Here follows code 
snippets...

Public Class TroubleTicket
    Inherits System.Web.UI.Page

    Protected WithEvents optMenu As RadioButtonList
    Protected WithEvents lstUsers As System.Web.UI.WebControls.DropDownList
    
    ' the radio button option "Add new user" calls DisplaylstUsers()
    Private Sub DisplaylstUsers()
    	'code here fills lstUser from a db
    	
    	lstUsers.AutoPostBack = True    ' Negates need for a submit button
    	AddHandler lstUsers.SelectedIndexChanged, AddressOf 
lstUsers_OnChange
    end sub

    Private Sub lstUsers_OnChange(ByVal sender As Object, ByVal e As 
EventArgs) Handles lstUsers.SelectedIndexChanged
        Select Case lstUsers.SelectedItem.Text
            Case "-Make a selection-"
                ' do nothing
            Case "Add a new user"
                Display_TextBoxes() ' Input boxes for new user info to add 
to database
            Case Else
                DisplayTickets(CInt(lstUsers.SelectedItem.Value))   ' All 
tickets for particular user
        End Select
    End Sub ' lstUsers_OnChange()
    
    ' in lstUsers I select "Add a new user" which calls Display_TextBoxes()
    Private Sub Display_TextBoxes()
        Dim btnNewUser As New System.Web.UI.HtmlControls.HtmlButton()
        With btnNewUser
            .ID = "btnNUser"
            .InnerText = "Add New User"
            .Visible = True
        End With
        AddHandler btnNewUser.ServerClick, New EventHandler(AddressOf 
dxaBtnClk)
        Me.FindControl("Form1").Controls.Add(btnNewUser)    	
    end sub 
    
    ' I click btnNewUser but never get to...
	Public Sub dxaBtnClk(ByVal sender As Object, ByVal e As EventArgs)
        Response.Write("dxaBtnClk was clicked")
    End Sub ' dxaBtnClk

> Neeta,

> I'm working on my first ASP.NET project and it includes dynamic controls 
a> nd their events.  Since I cannot get a button event to fire I hoped to 
g> et some ideas from you code.  Unfortunately, I got the following error 
r> eferring to line 31...

> Object reference not set to an instance of an object. 

> Line 30:         AddHandler l.Click, AddressOf show
L> ine 31:         Label1.Controls.Add(l)
L> ine 32:         If IsPostBack Then
L> ine 33:             If Request.Form("__EVENTTARGET") = "btn" Then
 > 
I>  tried debugging it but could not.  Thank you in advance.

> David

> > 
>>  Hi
>>        Please go through attach code . This  code will generate 
l> inkbutton,
>>  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 
c> lkBtn)
>>          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