Paging not working in GridView when ajax control is removed - c#

I have a grid view in my aspx page. I have enabled paging also.
Paging is working fine when an ajax control is in page, but when I removed the ajax control paging is not working anymore. ie, on clicking page number 2 grid becomes empty.
What could be the possible reason? Please suggest a solution.
code
<asp:GridView ID="SitesGrid" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SitesDataMgr" AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="3" CellSpacing="3" OnRowDataBound="viewSite_RowDataBound" class="tbl_blck gridtable clearfix" PageSize="4" EmptyDataText="No rows found">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="clientID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White" SortExpression="ClientName">
<ItemTemplate>
<asp:Label ID="clientLabel" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White" SortExpression="SiteStatus">
<ItemTemplate>
<asp:Label ID="siteLabel" runat="server" Text='<%# Bind("SiteStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="viewSite" runat="server" OnClick="viewSite_click" CausesValidation="False" CommandName="Edit"
Text="View"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="SitesDataMgr" runat="server" ConvertNullToDBNull="True" OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" FilterExpression="ClientName LIKE '%{0}%'" TypeName="Creation.DataMgr">
<SelectParameters>
<asp:SessionParameter Name="createdUserID" Type="String" SessionField="strUserName" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="ClientName" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:ObjectDataSource>

It looks like you have not added the SelectCount method to your ObjectDataSource.
This is required for the ObjectDatasource to correctly page your data.
Your select method needs the int parameters maximumRows and startRowIndex This is the default parameter names required for paging. if you do have them in. Please use them to filter your data.

Related

How can I save a checkboxList from EditItemTemplate in ASP.NET to database?

I have a gridView and I want to change ItemTemplate to CheckBoxList when I click on the Edit button.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:CommandField ShowEditButton="True"/>
<asp:TemplateField HeaderText="LeagueExpert" SortExpression="LeagueExpert">
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="SqlDataSource2"
DataTextField="League_Name" DataValueField="League_Name"
style="vertical-align: middle"></asp:CheckBoxList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label125" class="form-control" runat="server"
onCheckedChanged="onChackedChange"
Text='<%# Bind("LeagueExpert") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The result:
Now I want to save; if I checked 1 and 2 option from CheckboxList, I want to save this in this format: 1,2.
Thanks

Populating a gridview column with an object list

I have a gridview of messages that is populated by an object class. There is a list in the object so that for each message, there is a list of recipients. Problem is, right now the gridview is only displaying "Person[]" for each message. I want it to display as a count of the recipients in the list.
Any help would be appreciated.
HTML
<asp:GridView
ID="grdMsgSent"
runat="server"
CssClass="cellSpacing"
OnRowDataBound="grdMsgSent_RowDataBound"
AllowSorting="True"
EmptyDataText="You have not sent any messages."
AllowPaging="True"
PageSize="6"
OnPageIndexChanging="grdMsgSent_PageIndexChanging"
OnSorting="grdMsgSent_Sorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>
C#
grdMsgSent.DataSource = listSentMsg.List;
grdMsgSent.DataBind();
For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
In C# Code Behind,
DataTable dataTable = new DataTable();
DataColumn dataColumn;
dataColumn = new DataColumn("UserID");
You can use a templatefield:
For array[], do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Length") %>' />
</ItemTemplate>
</asp:TemplateField>
For List, do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Count") %>' />
</ItemTemplate>
</asp:TemplateField>

Sorting of Gridview data

I have A Grid view and I want to sort the Gridview .I tried severel methods from google and they are not working in my side.
my gridvie code is like
<asp:GridView ID="gridviewShopData" runat="server" DataSourceID="SqlDataSource1" Width="100%"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
CssClass="contactList grid" PageSize="30" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderText="Fornavn">
<ItemTemplate>
<%# Eval("Fornavn") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Efternavn">
<ItemTemplate>
<%# Eval("Efternavn") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Medarbejder nummer">
<ItemTemplate>
<%# Eval("Medarbejder nummer") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Varenummer">
<ItemTemplate>
<%# Eval("Varenummer") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle CssClass="altrow" />
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="50" />
<EmptyDataTemplate>
There is no data available to display!
</EmptyDataTemplate>
<PagerStyle CssClass="pager" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT firstName as 'Fornavn',lastName as 'Efternavn',employeeNumber as 'Medarbejder nummer',productID as 'Varenummer' FROM sydShopOrder where shopID=#pageid">
<SelectParameters><asp:QueryStringParameter Name="pageid" QueryStringField="id"/></SelectParameters>
</asp:SqlDataSource>
how to make it possible anyone help.
set SortExpression in template field and try
<asp:TemplateField HeaderText="Fornavn" SortExpression="Fornavn">
<ItemTemplate>
<%# Eval("Fornavn") %>
</ItemTemplate>
</asp:TemplateField>
do this for all that columns which you want to sort

How to define number of rows of GridView at runtime in code behind using C#

I need to develop such a program in which the GridView's rows should be decided at run time.
i.e. I have a table in database called dealer capacity.
Dealer_ID Capacity
D0001 5
Now when the Dealer D00001 is selected from combo box the number of rows in grid view should be 5. I want to use the template field also.
My code for GridView is:
<asp:GridView ID="grdlicence" runat="server" DataKeyNames="Version_id" GridLines="None" BorderStyle="Solid" AutoGenerateColumns="false" AllowSorting="true"
CssClass="mGrid table"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" >
<Columns>
<asp:BoundField DataField="Version_name" ItemStyle-CssClass="uppercase" ItemStyle-Width="150px" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Version_id" Visible="false" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<center><asp:TextBox ID="txtprice" CssClass="alignments TextStyle" MaxLength="5" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Licence Id">
<ItemTemplate>
<center><asp:TextBox ID="txtlicenceid" CssClass="alignments uppercase" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Date">
<ItemTemplate>
<center><asp:TextBox ID="txtpurchasedate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry Date">
<ItemTemplate>
<center><asp:TextBox ID="txtexpirydate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload File">
<ItemTemplate>
<center><asp:FileUpload ID="fileUpload" runat="server" /></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to define PageSize for your GridView and remember to set AllowPaging to true for the GridView
GridView.PageSize Property
Gets or sets the number of records to display on a page in a GridView
control.
The default is 10.
You may see this article: GridView Paging Sample in ASP.NET
You can use the linq Take() and pass the number as parameter.
Updated according to the comment, use following code.
grdlicence.DataSourse= ds.Take(5);
grdlicence.DataBind();

asp.net entity framework <%# Bind("linkedTable.Field") %>

.NET 4 ASP.NET
I have a DetailsView that is displaying an entity framework record for a table that has a linked lookup table. I have an asp:BoundField with the datafield set as "linkedTable.Field" and it displays a value.
<asp:BoundField DataField="linkedTable.Field" HeaderText="linkedTable.Field"
SortExpression="linkedTable.Field" />
I am trying to use that value in an asp:TemplateField but when I try to get it using:
<asp:TemplateField HeaderText="Field" SortExpression="linkedTable.Field" >
<EditItemTemplate>
<asp:Label runat="server" ID="lblField" Text='<%# Bind("linkedTable.Field") %>' />
</EditItemTemplate>
</asp:TemplateField>
Nothing shows up in the label. I can change the Bind() to a field that is not part of the linked table and it works (i.e. the "ID" field). My problem is I don't understand why the linkedtable.Field value shows up in one context and not in the other.
FYI, my data connection is a EntityDataSource
<asp:EntityDataSource ID="edsNYSEDaily" runat="server"
ConnectionString="name=ServerDBEntities"
DefaultContainerName="ServerDBEntities" EntitySetName="tblNYSE"
EntityTypeFilter="tblNYSE" EnableUpdate="True" EnableFlattening="true"
AutoGenerateWhereClause="True" Select="" Where="">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="0" Name="ID"
QueryStringField="ID" Type="Int32" />
</WhereParameters>
Let me know if you need any other information. I am stuck
Ok, discovered the problem:
Needed to add Include="linkedTable" to the EntityDataSource tag. Still not sure why it was even working in the <asp:DataBound /> tag.
Source for the answer: forums.asp.net
Copy of the text:
you should start here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.include.aspx
notice that, you won't be able to Bind (I mean two-way data-binding) the related entities (see the remarks there).
and, you'd have to use a TemplateField for those properties.
see this example (I used the link table 'TableAB' for EntitySetName and included the other two):
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="EntityDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="IDA" HeaderText="IDA" SortExpression="IDA" />
<asp:BoundField DataField="IDB" HeaderText="IDB" SortExpression="IDB" />
<asp:TemplateField HeaderText="TableA Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TableA.NameA") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TableB Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("TableB.NameB") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=ABLinkEntities"
DefaultContainerName="ABLinkEntities" EnableDelete="True" EnableFlattening="False"
EnableInsert="True" EnableUpdate="True" EntitySetName="TableABs" Include="TableA,TableB">
</asp:EntityDataSource>
you'll have to re-consider the updates and deletes, you could do them manually.

Categories