Click here to Skip to main content
15,881,381 members
Articles / Web Development / ASP.NET
Article

Dynamically load controls into a datagrid: Part 2

Rate me:
Please Sign up or sign in to vote.
3.56/5 (3 votes)
5 Oct 20042 min read 53.6K   579   25  
2 Methods for event handling within the dynamically populated datagrid

Image 1

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.
HTML
<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.

VB.NET
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:
VB.NET
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.
VB.NET
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --