Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Control to same Page Multiple times 2

Status
Not open for further replies.

ISPrincess

Programmer
Feb 22, 2002
318
US
I have a need to create datagrids (same one/same type of data/3 cols) on a page multiple times (indefinetely).

I thought I would start with a user control page with the datagrid on it and then add it to the page each time i need to, but not sure this is the way to go.

As a side-note, each grid has a link button added (such as Delete).

I have seen some threads on creating datagrids dynamically, but they are pretty basic.

Can anyone guide me in the right direction please?

Thanks

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
I am not quite sure I understand your question exactly, but you can create any control dynamically with progranatically a new command and define the loction and data binding at that time.
 
1) Create a user control with all of the required functionality
2) Use something like this (let's say the user control's path is /uc1.ascx:

Code:
using System.Web.UI;

//...

UserControl uc = (UserControl)LoadControl("/uc1.ascx");

for(int i = 0; i < 10; i++)
{
    this.Page.Controls.Add(uc);
}

This will add 10 instances of the user control "/uc1.ascx" to the base page.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
I'm sorry, I thought that since the datagrid in the usercontrol would have the same name (datagrid1) that would be a problem. Seems it is not.

From the page that I will be loading the UserControls onto, how can I bind to each template?

Normally,
Datagrid1.databind()

I must have to change the names of the datagrid as I add them, correct?


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Do all of your binding code in your user control. If you need to specify a datasource from the page that you're adding them to, do something like:

Code:
// uc1.ascx
public object DataSource
{
    get { return this.DataGrid1.DataSource; }
    set { this.DataGrid1.DataSource = value; }
}

// page.aspx
((uc1)UserContol1).DataSource = [DataSource];
((uc1)UserControl2).DataSource = [DataSource];
((uc1)UserControl3).DataSource = [DataSource];

There is, of course, error checking 'n stuff that should happen on the property setting, but that's up to you to figure out ;)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thank you for all your help, but I think I may have possibly mislead everyone in trying to use Multiple Datagrid User Controls.

Here is my problem: I will get 1 dataset from SQL as such:
Code:
Date        Wave
07/01       1
07/01       2
07/01       3
07/02       1
07/02       2
etc...

What I need to set up, dynamically is a 'grid' for each Date such as:

Code:
Date 07/01                   Date 07/02            etc....
Wave     Override?           Wave     Override?     
1        [ ]                 1        [ ]
2        [ ]                 2        [ ]  
3        [ ]

Where 07/01 is its own grid, 07/02 is its own grid, etc...

Under the Override column will be a linkbutton that will require some programmatic action.

Any thoughts on the best way to approach this?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Why not use a data list with a label and data grid inside, or a repeater control with a data grid and label.

Label Value: Date
Data Grid: Info

Get your results and place them in a dataview. Filter the results to get a distinct list of dates and place them in an array.

Then bind the array to the datalist in the item bound event place the date in the label and use the date to filter the dataview again. Bind that result to the datagrid.

I have never used the dataview filter property, but it should work. If you cannot get a distinct list using the dataview for dates, then run 2 queries against the database. 1.) for a list of dates, 2.) for the dates and details of the date.

Then bind the dates to the datalist on a click or page load event, and bind the details to the datagrid in the databind event of the datalist.

Jason Meckley
Database Analyst
WITF
 
Umm, maybe I haven't been totally clear...

> Set up one user control (/uc1.ascx)
> Put a datagrid in that user control
> Add two columns to the datagrid (One with the wave, one with the linkbutton
> Add the click event handling for the linkbutton to the datagrid
> Add a public "DataSet" property to the usercontrol
> Add a public "DataBind" method to the usercontrol that takes the "DataSource" property as the binding datasource
> Add the page to which the user control will be dynamically added
> Add a code block to dynamically add the user controls to the page as required:

Code:
for(int i = 0; i < 10; i++)
{
    // Select records for a given date
    SqlCommand _cmd = new SqlCommand([SelectByDateCommand]);
    _cmd.Parameters.Add(new SqlParameter("@date", [date]));
    SqlDataAdapter _da = new SqlDataAdapter(_cmd);
    DataSet _ds = new DataSet();

    _da.Fill(_ds);

    // Use the public datasource property and databind method
    // to bind the data in the usercontrol
    UserControl uc = (UserControl)LoadControl("/uc1.ascx");
    ((uc1)uc).DataSource = _ds.Tables[0].DefaultView;
    ((uc1)uc).DataBind();
}

This way, you can add as many of these user controls as required (you can use different logic in the "for" loop if you want). Each user control that's added to the page will be able to handle the "Override" command as needed because of it's private methods (of course, you need to add those).

Hope that makes sense, 'cuz I'm done posting to this thread :)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks Atomic Chip - as I see 'you are done posting to this thread' I suppose this is beating a dead horse, but I already had done everything you said and got the following error on the linkbutton:

Code:
Control '_ctl0_dgWaves__ctl2_Linkbutton1' of type 'LinkButton' must be placed inside a form tag with 
runat=server.

therefore, I thought I had taken a wrong turn and mislead.




PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
jmeckley,
Thanks for your suggestion, that was my first suggestion to the business group, but they did not like it.

I have come up with another easier idea for the mean-time though.

I appreciate your help!

PS - and not directed to anyone in particular....;)

I am sorry, but I must say this, although, I am not sure everyone would agree.

Answering a thread requires some level of commitment, therefore, if you do not want to commit, do not post answers.


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
How are you adding the control programmatically to the page (I forgot to put that in my last codeblock). Should be:

Code:
for(int i = 0; i < 10; i++)
{
    // Select records for a given date
    SqlCommand _cmd = new SqlCommand([SelectByDateCommand]);
    _cmd.Parameters.Add(new SqlParameter("@date", [date]));
    SqlDataAdapter _da = new SqlDataAdapter(_cmd);
    DataSet _ds = new DataSet();

    _da.Fill(_ds);

    // Use the public datasource property and databind method
    // to bind the data in the usercontrol
    UserControl uc = (UserControl)LoadControl("/uc1.ascx");
    ((uc1)uc).DataSource = _ds.Tables[0].DefaultView;
    ((uc1)uc).DataBind();
    this.Page.Controls.Add(uc); // **** Just added this
}

I know this method works. It's used pretty much throughout one of my team's projects (page layout's set by database).

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
That helped quite alot and I am afraid to ask but.....

Is there a way to specify that
Dim uc As New UserControl
is a datagrid?
as I am getting an error in code:
uc.datasource = ds


error:
'Datasource' is not a member of System.Web.Ui.UserControl



ps.
I am writing in VB.

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
did you setup your public properties to set and get the dataset on the user control? You are adding the entire user control to the web page, not just the data grid.


Jason Meckley
Database Analyst
WITF
 
I am doing it similarly to how Atomic Specified, in the main page in a loop.

There error is a VB error. I think what it is saying is that specifying a datasource for a usercontrol is not valid, that is why I ask, how can I specify that the UserControl I added is a datagrid when I either Load or add the control to the page?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
if you include this in the acsx file it should work
Code:
Public Property DataSource() as Object
  Get
    Return myDataGrid.DataSource
  End Get
  Set(ByVal Value as Object)
    myDataGrid.DataSource = Value
  End Set
End Property

Jason Meckley
Database Analyst
WITF
 
JMeckley,

I understand what you are saying... I am loading the entire usercontrol not just a grid.

And, I am sorry, I do not know how placing the get and set properties in the UserControl will help me to set the datasource in the Main Page.

If I translate Atomics code line:
((uc1)uc).DataSource = _ds.Tables[0].DefaultView;
to VB
CType(uc, uc1).DataSource = _ds.Tables(0).DefaultView

But I still get the error:


Control '_ctl0_dgWaves__ctl2_Linkbutton1' of type 'LinkButton' must be placed inside a form tag with
runat=server.


I think it is time to stop this thread so I do not waste anymore of your or Atomics time.

If anyone knows specifically what I could to to avoid the above error, please reply, otherwise thank you both for all your help.

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
this error has nothing to do with the code, the error says the object is not between the form tags, or that the form tags does not contain the runat attribute.

The link button referred to in the error, is this the delete link? I may help if we look at the aspx and ascx tags.

Jason Meckley
Database Analyst
WITF
 
That makes sense, but the form tags are in the aspx - not in ascx.

I will have other controls on the aspx so will need the form tags there.

Is there a way that I can force the control between the form tags (vague I know)?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
I would assume that if you dynamically add controls then the system would know to put them between the form tags.

Put a try/catch around the code where you add the user control.
Code:
try
  ... run loop here
catch
  'do nothing
end try
then when it loads view the source of the page, and see if the controls are present and if they are listed inside the forms tag.

You may need to put a place holder on the page and add the control at the place holder. Never used place holders before, but it may work.

Jason Meckley
Database Analyst
WITF
 
Well thank you, the placeholder worked, but....

I have negated the entire reason for trying this because it seems that I must add a placeholder for each grid... now i have to dynamically create placeholders....
maybe that is easier to do.

arrrggghhhh.

Thank you so much for your help!



PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top