473,394 Members | 1,693 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,394 software developers and data experts.

Dynamically Created Gridview with Button in footer template

Hi

I have created a gridview dynamically from scratch and added it to my aspx page. This works fine, but i have a button in the footer of on of the columns, which when clicked on makes the gridview disappear and doesn't fire the onclick event that it should. This should obviously not be the case. My question is why does the event not fire and why does the gridview vanish?

Bear in mind i have never created aa gridview in the code behind before, so excuse me if i am being dense

Code creating columns
Expand|Select|Wrap|Line Numbers
  1.    Dim gvTownships As New GridView
  2.             'COLUMNS
  3.             Dim tmpCategory As New TemplateField
  4.             tmpCategory.ItemTemplate = New cGridViewTemplate(DataControlRowType.DataRow, "Category")
  5.             tmpCategory.HeaderTemplate = New cGridViewTemplate(DataControlRowType.Header, "Category")
  6.             tmpCategory.FooterTemplate = New cGridViewTemplate(DataControlRowType.Footer, "Calculation")
  7.    'INFORMATION
  8.             gvTownships.AutoGenerateColumns = False
  9.             gvTownships.ID = cmbChoice.SelectedItem.Text
  10.             gvTownships.ShowFooter = True
  11.             gvTownships.CssClass = "mGrid"
  12.             gvTownships.RowStyle.Wrap = False
  13.             gvTownships.HeaderStyle.CssClass = "mgrid"
  14.             gvTownships.HeaderStyle.Wrap = True
  15.             gvTownships.FooterStyle.CssClass = "mGridtf"
  16.             gvTownships.AlternatingRowStyle.CssClass = "alt"
  17.             gvTownships.GridLines = GridLines.Both
  18.  
  19.             'add columns
  20.             gvTownships.Columns.Add(tmpCategory)
  21.  
  22.   gvTownships.DataSource = dstSuburbTowhShip
  23.                 gvTownships.DataBind()
  24.  
Code to add button to footer

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
  3.           Implements ITemplate.InstantiateIn
  4.  
  5.                 Case DataControlRowType.Footer
  6.                     If columnName = "Calculation" Then
  7.                         Dim btnCalculate As New Button
  8.                         btnCalculate.Text = "Calculate"
  9.                         btnCalculate.CssClass = "button"
  10.  
  11.                         'add handler
  12.                         AddHandler btnCalculate.Click, AddressOf btnCalculate_Clicked
  13.                         container.Controls.Add(btnCalculate)
  14.  
  15.                     End If
  16.  
  17.                 Case Else
  18.  
  19.                     ' Insert code to handle unexpected values. 
  20.  
  21.             End Select
  22.  
  23.         End Sub
  24.      Private Sub btnCalculate_Clicked(ByVal sender As Object, ByVal e As EventArgs)
  25.             Try
  26.                 Dim btnCalculate As Button = CType(sender, Button)
  27.                 Dim gvRow As GridViewRow = CType(btnCalculate.NamingContainer, GridViewRow)
  28.                 btnCalculate.CommandArgument = "Calculate"
  29.             Catch ex As Exception
  30.  
  31.             End Try
  32.         End Sub
  33.     End Class
  34.  
Mar 25 '11 #1
5 5135
kadghar
1,295 Expert 1GB
When you clic the button below, the page loads again. if you dont generate the gv again, there is no reason for it to be shown.
Mar 25 '11 #2
Thanks for the prompt reply, so as i understand it the grid has to be rebuilt, thats fine, but how do i do that if the onclick event doesnt fire?
Mar 28 '11 #3
kadghar
1,295 Expert 1GB
Mmm, perhaps the problem is because the page loads first, and then try to runs an event that isn't handled anymore... I'm not sure, but that could be he problem. Working with code created controls always give trouble in aspx.

Have you tried to create it in your project, and fill it with your info at runtime? just put it into a div that runs at server, with its display set to 'none' and set it to 'block' when you want to see it (or with its visible attribute).
Mar 28 '11 #4
Rob S
14
Have you found an answer to this problem yet?
May 7 '11 #5
Marknut
42
I don’t understand what the footer button, “Calculate,” does. Does it calculate the contents of the gridivew? If so, where does the calculation go?

Without the answers to those questions, the closest fix I can come up with is to use JavaScript for the “onClick” event of the button and finish the javascript code with “ return false;” so that there’s no postback.
May 9 '11 #6

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

Similar topics

1
by: Karl Seguin | last post by:
i have a dynamically created user control which contains a non-dynamically created ASP.Net button. When the button is clicked, the event is not fired. I know that the control must be created on...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: Karthick Kumar | last post by:
Hi, I have the following code which displays all the images from a specific folder with a Radio button in it: Dim objFile i = 1 For Each objFile In objFolder.Files If (i = 1) Then...
5
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
1
by: tfsmag | last post by:
Hello, I have a function that returns a dynamically created gridview. This works fine, however it does not seem to be able to maintain state when adding sorting or paging to the gridview. Does...
2
by: showens | last post by:
I have a table that has rows appended dynamically, based on the item selected from a dropdownlist. The rows have a number of cells, one with a literal control using the text of the dropdownlist,...
2
by: ami | last post by:
In my code, I need to implement x numbers of Gridviews dynamically based on user selection. Because I am unaware of the number of gridviews, this is done after the user enters input. I need to...
2
by: gnewsgroup | last post by:
I have a GridView, in which the header of one column changes depending on the selected value of a DropDownList outside of this GridView. I did this dynamic header through protected void...
1
by: naasirsom | last post by:
Dear sir, I am using Gridview asp.net 2.0, I have a problem with Gridview Footer Template. I have put Button, Textbox and RequiredValidator in the Footer Template and Want to validate...
1
by: Sujeetharan | last post by:
I am writing a code for a search function. I need to search the folders for the particular file/files(only excel files/ there may be several files with the same name) & display them. I should also be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...

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.