Divs on an ASP.NET Gridview - c#

The GridView in ASP.NET when rendered isn't the prettiest or most semantic control ever, is there a way to use <div> using constructing it? Or should there be another approach, I ideally would like to remain using the gridview because I select the DataKeyNames in my code, unless there is a similar way to select the DataKeyNames using another control like a listview?

+1 to Lareau. ListView has a DataKeyNames property as well. I would use Ingrid (jQuery) along with a ListView control to make it better.
ListView samples:
http://weblogs.asp.net/scottgu/archive/2008/01/04/jan-4th-links-asp-net-asp-net-ajax-asp-net-mvc-visual-studio-iis7.aspx
http://basgun.wordpress.com/2007/12/29/listview-control-in-aspnet-35-3/
https://web.archive.org/web/20211020153238/https://www.4guysfromrolla.com/articles/122607-1.aspx
https://web.archive.org/web/20210125144848/http://www.4guysfromrolla.com/articles/021308-1.aspx
Download and demo Ingrid:
http://www.reconstrukt.com/ingrid/

You can use repeater instead

I would go with a listView. Similar to a repeater but just a bit better.

The GridView is tabular data. It uses a table to construct it. It's perfectly semantic. I agree it isn't pretty, but a <div> construction of tabular data is just as ugly. The GridView has the greatest functionality for this kind of data display/management.

You might be able to customize the rendering by overriding the Render method of GridView control. This would mean that you are changing the default behaviour of GridView and might need to handle additional events if you decide to do change defaults.

Instead of using the semantics of a div, I would look to format the class using some tailored CSS. Let me know if this is something you're interested in and I can offer you some links.

Related

ASP.net ListView with groups

I can't seem to find a good answer for this. I'm trying to set up a webpage to grab data from a database and display it something like:
username1
image1.jpg
imagename
image2.jpg
imagename2
username2
image3.jpg
imagename3
username3
image4.jpg
imagename4
image5.jpg
imagename5
image6.jpg
imagename6
There is a username, and with that username there is one or more images and image names. Not all the usernames will have the same number of images.
My question is, what is the best way to do this? I am able to accomplish this using a repeater control with a nested repeater. It's messy but works.
From what I've read, a ListView should be able to do the same thing but much cleaner. My problem with a ListView is that I have to set the GroupItemCount, it's not dynamic like i need. I think I can nest another ListView, but it turns out to be as much code as using a repeater.
Is one of these methods (repeater or listview) preferred over the other. Or is there a better way to do this that i'm not thinking of? I don't think that what i'm trying to do is out of the ordinary, so I think there would be a quicker way. To me, it seems much easier to do this in classic asp using for loops.
Thanks in advance for any input.
The GroupTemplate is instantiated when a certain number (GroupItemCount) of ItemTemplate are instantiated. The ItemTemplate is called for each object in the collection and you first have a collection of users. Even if you can change the GroupItemCount during the ItemDataBound event, this will reflect how the next group of users is created. There is nothing about images until now, so I can say that you can't use ListView for a such task.
Using a repeater inside the ItemTemplate of the main Repeater (or ListView if you want) is the best choice, also common, so you don't have to worry about the mess, another developer will easily see your intention.

Gridview clean up and neatness asp.net

So my question is this, I have a gridview that filters data from a database when a search keyword is entered. The problem is, that I need it to show some 20 different fields, which is significantly too long for a web page. The gridview goes beyond my asp.net webpage width and off of it. I was wondering if there was any way I could make it neater and easier to read, or fit all the data fields on one page. I'm not too familiar with this, so excuse my lack of know-how in and thank you in advanced.
A few suggestions:
Show fewer fields.
Make your columns really narrow.
Make your grid a header row only (contains only minimal info for a record). Clicking a link in a row will display further details for that item.
Use a DataList, Repeater or ListView so you can layout the row yourself.
You can try something like this:
http://weblogs.asp.net/dwahlin/archive/2007/07/31/freeze-asp-net-gridview-headers-by-creating-client-side-extenders.aspx
I think this thread on another site addresses exactly what you're asking:
http://forums.asp.net/t/1277793.aspx/1

In asp.net , what is the difference between gridview and repeater controls

I have worked with both the controls for simple application development . Both almost do the same functionality. What is the difference between them ?
There is a table here that compares the data bound server controls:
http://weblogs.asp.net/anasghanem/archive/2008/09/06/comparing-listview-with-gridview-datalist-and-repeater.aspx
GridView always renders as a grid. Repeater allows you to create your own template layout. For instance, you can put things in divs. GridView also has a much more rich object model, while repeater is rather simplistic.
Yes, you can achieve similar results with both, but they are nowhere near the same thing.
Both these controls are Data-Bound Web Server control.
GridView : It displays data as a table and has ability to preform sort, paging,edit and delete a record.
Repeater : has fewer templates then GridView. It renders a read-only list from the datasource.

advice on how to create dynamic controls

My web page will get a set of results from the database and display it to the user. However I am not sure about "number" of results.
Each result will have a panel which contains several controls in itself, an image, several labels, etc.
What is the best way to do this dynamically, eg. create these controls dynamically?
Is it better to use an AJAX control? Should I use Gridview?
Thanks for the help,
Behrouz
You need to give us more details about how each result will look like.
I would being by looking at a repeater control. You don't need to know the number of results that get passed to it. You'll be able to specify a template for how each result will look like and the repeater control will take care of rendering one for each result.
A repeater is probably the better option for that scenario.
Add all the controls you're likely to need and switch them on and off as needed via the item databound event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
If the template for each item is the same, use a Repeater. Bind via AJAX if you'd rather bind via web services and save on server-side performance. Then you have to manage everything on the client, which can be tedious, but very performant. Use JQuery to make it easier.
Server-side adding of controls can be a pain, but doable.
HTH.

Dragging & dropping items from one list to another in a ASP.NET page?

I would like to move items from one list to another on a page, and I'm pretty flexible about what type of a list it is. What's the best way to do that? ASP.NET Ajax? jQuery? Anything else?
There's a nice tutorial on CodeProject that covers dragging with ASP.NET and jQuery:
http://www.codeproject.com/KB/webforms/JQueryPersistantDragDrop.aspx
if you want to do this and PostBack instead of using AJAX to update your data based on from fields you'll need to get creative about what types of controls you use. Page validation will complain about ASP controls like dropdownlists, listboxes, etc. if they contain selected items that weren't in the list when it was rendered.
Better to use AJAX to send back your updates or use HTML controls like unordered lists or select added via javascript and stuff the selected data in another control that doesn't complain (hiddenfield) on PostBack. The alternative is turning off page validation and I don't think that's a good idea.
You can also might look at YUI Library, I'd say it implements Drag & Drop in a very simple and flexible way:
http://yuilibrary.com/yui/docs/dd/
There are a lot of examples etc...

Categories

Resources