How do you create an ASP.Net custom row thingey?

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
My website currently has more bugs than the congo... but we're getting somewhere.

The next obstacle is:
I need to create a custom item almost like a row for a table, but I've been warned against using html tables. The "row" must have different types of columns, depending on the page, but generally it will be:
a date - in a small font
an image/header - big and colorful font
a description box - multiline and a std font
and a download/view hyperlink. - STD hyperlink.

There rows will then be dynamically created for each item in a folder, so there will be a few. But I'll get to that automation a bit later.

I'm using a css stylesheet and Visual Studio 2008 C#, and I have no clue how to do this one.
Any pointers?
 

simp

Senior Member
Joined
May 6, 2008
Messages
718
I don't know why you have been "warned against using html tables", but then I'm not privy to any background.

Your requirement vague, but I'd suggest looking at programatically adding controls to a control's control collection.

IOW, place a container on your page (like a Table for example), programatically create the elements you are going to need (like a TableRow with TableCells, and your data could be Labels, Images or whatever inside the TableCell), and add those to the container's control collection, all from code behind.

Also, Google.....
 

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
I don't know why you have been "warned against using html tables", but then I'm not privy to any background.

Your requirement vague, but I'd suggest looking at programatically adding controls to a control's control collection.

IOW, place a container on your page (like a Table for example), programatically create the elements you are going to need (like a TableRow with TableCells, and your data could be Labels, Images or whatever inside the TableCell), and add those to the container's control collection, all from code behind.

Also, Google.....

I know that it sounds vague. :eek: It's hard to explain the image that I have in my head in words. But I catch what you're saying. Let me give it a whirl, and see what I can come up with when creating my own custom control.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
I hate using those standard controls. I prefer writing my own markup and writing it back to a placeholder, if I have to do it that way. My preferred way is actually serializing whatever data I'm getting to a form of XML and transforming the XML with XSL. That way you can make changes to the layout "on the fly" without having to recompile anything, because no server-side controls are being created.
 

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
nothing wrong with tables if you use it for what its supposed to be used for.
are you always going to use either a date, image, header, description box or and hyperlink ?

why not create a user control which can be set on what you need to display ? (not really sure what you really want to achieve... me running low on coffee it has gone empty :eek:)
 

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
Right. Here's what I did last night:

-Create new custom control
-Added it's own style sheet
-Added 2 labels, an image button, and a text box to it
-coded some get/set operators so that you can set the text fields.
-Added it to my gallery sheet
-And it's there. But the Get/Set part isn't working. :(

If I say that DataText="Hello" in the gallery sheet, then nothing happens.
Did I miss something?

Secondly, I'm using a PhotoShow gallery on one of my other sheets. It works fine on a normal sheet, but I can't get it to work within a ContentPlaceHolder from the master sheet. And I have no idea where to start looking.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
I think you'll have to modify the properties manually in the ItemDataBound even of the grid... That way you can dynamically add new controls or change the properties of the controls already contained in the grid.

Just a question: Why use a grid and not manually add the custom controls to a placeholder? You have a lot more control by adding them manually in a placeholder... The grid adds extra client-side and server-side overheads to the page that you can avoid, especially in this instance in which you only want to display data and not interact with it.
 

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
Where do you get the notion that I'm using a grid? ;)

Currently the code to create the row on the sheet is something like this:

<p>...some text...</p
<uc1:galleryRow ID="UserControl1" runat="server" RowDate="20/09/1988" RowHeader="Sheep" />
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Oh, hehe... I just thought, seeing as the thread started out discussing datagrids, that you were using the grid to "repeat" your user control. So how are you repeating the rows? A simple example would be to implement something like this:

Code:
foreach (DataRow dr in ds.Tables[0].Rows)
{
    galleryRow gr = (galleryRow)Page.LoadControl("galleryRow.ascx");
    gr.RowDate = dr["COLUMN_NAME_FOR_ROW_DATE"].ToString();
    gr.RowHeader = dr["COLUMN_NAME_FOR_ROW_HEADER"].ToString();
    plcRows.Controls.Add(gr);
}

Assumptions:
- You have a PlaceHolder on your page called "plcRows".
- Your user control is located in the same folder structure as the page.
- Your data is returned through a DataSet, called dsResults, with a single DataTable.

Obviously, it would be a lot better if you have proper objects to represent the "rows", so then you could easily discard the DataSet and DataRow object types and use your proprietary objects instead (which is my preferred method of doing things ;)).
 

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
Erm... (feels like a 5 year old in a plane's cockpit)
Can I stick you for lunch sometime over a weekend and bring my laptop? It's getting apparent that I need to be shown a few things. Somewhere I'm missing something really basic.

No, I don't have a placeholder. If VS2008 didn't create it automatically, the odds are that it's not there.
Yes, the control is embedded in the same folder.
No, the dataset part is reserved for version 2. For now, I just want whatever I have to work and up and running.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Erm... (feels like a 5 year old in a plane's cockpit)
Can I stick you for lunch sometime over a weekend and bring my laptop? It's getting apparent that I need to be shown a few things. Somewhere I'm missing something really basic.

No, I don't have a placeholder. If VS2008 didn't create it automatically, the odds are that it's not there.
Yes, the control is embedded in the same folder.
No, the dataset part is reserved for version 2. For now, I just want whatever I have to work and up and running.
I think the problem here is that you don't understand the Document Object Model of the page too well... I'd recommend reading up on that a bit to get an understanding of how a page is constructed and how the elements in a page relate to one another. Also, remember that you can always dynamically add new controls to a page on server-side. It is entirely possible to build a complete, functional page on server-side on Page Load, without touching the page's design or HTML views. That is imperative for a data-driven website. if you just focus on dragging and dropping from the VS toolbox then you will quickly run into an impassable wall.

...but the lunch would be nice! M&B usually works nicely for these types of meetings and I'm a regular at my local M&B. :p
 

foozball3000

Executive Member
Joined
Oct 28, 2008
Messages
5,828
Super. It will only be around the end of the month though.

I'll then make the most of this opportunity to try and do it your way. Luckily we have google. :) But I need the site up and running quite fast seeing as it doubles as my portfolio.
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
But I need the site up and running quite fast seeing as it doubles as my portfolio.

Quality over quantity man....

FarligOpptreden said:
Not "my way", it's the "proper way"... :p

That's the spirit :p If I had R1 for every line of code I've rewritten by living by that philosophy, I'd be driving a much nicer car :D
 
Top