A
Ashton
Hello,
I have a GridView; once the page is loaded I can sort it just fine, I
can sort it as many times as I would like, I can sort any of the
columns.
After clicking on Search and the GridView is updated with new data the
sort does not work.
I'm using C# and Ajax for the implementation, below is the code,
any help would be greatly appreciated
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs"
Inherits="User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript">
document.write(Math.random());
</script>
<form id="UserForm" runat="server">
<asp:ScriptManager ID="sm" runat="Server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:TextBox ID="StatusTxt" runat="server"/>
<br />
<table>
<tr>
<th>Login</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th></th>
</tr>
<tr>
<td><asp:TextBox ID="LoginTxt" runat="server"
/></td>
<td><asp:TextBox ID="FirstNameTxt"
runat="server" /></td>
<td><asp:TextBox ID="LastNameTxt"
runat="server" /></td>
<td><asp:TextBox ID="EmailAddressTxt"
runat="server" /></td>
<td>
<asp:Button ID="SearchBtn"
Text="Search"
OnClick="Select"
runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="UserDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:RolloutsProject %>"/>
<asp:UpdatePanel ID="panel2" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:GridView
EnableSortingAndPagingCallbacks="true"
ID="UserDataGrid"
AutoGenerateColumns="False"
DataKeyNames="UserID"
AllowPaging="True"
AllowSorting="True"
runat="server">
<Columns>
<asp:BoundField DataField="UserID"
HtmlEncode="False"
HeaderText="Login"
SortExpression="UserID"/>
<asp:BoundField DataField="Login"
HtmlEncode="False"
HeaderText="Login"
SortExpression="Login"/>
<asp:BoundField DataField="LastName"
HtmlEncode="False"
HeaderText="Last Name"
SortExpression="LastName"/>
<asp:BoundField DataField="FirstName"
HtmlEncode="False"
HeaderText="First Name"
SortExpression="FirstName"/>
<asp:BoundField DataField="EmailAddress"
HtmlEncode="False"
HeaderText="Email"
SortExpression="EmailAddress"/>
</Columns>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="SearchBtn" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class User : System.Web.UI.Page
{
public SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["RolloutsProject"].ConnectionString);
public SqlDataSource ds = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
ds.ConnectionString =
ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
ds.SelectCommand = " select * from UserInfo ";
ds.SelectCommand += " where 0=0 ";
ds.SelectCommand += " and LastName like @LastName ";
ds.SelectCommand += " and FirstName like @Firstname ";
ds.SelectCommand += " and Login like @Login ";
ds.SelectCommand += " and EmailAddress like @EmailAddress ";
ds.SelectCommandType = SqlDataSourceCommandType.Text;
ds.SelectParameters.Add("UserID", TypeCode.Int32, "0");
ds.SelectParameters.Add("LastName", TypeCode.String, "%");
ds.SelectParameters.Add("FirstName", TypeCode.String, "%");
ds.SelectParameters.Add("EmailAddress", TypeCode.String, "%");
ds.SelectParameters.Add("Login", TypeCode.String, "%");
ds.SelectParameters.Add("Password", TypeCode.String, "%");
UserDataGrid.DataSource = ds;
UserDataGrid.DataBind();
}
public void Select(object sender, EventArgs e)
{
ds.SelectParameters["LastName"].DefaultValue = LastNameTxt.Text
+ "%";
ds.SelectParameters["FirstName"].DefaultValue =
FirstNameTxt.Text + "%";
ds.SelectParameters["EmailAddress"].DefaultValue =
EmailAddressTxt.Text + "%";
ds.SelectParameters["Login"].DefaultValue = LoginTxt.Text +
"%";
UserDataGrid.DataBind();
}
}
I have a GridView; once the page is loaded I can sort it just fine, I
can sort it as many times as I would like, I can sort any of the
columns.
After clicking on Search and the GridView is updated with new data the
sort does not work.
I'm using C# and Ajax for the implementation, below is the code,
any help would be greatly appreciated
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs"
Inherits="User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
</head>
<body>
<script type="text/javascript" language="javascript">
document.write(Math.random());
</script>
<form id="UserForm" runat="server">
<asp:ScriptManager ID="sm" runat="Server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:TextBox ID="StatusTxt" runat="server"/>
<br />
<table>
<tr>
<th>Login</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th></th>
</tr>
<tr>
<td><asp:TextBox ID="LoginTxt" runat="server"
/></td>
<td><asp:TextBox ID="FirstNameTxt"
runat="server" /></td>
<td><asp:TextBox ID="LastNameTxt"
runat="server" /></td>
<td><asp:TextBox ID="EmailAddressTxt"
runat="server" /></td>
<td>
<asp:Button ID="SearchBtn"
Text="Search"
OnClick="Select"
runat="server" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="UserDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:RolloutsProject %>"/>
<asp:UpdatePanel ID="panel2" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:GridView
EnableSortingAndPagingCallbacks="true"
ID="UserDataGrid"
AutoGenerateColumns="False"
DataKeyNames="UserID"
AllowPaging="True"
AllowSorting="True"
runat="server">
<Columns>
<asp:BoundField DataField="UserID"
HtmlEncode="False"
HeaderText="Login"
SortExpression="UserID"/>
<asp:BoundField DataField="Login"
HtmlEncode="False"
HeaderText="Login"
SortExpression="Login"/>
<asp:BoundField DataField="LastName"
HtmlEncode="False"
HeaderText="Last Name"
SortExpression="LastName"/>
<asp:BoundField DataField="FirstName"
HtmlEncode="False"
HeaderText="First Name"
SortExpression="FirstName"/>
<asp:BoundField DataField="EmailAddress"
HtmlEncode="False"
HeaderText="Email"
SortExpression="EmailAddress"/>
</Columns>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="SearchBtn" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class User : System.Web.UI.Page
{
public SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["RolloutsProject"].ConnectionString);
public SqlDataSource ds = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
ds.ConnectionString =
ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
ds.SelectCommand = " select * from UserInfo ";
ds.SelectCommand += " where 0=0 ";
ds.SelectCommand += " and LastName like @LastName ";
ds.SelectCommand += " and FirstName like @Firstname ";
ds.SelectCommand += " and Login like @Login ";
ds.SelectCommand += " and EmailAddress like @EmailAddress ";
ds.SelectCommandType = SqlDataSourceCommandType.Text;
ds.SelectParameters.Add("UserID", TypeCode.Int32, "0");
ds.SelectParameters.Add("LastName", TypeCode.String, "%");
ds.SelectParameters.Add("FirstName", TypeCode.String, "%");
ds.SelectParameters.Add("EmailAddress", TypeCode.String, "%");
ds.SelectParameters.Add("Login", TypeCode.String, "%");
ds.SelectParameters.Add("Password", TypeCode.String, "%");
UserDataGrid.DataSource = ds;
UserDataGrid.DataBind();
}
public void Select(object sender, EventArgs e)
{
ds.SelectParameters["LastName"].DefaultValue = LastNameTxt.Text
+ "%";
ds.SelectParameters["FirstName"].DefaultValue =
FirstNameTxt.Text + "%";
ds.SelectParameters["EmailAddress"].DefaultValue =
EmailAddressTxt.Text + "%";
ds.SelectParameters["Login"].DefaultValue = LoginTxt.Text +
"%";
UserDataGrid.DataBind();
}
}