Solved

Dynamically creating gridviews in ASP.NET with VB

Posted on 2007-11-22
15
402 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
 

Author Comment

by:odubhgaill
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:odubhgaill
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thats great.... working now!

Thanks a lot Chris.

Robert
0
 
LVL 11

Expert Comment

by:cmhunty
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now