gridview paging and sorting

A

Abraham Luna

i have a gridview and sqldatasource control on a page
i also have a bunch of textboxes and dropdownlists to filter the data.
when the page is first navigated to, i set the selectcommand and call the
select method of the sqldatasource control. then i filter the data and it
still works. but then i try to page and sort the data and it breaks. below is
the code. should i be handling the PageIndexChanged/Sorted event instead of
the PageIndexChanging/Sorting event?

<%@ Page Language="C#" EnableViewState="True"
MasterPageFile="AdminPage.master" %>
<script language="c#" runat="server">

void sdsRDK_Selected(Object Sender, SqlDataSourceStatusEventArgs E)
{
lblTotal.Text = E.AffectedRows.ToString();
}

void Page_Load (Object Sender, EventArgs E)
{
if (!Page.IsPostBack)
{
DataBind();
}
}

void btnRefresh_Click(Object Sender, EventArgs E)
{
DataBind();
}

void gvCustomers_Sorting(Object Sender, GridViewSortEventArgs E)
{
DataBind();
}

void gvCustomers_PageIndexChanging(Object Sender, GridViewPageEventArgs E)
{
DataBind();
}

void DataBind()
{
gvCustomers.PageSize = Convert.ToInt32(ddlRPP.SelectedItem.Value);
lblSQL.Text = "SELECT [CusId], [Name], [Addr1] + '<br>' + [City] + ', ' +
[State] + ' ' + [Post] AS Address, [PhoneWork], [Contact], [EmailWork] FROM
[COCUS] WHERE (CusId Like " + (tbCusId.Text == "" ? "'%' OR CusId Is Null)" :
"'%" + tbCusId.Text + "%')") + " AND (Name Like " + (tbName.Text == "" ? "'%'
OR Name Is Null)" : "'%" + tbName.Text + "%')") + " AND (Addr1 Like " +
(tbAddr1.Text == "" ? "'%' OR Addr1 Is Null)" : "'%" + tbAddr1.Text + "%')")
+ " AND (Addr2 Like " + (tbAddr2.Text == "" ? "'%' OR Addr2 Is Null)" : "'%"
+ tbAddr2.Text + "%')") + " AND (City Like " + (tbCity.Text == "" ? "'%' OR
City Is Null)" : "'%" + tbCity.Text + "%')") + " AND (County Like " +
(tbCounty.Text == "" ? "'%' OR County Is Null)" : "'%" + tbCounty.Text +
"%')") + " AND (State Like " + (ddlState.SelectedItem.Value == "%" ? "'%' OR
State Is Null)" : "'" + ddlState.SelectedItem.Value + "')") + " AND (Post
Like " + (tbPost.Text == "" ? "'%' OR Post Is Null)" : "'%" + tbPost.Text +
"%')") + " AND (Country Like " + (tbCountry.Text == "" ? "'%' OR Country Is
Null)" : "'%" + tbCountry.Text + "%')") + " AND (BillCusId Like " +
(tbBillCusId.Text == "" ? "'%' OR BillCusId Is Null)" : "'%" +
tbBillCusId.Text + "%')") + " AND (BillAddr1 Like " + (tbBillAddr1.Text == ""
? "'%' OR BillAddr1 Is Null)" : "'%" + tbBillAddr1.Text + "%')") + " AND
(BillAddr2 Like " + (tbBillAddr2.Text == "" ? "'%' OR BillAddr2 Is Null)" :
"'%" + tbBillAddr2.Text + "%')") + " AND (BillCity Like " + (tbBillCity.Text
== "" ? "'%' OR BillCity Is Null)" : "'%" + tbBillCity.Text + "%')") + " AND
(BillCounty Like " + (tbBillCounty.Text == "" ? "'%' OR BillCounty Is Null)"
: "'%" + tbBillCounty.Text + "%')") + " AND (BillState Like " +
(ddlBillState.SelectedItem.Value == "%" ? "'%' OR BillState Is Null)" : "'" +
ddlBillState.SelectedItem.Value + "')") + " AND (BillPost Like " +
(tbBillPost.Text == "" ? "'%' OR BillPost Is Null)" : "'%" + tbBillPost.Text
+ "%')") + " AND (BillCountry Like " + (tbBillCountry.Text == "" ? "'%' OR
BillCountry Is Null)" : "'%" + tbBillCountry.Text + "%')") + " AND (PhoneHome
Like " + (tbPhoneHome.Text == "" ? "'%' OR PhoneHome Is Null)" : "'%" +
tbPhoneHome.Text + "%')") + " AND (PhoneWork Like " + (tbPhoneWork.Text == ""
? "'%' OR PhoneWork Is Null)" : "'%" + tbPhoneWork.Text + "%')") + " AND
(PhoneOther Like " + (tbPhoneOther.Text == "" ? "'%' OR PhoneOther Is Null)"
: "'%" + tbPhoneOther.Text + "%')") + " AND (PhoneFax Like " +
(tbPhoneFax.Text == "" ? "'%' OR PhoneFax Is Null)" : "'%" + tbPhoneFax.Text
+ "%')") + " AND (Contact Like " + (tbContact.Text == "" ? "'%' OR Contact Is
Null)" : "'%" + tbContact.Text + "%')") + " AND (EmailHome Like " +
(tbEmailHome.Text == "" ? "'%' OR EmailHome Is Null)" : "'%" +
tbEmailHome.Text + "%')") + " AND (EmailWork Like " + (tbEmailWork.Text == ""
? "'%' OR EmailWork Is Null)" : "'%" + tbEmailWork.Text + "%')") + " AND
(EmailOther Like " + (tbEmailOther.Text == "" ? "'%' OR EmailOther Is Null)"
: "'%" + tbEmailOther.Text + "%')") + " AND (Ssn Like " + (tbSsn.Text == "" ?
"'%' OR Ssn Is Null)" : "'%" + tbSsn.Text + "%')") + " AND (SsnSpouse Like "
+ (tbSsnSpouse.Text == "" ? "'%' OR SsnSpouse Is Null)" : "'%" +
tbSsnSpouse.Text + "%')") + " AND (AllowSpecialPri Like " +
(ddlAllowSpecialPri.SelectedItem.Value == "%" ? "'%' OR AllowSpecialPri Is
Null)" : "'" + ddlAllowSpecialPri.SelectedItem.Value + "')") + " AND (NotId
Like " + (tbNotId.Text == "" ? "'%' OR NotId Is Null)" : "'%" + tbNotId.Text
+ "%')") + " AND (CusMisc1 Like " + (tbCusMisc1.Text == "" ? "'%' OR CusMisc1
Is Null)" : "'%" + tbCusMisc1.Text + "%')") + " AND (CusMisc2 Like " +
(tbCusMisc2.Text == "" ? "'%' OR CusMisc2 Is Null)" : "'%" + tbCusMisc2.Text
+ "%')") + " AND (CusMisc3 Like " + (tbCusMisc3.Text == "" ? "'%' OR CusMisc3
Is Null)" : "'%" + tbCusMisc3.Text + "%')") + " AND (CusMisc4 Like " +
(tbCusMisc4.Text == "" ? "'%' OR CusMisc4 Is Null)" : "'%" + tbCusMisc4.Text
+ "%')") + " AND (CusMisc5 Like " + (tbCusMisc5.Text == "" ? "'%' OR CusMisc5
Is Null)" : "'%" + tbCusMisc5.Text + "%')");
sdsRDK.SelectCommand = lblSQL.Text;
sdsRDK.Select(new DataSourceSelectArguments());
}

</script>
<asp:content id="contentHeader" contentplaceholderid="cphHeader"
runat="server"><img src="../../images/headercustomers.gif" width="185"
height="30"></asp:content>

<asp:content id="contentSubNav" contentplaceholderid="cphSubNav"
runat="server">

</asp:content>

<asp:content id="contentHome" contentplaceholderid="cphMainContent"
runat="server">
<form id="frmCustomers" runat="server">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellpadding="3"
cellspacing="0" class="tablefilter">
<tr>
<td><strong><a
onClick="javascript:ShowHide('tdFilter','imgFilterShow');"
style="cursor:hand"><img id="imgFilterShow" src="../../images/buttonshow.gif"
width="11" height="11" border="0"></a> Filter</strong></td>
</tr>
<tr>
<td id="tdFilter" style="display:none;"><table
width="100%">
<tr>
<td width="20"> </td>
<td><strong>Company Info </strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right">Customer ID:</td>
<td><asp:TextBox ID="tbCusId"
runat="server" /></td>
<td align="right">Company Name:</td>
<td><asp:TextBox ID="tbName"
runat="server" /></td>
</tr>
<tr>
<td align="right">Address 1:</td>
<td><asp:TextBox ID="tbAddr1"
runat="server" /></td>
<td align="right">Address 2:</td>
<td><asp:TextBox ID="tbAddr2"
runat="server" /></td>
</tr>
<tr>
<td colspan="4"><table width="100%"
border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right">City:</td>
<td><asp:TextBox ID="tbCity"
runat="server" /></td>
<td align="right">County:</td>
<td><asp:TextBox ID="tbCounty"
runat="server" /></td>
<td align="right">State:</td>
<td><RDK:StateDropDown ID="ddlState"
runat="server" /></td>
<td align="right">Postal Code:</td>
<td><asp:TextBox ID="tbPost"
runat="server" /></td>
<td align="right">Country:</td>
<td><asp:TextBox ID="tbCountry"
runat="server" /></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>Billing Info</strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right">Billing Customer ID:</td>
<td colspan="3"><asp:TextBox
ID="tbBillCusId" runat="server" /></td>
</tr>
<tr>
<td align="right">Address 1:</td>
<td><asp:TextBox ID="tbBillAddr1"
runat="server" /></td>
<td align="right">Address 2:</td>
<td><asp:TextBox ID="tbBillAddr2"
runat="server" /></td>
</tr>
<tr>
<td colspan="4"><table width="100%"
border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right">City:</td>
<td><asp:TextBox ID="tbBillCity"
runat="server" /></td>
<td align="right">County:</td>
<td><asp:TextBox ID="tbBillCounty"
runat="server" /></td>
<td align="right">State:</td>
<td><RDK:StateDropDown
ID="ddlBillState" runat="server" /></td>
<td align="right">Postal Code:</td>
<td><asp:TextBox ID="tbBillPost"
runat="server" /></td>
<td align="right">Country:</td>
<td><asp:TextBox ID="tbBillCountry"
runat="server" /></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>Phone Numbers </strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right">Home:</td>
<td><asp:TextBox ID="tbPhoneHome"
runat="server" /></td>
<td align="right">Work:</td>
<td><asp:TextBox ID="tbPhoneWork"
runat="server" /></td>
</tr>
<tr>
<td align="right">Other:</td>
<td><asp:TextBox ID="tbPhoneOther"
runat="server" /></td>
<td align="right">Fax:</td>
<td><asp:TextBox ID="tbPhoneFax"
runat="server" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>Contact Info</strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right">Name:</td>
<td><asp:TextBox ID="tbContact"
runat="server" /></td>
<td align="right">SSN:</td>
<td><asp:TextBox ID="tbSsn"
runat="server" /></td>
<td align="right">Spouse SSN: </td>
<td><asp:TextBox ID="tbSsnSpouse"
runat="server" /></td>
</tr>
<tr>
<td align="right">DOB:</td>
<td><asp:TextBox ID="tbDateBirth"
runat="server" /></td>
<td align="right">Spouse DOB: </td>
<td><asp:TextBox ID="tbDateBirthSpouse"
runat="server" /></td>
<td align="right">Allow Special Price:
</td>
<td><RDK:AllowSpecialPriDropDown
ID="ddlAllowSpecialPri" runat="server" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>Email Addresses</strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right">Home:</td>
<td><asp:TextBox ID="tbEmailHome"
runat="server" /></td>
<td align="right">Work:</td>
<td><asp:TextBox ID="tbEmailWork"
runat="server" /></td>
<td align="right">Other:</td>
<td><asp:TextBox ID="tbEmailOther"
runat="server" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>User Info</strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td align="right"> </td>
<td> </td>
<td align="right">Date Created:</td>
<td><asp:TextBox ID="tbDateCreate"
runat="server" /></td>
<td align="right">Date Updated: </td>
<td><asp:TextBox ID="tbDateUpdate"
runat="server" /></td>
</tr>
<tr>
<td align="right">Note ID: </td>
<td><asp:TextBox ID="tbNotId"
runat="server" /></td>
<td align="right"> </td>
<td> </td>
<td align="right">Date Invoice:</td>
<td><asp:TextBox ID="tbDateInvoice"
runat="server" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><strong>Miscellaneous</strong></td>
</tr>
<tr>
<td> </td>
<td><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td><asp:TextBox ID="tbCusMisc1" Rows="5"
TextMode="MultiLine" runat="server" /></td>
<td><asp:TextBox ID="tbCusMisc2" Rows="5"
TextMode="MultiLine" runat="server" /></td>
<td><asp:TextBox ID="tbCusMisc3" Rows="5"
TextMode="MultiLine" runat="server" /></td>
<td><asp:TextBox ID="tbCusMisc4" Rows="5"
TextMode="MultiLine" runat="server" /></td>
<td><asp:TextBox ID="tbCusMisc5" Rows="5"
TextMode="MultiLine" runat="server" /></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td align="right"><asp:Button
ID="btnRefresh1" Text="Refresh" runat="server" CssClass="formbutton"
OnClick="btnRefresh_Click" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><a
onClick="javascript:ShowHide('tdDisplay','imgDisplayShow');"
style="cursor:hand"><img id="imgDisplayShow"
src="../../images/buttonshow.gif" width="11" height="11" border="0"></a>
<strong>Display </strong></td>
</tr>
<tr>
<td id="tdDisplay" style="display:none;"><table
width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right">Results Per Page:</td>
<td><RDK:RPPDropDown ID="ddlRPP"
runat="server" /></td>
</tr>
<tr>
<td colspan="2" align="right"><asp:Button
ID="btnRefresh2" Text="Refresh" runat="server" CssClass="formbutton"
OnClick="btnRefresh_Click" /></td>
</tr>
</table></td>
</tr>
</table>

</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0"
cellpadding="3">
<tr>
<td> </td>
<td align="center"><asp:Label ID="lblTotal"
runat="server" /> Customers</td>
<td align="right"> </td>
</tr>
</table></td>
</tr>
<tr>
<td>
<asp:GridView ID="gvCustomers" runat="server"
AutoGenerateColumns="False" DataSourceID="sdsRDK" AllowPaging="True"
AllowSorting="True" BackColor="#000000" BorderWidth="0px" CellPadding="1"
CellSpacing="2" Width="100%" OnSorting="gvCustomers_Sorting"
OnPageIndexChanging="gvCustomers_PageIndexChanging">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="CusId"
DataNavigateUrlFormatString="editcustomer.aspx?CusId={0}"
Text="Edit" />
<asp:BoundField DataField="CusID" HeaderText="Customer ID:"
SortExpression="CusId" />
<asp:BoundField DataField="Name" HeaderText="Company Name:"
SortExpression="Name" />
<asp:BoundField DataField="Contact" HeaderText="Contact
Name:" SortExpression="Contact" />
<asp:BoundField DataField="PhoneWork" HeaderText="Business
Phone:" SortExpression="PhoneWork" />
<asp:BoundField DataField="Address" HeaderText="Address:"
SortExpression="Addr1" />
<asp:BoundField DataField="EmailWork" HeaderText="Email:"
SortExpression="EmailWork" />
</Columns>
<RowStyle CssClass="tableitem" />
<PagerStyle CssClass="tableitem" />
<HeaderStyle CssClass="tableheader" />
<AlternatingRowStyle CssClass="tablealtitem" />
<PagerSettings Mode="Numeric" />
</asp:GridView>
<asp:SqlDataSource ID="sdsRDK" runat="server"
OnSelected="sdsRDK_Selected" ConnectionString="<%$
ConnectionStrings:TestConnection %>" />

</td>
</tr>
<tr>
<td align="center"><table width="100%" border="0"
cellspacing="0" cellpadding="3">
<tr>
<td> </td>
<td align="center"> </td>
<td align="right"> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><asp:Label ID="lblSQL"
runat="server" Visible="false" /></td>
</tr>
</table></td>
</tr>
</table>
</form>
</asp:content>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top