Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamically creating gridviews in ASP.NET with VB

Posted on 2007-11-22
15
Medium Priority
?
411 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

926 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