Click here to Skip to main content
Click here to Skip to main content

Dynamically load controls into a datagrid: Part 2

, 5 Oct 2004
Rate this:
Please Sign up or sign in to vote.
2 Methods for event handling within the dynamically populated datagrid

Introduction

In a previous article, "Dynamically load controls into a DataGrid", I discussed a method of dynamically creating and inserting controls into a datagrid which did not require configuration at design time. This method allowed the run-time selection of appropriate server controls based on the data content on a cell by cell basis. Additionally, a method for allowing a complete screen update (as opposed to a single line update) of the underlying data was presented.

In this article I will present two different methods for handling events raised by dynamically inserted controls. The first method uses a combination of JavaScript and Control.Attribute.add. The second method uses the Addhandler statement to assign a delegate function to an event.

Client Side event handling

Client side event handling has the advantage of not requiring a server round trip to handle the event. This is especially useful for data validation. In this example, I have inserted a simple JavaScript function into the head area of the ASPX page. This example runs a function every time a specific check box changes value.
 <HEAD>
  <title>sampleGrid</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content=http://schemas.microsoft.com/intellisense/ie5 
      name="vs_targetSchema">
  <script>
  function warning(){
  alert("Are you sure?")
  }
  </script>
 </HEAD>

Within the ItemDataBound delegate for the datagrid, I have used the Attributes.Add function to associate the OnClick event to the JavaScript function.

 Dim myCB As New CheckBox
     If Not IsPostBack Then
    If dgTable(dg1).Rows(e.Item.DataSetIndex)(1).tolower = "t" Then
    myCB.Checked = True
    Else
    myCB.Checked = False
    End If
     End If
     mycb.Attributes.Add("OnClick", "warning()")
     e.Item.Cells(1).Controls.Add(myCB)

This method is ideal for simple validation where a server round trip is not desired. But in some cases we might need to perform more complex work...

Server Side event handling

Server Side event handling allows us to perform complex instructions at the expense of a server round trip. This example performs a simple Text.ToUupper, although database lookup might be more typical. First we need to create a subroutine with the appropriate parameter template:
Private Sub MakeUpper(ByVal sender As Object, ByVal e As System.EventArgs)
    CType(sender, TextBox).Text = CType(sender, TextBox).Text.ToUpper
End Sub
Within the ItemDataBound delegate for the datagrid, I have used the Addhandler method to assign the event handling subroutine to the control event.
  If dgTable(dg1).Rows(e.Item.DataSetIndex)(2) = "uppertextbox" Then
    'create and load a textbox
    Dim myTB As New TextBox
    If Not IsPostBack Then
    myTB.Text = dgTable(dg1).Rows(e.Item.DataSetIndex)(1)
    End If
    myTB.AutoPostBack = True
        AddHandler myTB.TextChanged, AddressOf MakeUpper
    e.Item.Cells(1).Controls.Add(myTB)

Note that I have set AutoPostBack = True. This will create a postback when focus is lost. I noticed intellisense does not display the newly created control after typing "AddHandler" (my VS version, anyway). The code will still compile correctly.

Conclusions

Two methods of event handling have been presented: Client Side event handling is appropriate for local processing which does not require server resources, and Server Side event handling which may be more complex or require additional resources. Good luck and Happy Coding!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mark Killmer

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 6 Oct 2004
Article Copyright 2004 by Mark Killmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid