473,670 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic GridView Template for Header that fires Sorting Event

Frinavale
9,735 Recognized Expert Moderator Expert
I have implemented the ITemplate interface to dynamically "display" complicated data in a GridView.

For the life of me I cannot implement the template in such a way that when it is applied to Header Data it is displayed as a link that raises a Sorting Event upon postback.

In my InstantiateIn method I create a LinkButton for my Header. I have added a handler to the LinkButton that calls a method upon DataBinding. In that method I have attempted everything I could possibly think of to get the LinkButton to cause a Sorting Event upon postback.

Has anyone done this?

I have a work around (that doesn't use a template for the Header) so there's is no urgency to this. I'm just dying to know how to get this to work.

Thanks

-Frinny
Jan 27 '09 #1
3 13995
Frinavale
9,735 Recognized Expert Moderator Expert
I went back to researching how to wire up dynamically created links/buttons in a template to click (command) events because I was never able to get this to work in the past.

The first thing to note is that you Must create your columns in your Page Init Event.

If you do not do this, the events for the controls created dynamically in your template will be lost. They will post back to the server, but then the event cease to exist.

Another thing to note is that events are handled by the controls that they occur in. This means that you have to implement code that handles the event in your ITemplate class. If you want this to be handled in the Page that the template belongs to you will have to Bubble Up the event.

One last thing to note is that Click Events do not automatically bubble up for LinkButtons, Buttons and ImageButtons. Only the Command Event will bubble up.

The following is an example of some code that creates a template and bubbles up the Command Event of the link buttons in the template to the Page that the template belongs to:

Expand|Select|Wrap|Line Numbers
  1. Public Partial Class Practice
  2.     Inherits System.Web.UI.Page
  3.     Private _theSource As List(Of SomeClass)
  4.  
  5.     Private Sub Practice_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  6.         _theSource = New List(Of SomeClass)
  7.         _theSource.Add(New SomeClass("first", "data 1"))
  8.         _theSource.Add(New SomeClass("second", "data 2"))
  9.         _theSource.Add(New SomeClass("third", "data 3"))
  10.         _theSource.Add(New SomeClass("fourth", "data 4"))
  11.         Session("_theSource") = _theSource
  12.  
  13. 'Please note that we are creating the GridView's Columns in the Page Init event.
  14. 'This is important because if you do not create the columns at this stage in the
  15. 'page life cycle the event for the dynamic link buttons will be lost!
  16.  
  17.         Dim tf As New TemplateField
  18.         tf.HeaderText = "My Stuff"
  19.  
  20. 'My Template is a Private Class that implements the ITemplate Field
  21. 'See it's definition farther down in the code.
  22.         Dim item As New MyTemplate()
  23.  
  24. 'Assigning the function named "MyCommandHandlingFunction" to handle the templates LinkButtonCommand Event 
  25.         AddHandler item.LinkButtonCommand, New CommandEventHandler(AddressOf MyCommandHandlingFunction)
  26.  
  27. 'I was using the following in an attempt to bubble up the Click event for the 
  28. 'Dynamic Link Button but this will never happen because I discovered that 
  29. 'Only the Command Event will bubble up.
  30. '        AddHandler item.LinkButtonClick, New EventHandler(AddressOf MyCommandHandlingFunction)
  31.  
  32.  
  33. 'Adding the item to the template field
  34.         tf.ItemTemplate = item
  35.  
  36. 'Creating the GridView's columns 
  37.         myGrid.Columns.Add(tf)
  38.  
  39.     End Sub
  40.  
  41. 'This method simply binds the data source to the data grid
  42.     Private Sub DisplayGridView()
  43.         If _theSource Is Nothing Then
  44.             _theSource = Session("_theSource")
  45.         End If
  46.         myGrid.DataSource = _theSource
  47.         myGrid.DataBind()
  48.     End Sub
  49.  
  50.     Private Sub Practice_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  51.         DisplayGridView()
  52.     End Sub
  53.  
  54. 'This method handles the Command Event that originates from the LinkButton
  55. 'In the Template
  56.     Private Sub MyCommandHandlingFunction(ByVal sender As Object, ByVal e As CommandEventArgs)
  57.         'e.CommandArgument contains "Sort"...we set this in the Template
  58.     End Sub
  59.  
  60. '    Private Sub MyClickHandlingFunction(ByVal sender As Object, ByVal e As EventArgs)
  61. '
  62. '    End Sub
  63.  
  64.  
  65.     Private Class MyTemplate
  66.         Implements ITemplate
  67.         'Expose the command event handler - when the command event handler is trapped internally this event is raised to inform the container
  68.         Public Event LinkButtonCommand As CommandEventHandler
  69.         'Public Event LinkButtonClick As EventHandler
  70.  
  71.         Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
  72.             Dim text As New Label
  73.             text.Style.Add("display", "block")
  74.             text.Style.Add("width", "80px")
  75.             text.Style.Add("float", "left")
  76.  
  77.             AddHandler text.DataBinding, AddressOf Me.BindText
  78.             container.Controls.Add(text)
  79.  
  80.             Dim lnkBtn As New LinkButton
  81.             lnkBtn.Style.Add("float", "left")
  82.             AddHandler lnkBtn.DataBinding, AddressOf Me.BindLinkButtons
  83.             'AddHandler lnkBtn.Click, New EventHandler(AddressOf Click)
  84.             AddHandler lnkBtn.Command, New CommandEventHandler(AddressOf Command)
  85.             container.Controls.Add(lnkBtn)
  86.         End Sub
  87.  
  88.         Private Sub BindLinkButtons(ByVal sender As Object, ByVal e As EventArgs)
  89.             Dim lnkBtn As LinkButton = CType(sender, LinkButton)
  90.             Dim container As GridViewRow = CType(lnkBtn.NamingContainer, GridViewRow)
  91.             Dim cellContent As String = DataBinder.Eval(container.DataItem, "Content").ToString
  92.             lnkBtn.Text = cellContent
  93.             lnkBtn.CommandArgument = "Sort"
  94.  
  95.         End Sub
  96.         Private Sub BindText(ByVal sender As Object, ByVal e As EventArgs)
  97.             Dim txt As Label = CType(sender, Label)
  98.             Dim row As GridViewRow = CType(txt.NamingContainer, GridViewRow)
  99.             Dim cellContent As String = DataBinder.Eval(row.DataItem, "Title").ToString
  100.             txt.Text = cellContent + ": "
  101.         End Sub
  102.  
  103. ''This will crash because the only event that's bubbled up is the Command Event....which takes a parameter of CommandEventArgs
  104.         'Private Sub Click(ByVal sender As Object, ByVal e As EventArgs)
  105.         '    RaiseEvent LinkButtonClick(Me, e)
  106.         'End Sub
  107.  
  108. 'Handles the Command Event: 
  109. 'When the link button is clicked the Command event is bubbled up to the container, which happens to be this class.
  110. 'This method bubbles the event up to the next level so that the event can be
  111. 'handled in the Page.
  112.         Private Sub Command(ByVal sender As Object, ByVal e As CommandEventArgs)
  113.             RaiseEvent LinkButtonCommand(Me, e)
  114.         End Sub
  115.     End Class
  116.  
  117.  
  118.  
  119. 'This class is used for the DataSource of the GridView
  120.     Private Class SomeClass
  121.         Private _title As String
  122.         Private _content As String
  123.         Public Property Title() As String
  124.             Get
  125.                 Return _title
  126.             End Get
  127.             Set(ByVal value As String)
  128.                 _title = value
  129.             End Set
  130.         End Property
  131.         Public Property Content() As String
  132.             Get
  133.                 Return _content
  134.             End Get
  135.             Set(ByVal value As String)
  136.                 _content = value
  137.             End Set
  138.         End Property
  139.  
  140.         Public Sub New(ByVal theTitle As String, ByVal theContent As String)
  141.             _title = theTitle
  142.             _content = theContent
  143.         End Sub
  144.     End Class
  145.  
  146.  
  147. End Class

The ASPX code for this page is:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Practice.aspx.vb" Inherits="ScratchPad.Practice" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:GridView ID="myGrid" runat="server"
  13.                                   BorderColor="black"
  14.                                   GridLines="Horizontal"
  15.                                   BorderStyle="None"
  16.                                   allowpaging="True"
  17.                                   autogeneratecolumns="False"
  18.                                   style="margin-left:auto; margin-right:auto;"
  19.                                   width="200" EnableViewState="true"></asp:GridView>
  20.     </div>
  21.     </form>
  22. </body>
  23. </html>
Jan 28 '09 #2
Jack Hack
6 New Member
Frinny,

I've been grappling with this problem for several days before locating your informative post. Not only did you provide a working example but your explanation of the problem allowed me to understand why my original attempts to handle such events were not working. Priceless.

I made one small deviation, I found that I had to add the gridview_RowCom mand event handler to process the commandName and commandArgument (and subsequent logic) passed by my link button.

My solution is within a visual web part but I'm not sure if this would account for the change.

Thanks again,

Jack
Feb 17 '11 #3
Frinavale
9,735 Recognized Expert Moderator Expert
I'm not sure why you had to make that change but I'm glad that my post helped you. I know I spent a lot of time trying to figure this stuff out :)
Feb 17 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13741
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
28
10258
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when appropriate based on your method names . the GridView has OnRowCommand="GridView1_RowCommand" in the aspx. my problem is that the RowCommand event is firing twice (95% of the time) on the page. the other 5% it only fires once. there's no
1
9350
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
9
24784
by: J055 | last post by:
Hi I have a very simple configuration of the GridView with paging and sorting. When I do a postback the DataBinding event fires twice - in both the ProcessPostData and PreRender stages of the page life cycle. In this example the event fires twice when a) GridView EnableViewState=False and any image type control in the <columns/> element. When either EnableViewState is set to true or the image button is removed, the event fires once....
0
1651
by: Rolf Barbakken | last post by:
I have a most annoying problem: I have a gridview with users from AD. When the user clicks on the header row a postback is triggered, and the _Sorting-event fires. This is fine, and my SortGrid-sub is called from there. But the annoying part is that right after running through SortGrid the _Sorting-event fires again. SortGrid sorts ASC/DESC alternating, and because it fires twice every time, the gridview is obviously sorted the same...
6
28147
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the RowCommand event is not firing! and neither is the RowDeleting (the button's commandName is...
3
22750
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work. I turned on the Allow Sorting property but when I click one of the columns that is bound, it will not sort. Below is the code I am using
9
11894
by: lilOlMe | last post by:
Hi there! I have generated a GridView that looks something like: SportName| CompanyNameX |CompanyNameY |CompanyNameZ Hockey.....| Shipping------------ |Accounting-------- |Shipping------------ BaseBall...| Receiving-----------|Shipping------------|Accounting--------- Etc............| Accounting----------|Receiving---------- |Receiving----------- Where there are an unknown number of Company Names and an unknown number of Sport...
11
6060
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.