GirdView not sorting after change

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();
}

}
 
A

Alvin Bruney [MVP]

Do any of your events work after the change?

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc


Ashton said:
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();
}

}
 
A

Ashton

Alvin said:
Do any of your events work after the change?

I could still search/add/edit, but Sorting and Paging didn't work.

I found the problem - this line:

UserDataGrid.DataSource = ds;

I had to change it to

ds.ID = "UniqueIDNameWhatever";
UserDataGrid.DataSourceID = "UniqueIDNameWhatever";


It worked if I referenced it via the ID not by setting it directly
(?!?!?!?!?) but at least it works now

Thanks!
 
A

Alvin Bruney [MVP]

makes sense, the event needs to know the id of the control - something new
for 2.0
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top