Solved

Dynamically creating gridviews in ASP.NET with VB

Posted on 2007-11-22
15
404 Views
Last Modified: 2012-05-05
Hi,

I am trying to create an .aspx page which will dynamically create gridviews. I will be passing an Event ID into the page. There will be a different number of performers for each event so I will need to query the database for this. The number of performers will correspond to the number of gridviews required on the page.

Each gridview will display the information associated with each performer. So the datasource for each gridview will be based on the performer id.

How can I dynamically create each gridview along with the corresponding datasource?

Thanks for the help!

Robert
0
Comment
Question by:odubhgaill
  • 8
  • 7
15 Comments
 
LVL 11

Expert Comment

by:cmhunty
ID: 20335417
I would suggest creating a user control for each performer. In here you would have a property which is the performer id which sets the parameter in the datasource.

The event will then iterate through the performers and add a performer control for each performer referencing the performer id.

0
 

Author Comment

by:odubhgaill
ID: 20335598

Are you refering to the page load event?

Is it best for me to store the number of performers for each event in an ArrayList and iterate through that or is that a simpler way?

Thanks. Appreciate it.
0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20337868
Yeah, in page load event, get the performers into a datatable (or data reader) using something like SELECT * FROM Performers WHERE EventID = ??

Iterate through the datatable (or data reader) and create an instance of the performer control using properties brought back from database. This would prevent a call to the database for each control. Add the performer control to a panel on the page where you want it to appear.

Let me know how it goes?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:odubhgaill
ID: 20338479
Thanks for the information. Sounds good.

I'll let you know how it goes as I work on it.

thanks
0
 

Author Comment

by:odubhgaill
ID: 20346300
Hi,
It looks like your idea will work out perfect however I'm having trouble passing parameters into the user control.

I have an .aspx page with panels that will display the user controls dynamically. The user control consists of a gridview that populates based on the performer details from the database. In the aspx.vb page I pull back the number of performers for each event so I know how many user controls I will need to display.

For each performer I need to pass the performer id to each user control so the user control has the informaiton needed to use in the datasource for the gridview. This is where I have the problem.

I iterate through the number of performers and create a new user control in the aspx.vb page :
For each performer -

Dim myControl As Control = CType(Page.LoadControl("performerDisp.ascx"), Control)
Panelid.Controls.Add(myControl)

How can I pass a parameter from the aspx.vb file to the .ascx file?  I need to able to use the parameter as the user control loads as it needs it for the datasource. I have tried to create a public variable in the .ascx file and instantiate it from the .aspx.vb file but I can't get this to work.

thanks for your help.
0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20347089
Create a propery within your performer control. You can set this when you create an instance of the control. I'm off to bed now so give it a go, if you can't get this sorted by tomorrow morning then I'll go through how to do it.

Chris
0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20347144
Sorry, just noticed, instead of loading it as type Control, load it as the the custom control type - probably called Performer or something like that. That should then give you access to the properties of the class.
0
 

Author Comment

by:odubhgaill
ID: 20347281
Thanks a lot Chris.

Yeah nearly there now. When I cast it as the user control type I can get access to the control properties.

I just can't set the property in the datasource for the gridview which I need to pass into the stored procedure.

I have attached abbreviated code for the user control. What property can I access to set the parameter which the stored proc will use?

thanks for your help


<asp:GridView ID="grdPerformers" runat="server" AutoGenerateColumns="False" DataSourceID="srcPerformers">
                <Columns>                                                                            
                        'blah
               </Columns>                
</asp:GridView>
 
<asp:SqlDataSource ID="srcPerformers" runat="server" .......... >
 
SelectCommand="prcPerformerGetByEvent" SelectCommandType="StoredProcedure">
 
<SelectParameters> 
 ' need to set these for the stored proc ??             
</SelectParameters>
</asp:SqlDataSource>

Open in new window

0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20348916
Place your performer id parameter in the
<SelectParameters>
 <asp:Parameter Name="performerID"  />          
</SelectParameters>

section as usual.

Then set the parameter in the srcPerformers.Selecting event as below where intPerformerID is set by the property

    Protected Sub srcPerformers_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sqlds.Selecting
        e.Command.Parameters("@performerID").Value = intPerformerID
    End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20348919
Sorry, amend code snippet
    Protected Sub srcPerformers_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles srcPerformers.Selecting
        e.Command.Parameters("@performerID").Value = intPerformerID
    End Sub

Open in new window

0
 

Author Comment

by:odubhgaill
ID: 20349196
Thanks.

THe selecting event works fine but I still can't set the intPerformerID property.
I havve intPerformerID declared as public variable in the .ascx.vb file.

Then when I create an instance of the user control from the aspx.vb file I try to set the intPerformerID value but this fails saying that intPerformerID is not a member of 'System.Web.UI.Control'

How should I be creating and setting the intPerformerID ?
0
 
LVL 11

Accepted Solution

by:
cmhunty earned 500 total points
ID: 20349218
Definte the property in your performer control:

    Private intPerformerID As Integer
    Public Property PerformerID() As Integer
        Get
            Return intPerformerID
        End Get
        Set(ByVal value As Integer)
            intPerformerID = value
        End Set
    End Property

Then in your parent page, you'll get an instance of the control and set the PerformerID

Dim myControl As Performer = CType(Page.LoadControl("performerDisp.ascx"), Performer)
Performer.PerformerID = 1
Panelid.Controls.Add(myControl)

This will have set intPerformerID within the control and should set the parameter as required when the control is created.
0
 

Author Comment

by:odubhgaill
ID: 20349249
Thats great.... working now!

Thanks a lot Chris.

Robert
0
 
LVL 11

Expert Comment

by:cmhunty
ID: 20349258
Good stuff. Glad it's sorted.

If the performance starts suffering with calls to the database for each control, you could try one call to get a datatable, iterate through the table setting properties for the control as you go. Just a thought........
0
 

Author Comment

by:odubhgaill
ID: 20349269
Thanks I think I will probably have to implement a solution like that as there will probably be too many calls to the db taking place.
cheers
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question