odubhgaill
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
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
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?
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?
ASKER
Thanks for the information. Sounds good.
I'll let you know how it goes as I work on it.
thanks
I'll let you know how it goes as I work on it.
thanks
ASKER
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("pe rformerDis p.ascx"), Control)
Panelid.Controls.Add(myCon trol)
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.
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("pe
Panelid.Controls.Add(myCon
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
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.
ASKER
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
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>
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
<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
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
ASKER
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 ?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thats great.... working now!
Thanks a lot Chris.
Robert
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........
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........
ASKER
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
cheers
The event will then iterate through the performers and add a performer control for each performer referencing the performer id.