Nesting a GridView within Repeater - asp.net

I have a scenario wherein, for example, I need to repeat a list of US states and display a table of cities and city populations after the name of each state. The design requirement dictates that every outer repetition must be the name of a state followed by a table of cities, and that requirement cannot be changed at this time. Are there disadvantages to nesting a GridView within a Repeater and then binding each repeated GridView during the Repeater's ItemDataBound event? What are some alternative solutions?

If it were me, I'd reverse the question and ask why I should use a GridView, If you need a bunch of built-in features like paging and sorting, then the GridView might be a good fit. If you just want tabular data, I'd reconsider. Why? Because with GridView you're getting a whole bunch of stuff you won't use, your ViewState will be potentially huge, and your page performance will be slower.
I'm not a bigot when it comes to GridView, but I only use them when there is a damn good reason.

In your above scenario, you'd be better off doing a master-detail style GridView, which will save you the overhead of all those GridView objects that get created.
There are various implementation of it (using a drop down for the master, using a modal popup for the detail, etc.), but the main point is that there are implementations available.

At the very least, hopefully you can turn off ViewState on the GridViews.

The best solution I was able to come up with was to nest the GridView in the Repeater. Then I bound each repeated GridView during the Repeater's ItemDataBound event. I turned off their ViewStates, of course, as they weren't required.

Related

Automatic data binding mysteries

I have some questions about how and when data sources are bound in the page lifecycle, and I can't seem to find the answers anywhere.
I have a gridview which is bound to a data source at design time. One of the parameters for the DS is the selected value of a dropdown list. These dropdown lists are also databound to retreive their options, and the dropdowns have their auto-postback property set true. Now some things are confusing me about this.
Most of the time changing the value of the dropdown will reload the gridview with the new parameter, and it's not necessary to manually call databind() on the gridview in order to do so. However there are situations where the gridview is not rebound, and I'm not sure what these conditions are. When will a data-source be rebound automatically upon parameter change, and when do you need to call databind manually from code?
Does calling databind from codebehind prevent the automatic databind event from firing, or will they both fire, resulting in a wasteful extra query of the data source?
If the former, is there a way to stop the extra databind from occurring without having to move everything into code behind and lose the convenience of setting up data sources for the controls in the design view?
Are the answers to these questions documented on any official sources? (MSDN, etc)
I can't be sure without looking at you code, but you probably want to read about ViewState.
Once the control has been loaded, the data stays in the viewstate. You generally want to do the DataBinding on (!IsPostBack) event so that on postbacks you do not need to rebind the data from the DB.

What is advantage of repeater control over grid view in asp.net C#?

I have read many articles regarding difference between gridview and repeater. I come to know that gridview pattern is fixed in and , where as repeater can provide customized HTML mark up. If I am not wrong, we can also customize HTML mark up by adding template field and placing table with customized design. In that tables we can place labels and other .net control and can get whatever we want. Then why to use repeater control.
I am confused in which scenario it can be preferred over gridview.
In simple words we can say performance of repeater is far better than gridview. If you need basic rendering for read only items then its better to use repeater and if you need events , pagination and editable controls then you should go for gridview. Simpler controls with less in built functionality are speedy. you can do implement all functionalities of grid view to repeater but you have to do it manually.
So it depends upon you requirements either you need repeater or gridview
This discussion will be helpfull for you
http://forums.asp.net/t/1072020.aspx

GridView Sorting without losing user modifications

I have a bit of a unique problem. I am using a GridView to display information from a database. The requirements for this GridView are that it is always editable so I have TextBoxes in the ItemTemplates and want to save the values when a button is clicked.
That all works great.
The problem is I am trying to get sorting (and eventually paging) working and hitting problems. If I bind the GridView from an ObjectDataSource this obviously doesn't work because it rebinds the datasource before it sorts and I lose any modified values. If I bind the GridView from a DataTable and try to store it in a Session variable it still doesn't work because any modified values aren't in the session variable.
I'm really just looking for suggestions on what I could possibly do to sort (and page later) without losing values the user has modified. I realize most people don't modify the Gridview all at once so its a weird problem.
You could implement client-side sorting of the GridView which would have the added benefit of making your UI feel faster. There is a blog post on sorting with jQuery you could try, though I have not personally used the method suggested.
Alternatively, you could send the values back to the server via AJAX while editing and save them (temporarily) in the session, the database, or where it makes the post sense in your scenario. You would obviously need to retrieve them when sorting.
One final option would be to save your values on the postback caused by the sort command. Mostly you would do something along the lines of handling the OnTextChanged event of the TextBoxes and (as above) saving the values to a temporary store. You would not want these TextBoxes to autopostback - they would simply wait for a postback such as the one caused by your sorting event. In this scenario you could also use the temporary store you populate during the save event to avoid duplicating the effort.

"Action" column in a DataReader

I have an "action" column in my repeater which shows actions a user can select for an item. The column contains ASP.NET HyperLink or LinkButton controls. Some actions are based on whether a user is in a role, which I determine programatically. I'm struggling with the best way to dynamically generate this column when I populate the repeater. Now I am assigning in-line code to the Visible property of each control, but I feel that is sloppy and not very straight forward. Would I be better served using a PlaceHolder control and populating that at runtime? What kind of methods do other people use for situations such as this?
The "normal" way to apply any sort of dynamic rendering to a Template based control such as the Repeater is to handle the ItemCreated or ItemDataBound events.
In your particular case, you could check appropriate conditions within that event handler and toggle the visibility of the relevant "Action" column.
Also, see this question where Ian Quigley posted a code snippet that should serve as a good example for you. It may also help to read my own answer which shows how to use visibility toggling in inline code.

Repeater, ListView, DataList, DataGrid, GridView ... Which to choose?

So many different controls to choose from! What are best practices for determining which control to use for displaying data in ASP.NET?
It's really about what you trying to achieve
Gridview - Limited in design, works like an html table. More in built functionality like edit/update, page, sort. Lots of overhead.
DataGrid - Old version of the Gridview. A gridview is a super datagrid.
Datalist - more customisable version of the Gridview. Also has some overhead. More manual work as you have to design it yourself.
ListView - the new Datalist :). Almost a hybrid of the datalist and gridview where you can use paging and build in Gridview like functionality, but have the freedom of design. One of the new controls in this family
Repeater - Very light weight. No built in functionality like Headers, Footers. Has the least overhead.
Everyone else hit it: It Depends.
Now for some specific guidance (expanding upon WebDude's excellent answer above) ...
Does your design fit into a natural spreadsheet or grid view of the data? GridView.
Do you need to display a list or other formatted view of data, possibly with headers and footers, and probably with specific controls and/or formatting for each record of data? (EG, customized links, possibly LinkButtons, or specific edit controls?) Does this display specifically not fit naturally into a spreadsheet or grid view? ListView
If you meet all the criteria of ListView, but you would naturally fit in a grid, you may consider DataList.
I go for Repeater when I just need some basic data iterated with some custom design bits, no headers, no footers, nice and clean.
Markup View
Declaring the following sample code is possible for all 3( ListView, DataList , Repeater)
<asp:ListView runat="server" OnItemCommand="Unnamed1_ItemCommand">
<ItemTemplate> <%# Eval("Name")%> </ItemTemplate>
<asp:ListView>
in the following lists You can see the available templates and options for each of them and see the differences for yourself
ListView (note the edit,group,insert ,layout)
AlternatingltemTemplate
EditltemTemplate
EmptyDataTemplate
EmptyltemTemplate
GroupSeparatorTemplate
GroupTemplate
lnsertltemTemplate
ItemSeparatorTemplate
ItemTemplate
LayoutTemplate
SelectedltemTemplate
DataList (note the Style pairs)
AlternatingltemStyle
AlternatingltemTemplate
EditltemStyle
EditltemTemplate
FooterStyle
FooterTemplate
HeaderStyle
HeaderTemplate
ItemStyle
ItemTemplate
SelectedltemStyle
SelectedltemTemplate
SeparatorStyle
SeparatorTemplate
Repeater
AlternatingltemTemplate
FooterTemplate
HeaderTemplate
ItemTemplate
SeparatorTemplate
Code View (advanced view)
CompositeDataBoundControl:
look the following classes hierarchy (and related controls).
these controls hosts other asp.net controls in their templates to display bound-data to user
Some descriptions for better clarifications
The ListView Control
The ListView control also uses templates for the display of data. However, it supports many
additional templates that allow for more scenarios when working with your data. These templates include the LayoutTemplate,GroupTemplate,ItemSeparatorTemplate.
The ListView control (unlike DataList and Repeater) also implicitly supports the ability to
edit, insert, and delete data by using a data source control. You can define individual templates
for each of these scenarios.
The DataList Control
The DataList control works like the Repeater control. It repeats data for each row in your data set,
and it displays this data according to your defined template. However, it lays out the data defined
in the template within various HTML structures. This includes options for horizontal or vertical
layout, and it also allows you to set how the data should be repeated, as flow or table layout.
The DataList control does not automatically use a data source control to edit data. Instead,
it provides command events in which you can write your own code for these scenarios. To
enable these events, you add a Button control to one of the templates and set the button’s
CommandName property to the edit, delete, update, or cancel keyword. The appropriate
event is then raised by the DataList control.
The Repeater Control
The Repeater control also uses templates to define custom binding. However, it does not show data as individual records. Instead, it repeats the data rows as you specify in your template. This
allows you to create a single row of data and have it repeat across your page.
The Repeater control is a read-only template. That is, it supports only the ItemTemplate.
It does not implicitly support editing, insertion, and deletion. You should consider one of the
other controls if you need this functionality, otherwise you will have to code this yourself for
the Repeater control.
The above Descriptions are from MCTS Exam 70-515 Web Applications Development with Microsoft.NET Framework 4 book.
DataGrid is not even mentioned in this book and is replaced by popular GridViews and answered nicely by other users
It all comes down to how you want to layout your data.
If you need to control the layout (like tables versus CSS versus whatever), when use a Repeater or ListView. Between the two, ListView gives you a lot more events and built-in commands for editing, selecting, inserting. Additionally paging and grouping functionality. A Repeater is extremely simple, it repeats a layout with the data. Since you're building the layout by hand, Listview and Repeater require more code.
GridView is an updated DataGrid so there is hardly any reason to use DataGrid. GridView works really well when hooked up to standard ASP.NET datasources, but restricts you to a tabular layout with lots of layout rules. GridView requires less code since you're using a built-in layout.
Indeed! I've blogged on the differences between the ASP.NET 4.0 data tools. Basically, gridviews are the most powerful way to present tabular information, whereas ListView controls are for more complicated displays of repeated data. If I were giving advice to an ASP.NET newbie, I'd tell them to learn gridviews inside out and ignore the other controls to begin with.

Resources