Link to home
Start Free TrialLog in
Avatar of odubhgaill
odubhgaillFlag for Ireland

asked on

Dynamically creating gridviews in ASP.NET with VB

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
Avatar of cmhunty
cmhunty

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.

Avatar of odubhgaill

ASKER


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.
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?
Thanks for the information. Sounds good.

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

thanks
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.
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
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.
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

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

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

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 ?
ASKER CERTIFIED SOLUTION
Avatar of cmhunty
cmhunty

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thats great.... working now!

Thanks a lot Chris.

Robert
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........
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