Why does my ASP GridView Paging not work? - asp.net

I have a fairly simple asp grid view, tied to an object data source. What I want to have is this gridview update on a button click with results that a dynamic depending on the value within a textbox (it's the basis for a search screen).
So far, everything works as such:
ASPX File:
<h3>Search Parameters</h3>
<div>
Account Name
<asp:TextBox runat="server" ID="AccountName"></asp:TextBox>
</div>
<asp:Button ID="Search" runat="server" Text="Search" OnClick="Search_Click" />
<asp:GridView runat="server" ID="SearchGrid" DataSourceID="ObjectDataSource1" AutoGenerateColumns="False" Width="100%" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" >
<Columns>
<asp:BoundField DataField="PartyID" HeaderText="Party ID" SortExpression="PartyID" />
<asp:BoundField DataField="PartyName" HeaderText="Party Name" SortExpression="PartyName" />
<asp:BoundField DataField="CompleteAddress" HeaderText="Address" SortExpression="CompleteAddress" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="DIS.Data.DataSetAccountsTableAdapters.GetAccountsBySearchParametersTableAdapter">
<SelectParameters>
<asp:Parameter Name="PartyName" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
ASPX.CS File:
protected void Search_Click(object sender, EventArgs e)
{
ObjectDataSource1.SelectParameters["PartyName"].DefaultValue = AccountName.Text;
}
At face value, everything works. The user enters text into the account name box, hits search, and the grid view is updated with the appropriate values.
The problem occurs when we get enough row to generate paging. The page links are shown on the GridView, but clicking on them has no effect at all - The values will remain consistently on page 1.
I believe that it may be something to do with the postback when the page link is clicked, but unfortunately my knowledge in this area is not strong enough to actually diagnose what exactly is happening.
Any help would be greatly appreciated
Best regards

OK, on further testing it doesn't appear to be a GridView specific issue.
We aer also using JQuery mobile, and it seems that it's those scripts that are causing the issue. Disabling them removed the problem.

Related

Dropdownlist autopostback property is getting triggered even when I click any other options in gridview or on any other control on the same page

The dropdownlist and the gridview are on the same page.
Code for dropdownlist :-
<asp:DropDownList OnSelectedIndexChanged="drplist_SelectedIndexChanged"
AutoPostBack="true" ViewStateMode="Disabled" AppendDataBoundItems="true"
ID="drplist" runat="server" DataSourceID="datasource1"
DataTextField="User_ID" DataValueField="User_ID">
<asp:ListItem Value="" Text="Make a selection"/>
</asp:DropDownList>
Code for gridview :-
<asp:GridView ID="gview" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="User_ID" DataSourceID="datasource">
<Columns>
<asp:BoundField DataField="User_ID" HeaderText="User ID" />
<asp:BoundField DataField="User_Name" HeaderText="User Name" />
<asp:BoundField DataField="User_Email" HeaderText="User e-mail" />
<asp:BoundField DataField="User_Mob" HeaderText="User mobile number" />
<asp:BoundField DataField="User_Created" HeaderText="Created Date" />
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
</Columns>
</asp:GridView>
The dropdown has User IDs from the database and when a selection is made from the dropdown it fires it selectedIndexChanged event , code for the same:-
protected void drplist_SelectedIndexChanged(object sender, EventArgs e)
{
var index = drplist.SelectedValue;
drplist.SelectedIndex = 0;
Response.Redirect("~/Demo/TableDemo.aspx?User_ID="+index);
}
Now when I click browser's back button , the dropdown still retains its selected value, which I do not want, I want it to reset to index 0. As I return to the dropdownlist from the TableDemo.aspx page and click on the edit button or delete button in gridview , the postback for the dropdown is triggered again taking me to the TableDemo.aspx page. Only a manual page refresh sets things straight. I tried to search and implement JS code for forcing page refresh on pressing browser back button but it didn't solve my issue and created more. I have tried using the update panel to my frustration and it happens to do nothing to solve my issue here. Sorry for the long post I couldn't do better in making the scenario shorter in writing here.
You probably have your page cached, thats the reason it doesn't behave the way you want when you click the back button. Try to add this code to your page_load on the page you don't want to be cahched.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Hope this helps!

Dynamic population in gridview

I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards

Gridview ASP.NET is not shown when loading webpage

well I'm so new to ASP.NET I've been working on this project for about two weeks now, but I can't get a GridView with an objectdatasource to show when webpage loads, I can see all columns in design time.
The method returns a List and in design time I can see all columns with the right column name but when loading the service and the webpage afterwards nothing shows up. Any help will be appreciated. Regards to all readers.
The method receives a string value I've set a default to 'q'.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Contraseña" HeaderText="Contraseña"
SortExpression="Contraseña" />
<asp:BoundField DataField="Id_User" HeaderText="Id_User"
SortExpression="Id_User" />
<asp:BoundField DataField="Nombre" HeaderText="Nombre"
SortExpression="Nombre" />
<asp:BoundField DataField="Tipo" HeaderText="Tipo" SortExpression="Tipo" />
<asp:BoundField DataField="Usuario" HeaderText="Usuario"
SortExpression="Usuario" />
</Columns>
</asp:GridView>
Add the following to your GridView:
<asp:GridView ID="GridView1" runat="server"
EmptyDataText="DOH! No Data!"
ShowHeaderWhenEmpty="True"
The code looks fine. You don't actually need to call databind() with Data Source Control, instead perhaps you need to check the return value in SelectMethod of objectdatasource.

Access Cell value in gridview

I thought this would be an easy one for me to figure out (or atl east find someone online who has) but I am running into issues.
I have the following code that creates my gridview:
<asp:GridView runat="server" ID="ContactsGrid" AutoGenerateColumns="False" DataSourceID="LinqContact"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="IConact_ID" Visible="false" ReadOnly="true" />
<asp:BoundField DataField="cFirstName" HeaderText="First Name" ReadOnly="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqContact" runat="server" ContextTypeName="TIPS.App_Data.TIPSDataContext" onselecting="LinqContact_Selecting" >
</asp:LinqDataSource>
Now on the c# side, I want to be able to pull the value from the first column (which is hidden) and use it to delete that specific record (with the onroedeleting event), but all the ways I found to pull the value, all come up null, like what would happen if I didn't have a LinqDatasource.
I have tried (and a slew of other that really didn't seem to be right, so not listed):
ContactsGrid.SelectedRow.Cells[0].Text;
ContactsGrid.Columns[0];
Thanks for any help!
Edit:
Ok, so I found that you can't get the value of a column this is hidden when you hide it using the grid. I did find a work around. If you hide the column using css instead, you can still access the colum.
<style type="text/css">
.hiddencol
{
display:none;
}
</style>
<asp:BoundField DataField="IContact_ID" ReadOnly="true" itemstyle-cssclass="hiddencol" />
I don't think that this is the prefered .net way. I found a reference to something called datakeynames which seems to be the proper way. I am going to dig into those next.
Edit #2:
I see that Maras and myself both came up with a solution for the hidden field, but I think I found the best one (cause its simple and built in).
In the gridview tag you can set the datakeynames attribute to your column (like a primary key, which is what I was storing in my hidden column) you can also store multiple columns.
<asp:GridView runat="server" ID="ContactsGrid" AutoGenerateColumns="False" DataSourceID="LinqContact" DataKeyNames="IContact_ID"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >
you can then reference it with:
ContactsGrid.DataKeys[e.RowIndex].Value;
Try this:
void ContactsGrid_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
ContactsGrid.Rows[e.RowIndex].Cells[0];
}
See here for more details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx#Y200
.
Edit after author's comment:
If you set it visibility to 'false' then there is nothing your browser can send you back in postback. You can use hidden field like in the second example:
<asp:GridView runat="server" ID="gv" OnRowDeleting="gv_RowDeleting" AutoGenerateColumns="false" AutoGenerateDeleteButton="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hf" runat="server" Value="<%# ((YourItemType)Container.DataItem).Id %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
HiddenField hf = (HiddenField) gv.Rows[e.RowIndex].Cells[0].FindControl("hf");
}

Filtering ASPxGridView by a ASPxDateEdit using Control

I want to filter my gridview by choosing date and time in a dateedit. I put a button which has DataBind() controls. The query of the DataSource of the GridView has a WHERE clause. It has a Control connecting the gridview and the dateedit. When I test the query, it works fine. I enabled ButtonClick event and wrote the following code into that:
protected void ASPxButton2_Click(object sender, EventArgs e)
{
DateEdit.DataBind();
SQLDataSource.DataBind();
ASPxGridView.DataBind();
}
As you can see I bound all stuff that I need. But no items displayed when I choose date and time and click the button. Did I miss something? I appreciate if you help.
I'm the DevExpress ASP.NET Technical Evangelist, Mehul.
There are many ways you can approach this but I recommend using the ASPxGridLookup control which gives you a grid within a dropdown:
http://demos.devexpress.com/ASPxGridViewDemos/ASPxGridLookup/FilterServerMode.aspx
You can also use the built-in features:
http://www.devexpress.com/Support/Center/p/Q267406.aspx
Or try this sample:
http://www.devexpress.com/Support/Center/e/E2040.aspx
Some of these may help you as well:
http://search.devexpress.com/?q=filter+external&p=T4|P5|57
To answer your original question, this sample shows you how to databind on an external button click:
http://www.devexpress.com/Support/Center/e/E1662.aspx
Thanks, hope that helps.
Unfortunately, you did not post the code showing how the SQLDataSource is adjusted. However, I've tried to reproduce this problem and failed. Here is my code:
<dx:ASPxGridView ID="gvSupply" ClientInstanceName="gvSupply" Width="100%"
DataSourceID="SqlDataSource3" KeyFieldName="OrderID" AutoGenerateColumns="False" runat="server">
<Columns>
<dx:GridViewDataTextColumn FieldName="OrderID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CustomerID" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="EmployeeID" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataDateColumn FieldName="OrderDate" VisibleIndex="3">
</dx:GridViewDataDateColumn>
</Columns>
</dx:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Orders] WHERE ([OrderDate] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="ASPxDateEdit1" Name="OrderDate" PropertyName="Value"
Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>
<dx:ASPxDateEdit ID="ASPxDateEdit1" runat="server">
</dx:ASPxDateEdit>
<asp:Button ID="Button1" runat="server" Text="Button" />
I've tested the grid with the Orders table from the Northwind database. Also, to test your query, I suggest that you handle the Selecting event of the SQLDataSource and check the e.Command and e.Argumets parameters. I hope, this information will be helpful to you.

Resources