473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

GridView Dynamic Buttons not firing

1
Using SQL 2000 and VS2005
I have a GridView control which has 12 dynamically created buttons in the header row created as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Protected Sub gvSCTPivot_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSCTPivot.RowDataBound
  3.         'Create the 12 buttons
  4.         For Index = 1 To 12
  5.             If e.Row.RowType = DataControlRowType.Header Then
  6.                 Dim NewBtn As Button = New Button
  7.                 NewBtn.Text = GetFinMonth(Index)
  8.                 NewBtn.CausesValidation = False
  9.                 NewBtn.ID = Index
  10.                 NewBtn.Width = 35
  11.                 NewBtn.Font.Size = 11
  12.                 AddHandler NewBtn.Click, AddressOf btn_Clicked
  13.                 e.Row.Cells(Index).Controls.Add(NewBtn)
  14.  
  15.             End If
  16.         Next Index
  17.  
  18.     End Sub
  19.  
  20. Sub btn_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
  21.         Session("MonthNum") = CInt(CType(sender, Button).ID)
  22.         Session("MonthName") = GetFinMonth(Session("MonthNum"))
  23.         If Session("MonthNum") < 4 Then
  24.             Session("Year") = Session("FinYear") - 1
  25.         Else
  26.             Session("Year") = Session("FinYear")
  27.         End If
  28.         If Me.gvSCTPivot.Rows(0).Cells(Session("MonthNum")).Text.ToString = "&nbsp;" Then
  29.             Response.Redirect("SC+TAdd.aspx")
  30.         Else
  31.             Response.Redirect("SC+TEdit.aspx")
  32.         End If
  33.  
  34.     End Sub
  35.  
The GridView EnableViewState property is also set to False

One of 2 DataSource controls are bound at runtime depending on whether the underlying data exists or not. The buttons fire OK with the first DataSource control. But they do not work with the second DataSource control.

The first DataSource control code where the buttons DO work is as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT TitleId, Name,
  2. Sum(Case when FinMonth = 1 then Amount else NULL end) as Apr,
  3. Sum(Case when FinMonth = 2 then Amount else NULL end) as May,
  4. Sum(Case when FinMonth = 3 then Amount else NULL end) as Jun,
  5. Sum(Case when FinMonth = 4 then Amount else NULL end) as Jul,
  6. Sum(Case when FinMonth = 5 then Amount else NULL end) as Aug,
  7. Sum(Case when FinMonth = 6 then Amount else NULL end) as Sep,
  8. Sum(Case when FinMonth = 7 then Amount else NULL end) as Oct,
  9. Sum(Case when FinMonth = 8 then Amount else NULL end) as Nov,
  10. Sum(Case when FinMonth = 9 then Amount else NULL end) as Dec,
  11. Sum(Case when FinMonth = 10 then Amount else NULL end) as Jan,
  12. Sum(Case when FinMonth = 11 then Amount else NULL end) as Feb,
  13. Sum(Case when FinMonth = 12 then Amount else NULL end) as Mar,
  14. Sum(Amount) as Total,
  15. Round(AVG(Amount),1) as Mean
  16. from SCTPivot
  17. left join SCTTitle
  18. on SCTPivot.Title = SCTTitle.TitleId
  19. Where FinYear = @FinYear
  20. Group by TitleId, Name
  21. Order by TitleId
  22.  
The second DataSource control code where the buttons DO NOT work is as follows:
Expand|Select|Wrap|Line Numbers
  1. Select TitleId, Name,'' as Apr, '' as May, '' as Jun, '' as Jul, '' as Aug, '' as Sep, '' as Oct, '' as Nov, '' as Dec, '' as Jan, '' as Feb, '' as Mar, '' as Total, '' as Mean
  2. From SCTTitle
  3.  
  4. Union
  5.  
  6. SELECT TitleId, Name,
  7. Sum(Case when FinMonth = 1 then Amount else NULL end) as Apr,
  8. Sum(Case when FinMonth = 2 then Amount else NULL end) as May,
  9. Sum(Case when FinMonth = 3 then Amount else NULL end) as Jun,
  10. Sum(Case when FinMonth = 4 then Amount else NULL end) as Jul,
  11. Sum(Case when FinMonth = 5 then Amount else NULL end) as Aug,
  12. Sum(Case when FinMonth = 6 then Amount else NULL end) as Sep,
  13. Sum(Case when FinMonth = 7 then Amount else NULL end) as Oct,
  14. Sum(Case when FinMonth = 8 then Amount else NULL end) as Nov,
  15. Sum(Case when FinMonth = 9 then Amount else NULL end) as Dec,
  16. Sum(Case when FinMonth = 10 then Amount else NULL end) as Jan,
  17. Sum(Case when FinMonth = 11 then Amount else NULL end) as Feb,
  18. Sum(Case when FinMonth = 12 then Amount else NULL end) as Mar,
  19. Sum(Amount) as Total,
  20. Round(AVG(Amount),1) as Mean
  21. from SCTPivot
  22. left join SCTTitle
  23. on SCTPivot.Title = SCTTitle.TitleId
  24. Where FinYear = 2010
  25. Group by TitleId, Name
  26. Order by TitleId
  27.  
Both DataSource controls display data and the buttons in the GridView. One Datasource does work with the buttons and the other does not. Please can you help?

Thanks

Kind Regards

mike
Mar 15 '12 #1
1 1889
Frinavale
9,735 Expert Mod 8TB
Where are you doing your DataBind in the life cycle of the page?

Have you considered placing the buttons in the template for the GridView so that you can specify the method for the button click event instead of doing this in Code Behind?

Have you considered configuring your GridView to use the RowCommand Event to handle the button click event?

-Frinny
Mar 15 '12 #2

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

Similar topics

0
by: Dave | last post by:
Any nice examples utilizing dynamic buttons. I'd like to see a tutorial and a working example. Dave
6
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
1
by: andrei | last post by:
Hi Group, I have an aspx form with a variable number of buttons added dynamically, with id's like b01, b02, b03... Whenever I click one of these buttons, the form is posted back. I need to...
28
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...
13
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The...
4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
9
by: lilOlMe | last post by:
Hi there! I have generated a GridView that looks something like: SportName| CompanyNameX |CompanyNameY |CompanyNameZ Hockey.....| Shipping------------ |Accounting-------- |Shipping------------...
1
by: jimmysjams | last post by:
I'm modifying someone else's code and I have hit a snag. I don't want to rewrite what's already there but I might have to. I have a gridview, in the gridview is a set number of columns that...
1
by: spamguy | last post by:
I am working porting ugly .NET 1.1 DataGrids to 2.0 GridViews. The GridView fills correctly via SqlDataSource. When I click Edit -> -> Update, the page refreshes but the update command clearly never...
2
Frinavale
by: Frinavale | last post by:
I've created a ASP.NET control that displays a "book" of schedules. It dynamically displays scheduling times and allows the user to page through the schedules. It also lets the user edit the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.