Paging for Listview inside a Gridview not working - asp.net

I have a listview nested inside a gridview.
I'm trying to get paging working on the listview. I thought that it would display the paging controls, and just page through them normally.
It does display the controls, and limits the result set shown to the appropriate number of records (pageSize) but when I click on the paging controls the grid refreshes and nothing changes with the nested listview (it's still on the first page).
I've tried nesting the listview inside an updatepanel, but the behavior remains. The gridview itself is already in an updatepanel.
So this is the layout I've got:
<Gridview ID="gvApplications" DataSourceID="odsApplications" DataKeyNames="ID" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Functions">
<ItemTemplate>
<asp:ListView ID="lvFunctions" runat="server" DataSource='<%#Eval("ApplicationFunctions") %>'
DataKeyNames="ID">
<LayoutTemplate>
<asp:DataPager ID="dpFunctions" runat="server" PageSize="1" PagedControlID="lvFunctions">
<Fields>
<asp:NextPreviousPagerField />
</Fields>
</asp:DataPager>
<ul>
<li>
<span ID="itemPlaceholder" runat="server" />
</li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblFunction" runat="server" Text='<%# Eval("ApplicationFunction.Name") %>' />
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</Gridview>
Ideas?

Honestly, I would consider using a master-details pattern here. There are a lot of code examples on this. For example:
Google: Master Details Examples with Child Objects
There are also scenarios where the details view (child objects in your case) would display on a separate page. Either way, by displaying the child objects in a separate details view, you avoid the coding and display issues that come with nesting.
Matt Berseth has some of the best code examples out there on this topic:
http://mattberseth.com/blog/gridview/

Listview / datapager combination do not work properly if the listview does not use a datasource control.
Try including a datasource control (objectdatasource could be applicable) in the template field.

Related

Cannot remove viewstate hidden field

I have a massive viewstate hidden field that is causing my application to be unworkable. I have tried:
EnableViewState="false" on every control
EnableViewState="false" in page directive
Page.EnableViewState = false in Page_Init
<pages enableViewState="false" /> in web.config
The page causing the issue has a single GridView which I want to render once only, so I don't ever need the viewstate.
I examined the hidden field using this tool, and there is apparently hardly any info in it (since I disabled the property in every control probably). For some reason though, the page insists on including a hidden field that is thousands and thousands of lines long.
How can I get rid of this field (or reduce it to a usable size) for good?
Here is an exert from the offending GridView:
<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" EnableViewState="False"
CssClass="my-report">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<span title='title' class="abbr">My ID</span>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("my_id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<span title='title2' class="abbr">Second col heading</span>
</HeaderTemplate>
<ItemTemplate>
<asp:ListView ID="MyListView" runat="server" EnableViewState="False">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" EnableViewState="False" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><%# Eval("field_2")%></li>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The hidden field you see on the page is not only for ViewState, it also contains the ControlState. There is no way to disable the control state so you'll need to find a way to live with it. How many items the grid is displaying?
As a last option you may consider compressing generated viewstate field.
Here you have an MSDN article explaining how ControlState works
If your GridView is non-interactive (that is, it doesn't contain any child controls that post back), then you can reduce the size of view state by waiting until the page's Render method is called to bind the grid:
Protected Overrides Sub Render(writer As HtmlTextWriter)
MyGrid.DataSource = ...
MyGrid.DataBind()
MyBase.Render(writer)
End Sub
In case anyone has a similar problem, it was occuring because I had a ListView inside each row of the grid. I replaced the ListView with a Repeater and the viewstate is no longer a problem.
Another option is to use Flesk.ViewState something.
It can put the viewstate on files, compress it, session, etc.
Like the others say, sometimes is inevitable in ASPNET to live with ViewState.
Thats why your best option is to move to MVC :)

Dynamic Data GridView Pager missing DynamicHyperlink Edit parameters

Edit: Feb 7, 2012 - Turns out the key parameter disappears from the GridView Edit link after a Sort as well, so it doesn't appear to be the Pager after all, but the problem persists... any ideas very welcome.
I have a Asp.Net Dynamic Data app. It uses the standard GridViewPager.aspx in Custom List.aspx which is marked up like this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="True" CssClass="DDGridView" HeaderStyle-CssClass="th" RowStyle-CssClass="td" CellPadding="6" AllowSorting="True" AllowPaging="True" PageSize="3"
OnRowDataBound="GridView_OnRowDataBound" OnRowDeleting="GridView_OnRowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="admin">
<ContentTemplate>
<asp:DynamicHyperLink ID="EditLink" runat="server" Action="Edit" Text="edit" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="DDFooter" />
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
On page 1 of the List all is well. The Edit Link rendered includes ?key=xxxxx and the Edit form opens to the correct record.
Once we page off of Page 1 however, the Edit Link rendered on each row has no 'key' parameter and the Edit form always opens to the first row in the database.
I am at a loss to explain this or even where to look. There is no custom code attached to any event associated with the Edit DynamicHyperlink or the GridviewPager.
Has anyone experienced this or have any suggestions as to what might be the issue?
Found the answer here:
LinkButton CommandArgument is empty when it is inside LoginView in a GridView
Turns out good 'ol Microsoft forgot to wire LoginView to fire row-level databind events inside a GridView. No databind, no link parameters!
In deference to a positive attitude, I'll make no comment on the level of organization it takes to allow that out the door....

asp.net data controls

I have a table with the following structure,
I need to restructure the table to show the CustomSpaceName in the following order,
Space3 Personal Case Quick case
Space1 Space2
For each entry I will create a link button and pass the CustomSpaceId in query string.
So which is the ASP.NET Data Control matches best with my requirement. I think using the loop and generate table structure is a BAD idea.
No Need of doing that with a old method when ASP.net gives you GridView and other Data Bounding controls
Basically gridview will do the same operation that you told in a efficient way.
You can use the in-built methods.
Grid View
Repeater
ListView
DataList
Here I will recommend DataList.
Use RepeatColumns="4" property.
<asp:DataList ID="DataList1" RepeatColumns="4" runat="server">
<HeaderTemplate>
<asp:Label runat="server" ID="lbl1" Text='Header'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lbl1" Text='<% Eval("CustomSpaceName ") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>

Asp.net 2.0 Gridview edit

i have used grid view to display data. Now i need to edit the row. I have kept edit link button in template field of grid-view. Now when i click the edit button, i need to retrieve the data for particular row into the server controls, so that user can enter the data into it.
How can i do that?
let me know if any info required..
thanks!
UPDATED
See http://img18.imageshack.us/i/editform.jpg/
Now, when i click edit from below grid, the data in grid should come up in above form.
There is a different template available in Gridview and you have to use it properly. For example, if you want to edit something, the editTemplate is available for that.. look at the following sample:
<asp:GridView runat="server" ID="grd">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lbl"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</InsertItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
have a look on these article
http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx
This is such a general question that you should really review how the GridView works in the first place.
Try reviewing the following example from MSDN regarding GridView editting;
http://msdn.microsoft.com/en-us/library/ms972948.aspx

How can I display the same DataPager control in two places on a page at the same time?

I have a DataPager control that looks like this:
<asp:DataPager ID="page1" PagedControlID="ExperienceList" runat="server" PageSize="3" OnPreRender="page1_PreRender">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
<asp:DropDownList ID="ddlPage" runat="server" AutoPostBack="true" CssClass="default"
OnSelectedIndexChanged="ddlPage_SelectedIndexChanged"></asp:DropDownList>
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
I have slotted this datapager control into a place where I want my dropdown to appear.
This is all working great, but my problem now is that I need another dropdown control to function in exactly the same way, but at the bottom of the listview as well as this one, which is at the top.
Is there any way I can piggyback this existing datapager, or do I have to create an entirely separate datapager at the bottom of the listview and somehow link them together?
Just create a second data pager and hook the events up to the same event handlers

Resources