banner



How To Create Multiple Gridview Dynamically In Asp Net C#

#1

  • New D.I.C Head

Reputation: 1

  • View blog
  • Posts: 8
  • Joined: 02-July 09

Create multiple gridviews in runtime

Posted 28 December 2010 - 05:59 AM

Hello all,

I'm trying to create multiple gridviews for a number of individuals based on a selection from a dropdownlist. The user will select his choice from the dropdownlist and from there a query is run to find the number of gridviews to create. Each gridview will also have its own data to bind. I believe I have most of it, I just can't get the gridview to show up. My thinking is to create the labels, then insert a gridview, bind it, and then repeat the process all over. Thanks in advance to anyone who can help.

Dim lblHeader As Label = New Label         Dim grdTS As New GridView         Dim strString As String = ""         Dim cmd As SqlCommand = New SqlCommand(strString)         If intCounter = 0 Then             Return         Else             lblHeader.ID = "lblName" & intCounter             lblHeader.Text = "Name: " & Names(intCounter - 1)             panelTS.Controls.Add(lblHeader)             strString = *SQL COMMAND*             cmd = New SqlCommand(strString)             panelTS.Controls.Add(New LiteralControl("<br />"))             panelTS.Controls.Add(grdTS )             Try                 cnn.Open()                 grdTS .DataSource = cmd.ExecuteReader()                 grdTS .DataBind()             Catch ex As Exception             Finally                 cnn.Close()             End Try             panelTS.Controls.Add(New LiteralControl("<br />"))             panelTS.Controls.Add(lblHeader)             panelTS.Controls.Add(New LiteralControl("<br />"))         End If            


Is This A Good Question/Topic? 1

  • +

#2 Frinavale User is offline

Reputation: 205

  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Create multiple gridviews in runtime

Posted 24 February 2011 - 08:48 AM

Do not use Labels to insert your GridViews into.

This is actually a little more complicated than you would think.
You need to create your GridViews during the Init Event of the page. These GridViews have to be declared at a page level so that their events can be handled in your server side code. If you do not create your GridViews in the Init Page-Event, and they are not declared with a page-level scope, then you will run into problems later when you want to handle things like the GridView's Sorting or Paging or Editing events.

Later in the ASP.NET life cycle (after the Init event) you will have to add them to your Page. You can either use add them directly to the Page or you can add them to a Panel on the page. You cannot add a GridView to a Label control. (That really really does not make sense...labels are used to display text...)

The complicated part about this whole process is that in the Init event none of your ASP.NET controls are instantiated yet. This means that you cannot retrieve the number of GridViews to create from a DropDownList control. To get around this issue I have used a HiddenField in the past. A HiddenField can be accessed in the Init Event (so long as you know the ClientID of that element) and you can set the value of the HiddenField using Javascript (which is executed on the change of index in the DropDownList).

To implement this application you need to have a firm understanding of the ASP.NET Life Cycle, the strangeness around Dynamic Controls in ASP.NET, how to use Javascript with ASP.NET controls....and that's just the beginning.

If at all possible, I recommend that you avoid dynamically creating GridViews since I get the impression that you are new to ASP.NET

I recommend creating the maximum number of GridViews in the Page.
Set all of the GridView.Visible properties to False by default.
Then you can set their visible properties to True when the user selects the number of GridViews.

-Frinny

How To Create Multiple Gridview Dynamically In Asp Net C#

Source: https://www.dreamincode.net/forums/topic/207307-create-multiple-gridviews-in-runtime/

Posted by: listergioncy.blogspot.com

Related Posts

0 Response to "How To Create Multiple Gridview Dynamically In Asp Net C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel