Hiding or unhiding grid rows in Javascript is not a solution because of the fact that pagination causes any javascript solution to go wonky, unless you can pass up the filters all the way to the factory as shown with this server-side example.
The example I show you was handwritten under 30 minutes.
By wrapping up the controls (except the Object Data Source) inside an Update Panel, you have the best of both worlds. I leave that extra bit for your exercise.
Main Topics
Browse All Topics





by: b1xml2Posted on 2008-07-30 at 07:53:13ID: 22121006
The answer lies in the following:
- Keep the list of IDs
- Pass the list of IDs into the method/controller/factory that makes the call to SQL Server. By passing the list of IDs to the data tier, you allow paging to occur without including the list of ids.
You can pass say a pipe delimited string containing the ids into a stored proc which converts the ids into a table via a function and thus filters out the data in the resultset SQL-side.This will handle the paging issue.
What is needed is to remove the row without actually removing the underlying data. I suppose when a row is removed, a rebinding occurs because of pagination. If this is the case, then the solution to the paging issue also solves how rows are removed or added back to the GridView.
Below is a working example against the AdventureWorks database which comes as a sample with SQL Server 2005.
SQL Function To Handle Filters:
create function ufnGetTableFromIDList(@lis
returns @table table
(
Id int primary key not null
)
as
begin
declare @data varchar(max), @index int;
set @data = @list;
while len(@data) > 0
begin
set @index = charindex('|',@data);
if @index = 0
begin
insert @table
select convert(int,@data)
set @data = '';
end
else
begin
insert @table
select convert(int,left(@data,@in
set @data = substring(@data, @index + 1, len(@data) - @index)
end
end
return;
end
Entity Class
using System;
namespace ExpertsExchange.Q23594780
{
[Serializable]
public class Contact
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
}
}
Factory Class
using System;
using System.ComponentModel;
usin
using System.Data.SqlClient;
usin
namespa
{
[DataObject]
public class ContactFactory
{
private const string SELECT_COMMAND = @"select * from
(
select
row_number() over(order by a.LastName, a.FirstName) as RowNumber,
a.ContactID,
a.FirstName,
a.LastName,
a.EmailAddress
from
(
select
*
from
Person.Contact
where
ContactID not in (select Id from dbo.ufnGetTableFromIDList(
) a
) b
where
b.RowNumber between @startRowIndex + 1 and (@startRowIndex + @maximumRows)";
private const string COUNT_COMMAND = @"select count(*) from Person.Contact where ContactID not in (select Id from dbo.ufnGetTableFromIDList(
private SqlConnection GetConnection()
{
return new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks;Tru
}
public int GetContactsCount(string filters)
{
try
{
using (SqlConnection connection = GetConnection())
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(COUNT_COMMAND, connection))
{
command.Parameters.AddWith
int count = (int)command.ExecuteScalar
return count;
}
}
catch
{
throw;
}
finally
{
connection.Close();
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message
return 0;
}
}
[DataObjectMethod(DataObje
public List<Contact> GetContacts(string filters, int startRowIndex, int maximumRows)
{
try
{
using (SqlConnection connection = GetConnection())
{
try
{
connection.Open();
using (SqlCommand command = new SqlCommand(SELECT_COMMAND,
{
List<Contact> list = new List<Contact>();
command.Parameters.AddWith
command.Parameters.AddWith
command.Parameters.AddWith
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Contact item = new Contact();
item.Id = (int)reader["ContactID"];
item.FirstName = (string)reader["FirstName"
item.LastName = (string)reader["LastName"]
item.Email = (string)reader["EmailAddre
list.Add(item);
}
reader.Close();
}
return list;
}
}
catch
{
throw;
}
finally
{
connection.Close();
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message
return new List<Contact>();
}
}
}
}
ASPX File
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head runat="server">
<title>Q23594780</title>
</head>
<b
<form id="form1" runat="server">
<div>
<asp:GridView ID="ContactGridView" runat="server"
DataSourceID="ContactObjec
AutoGenerateColumns="False
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="RemoveButton" runat="server" OnClick="RemoveButton_Clic
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<a href='<%# Eval("Email","mailto:{0}")
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:ListBox ID="SelectedFiltersListBox
</asp:ListBox>
<asp:Button ID="RemoveFilterItemButton
</div>
<asp:ObjectDataSource ID="ContactObjectDataSourc
TypeName="ExpertsExchange.
OldValuesParameterFormatSt
EnablePaging="True" SelectCountMethod="GetCont
onselecting="ContactObject
<SelectParameters>
<asp:Parameter Name="filters" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</form>
</body>
</html>
Code-Behind
using System;
using System.Collections;
using System.Collections.Generic
using System.Web.UI.WebControls;
namespace ExpertsExchange.Q23594780
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RemoveButton_Click(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)((Button)send
DataKey key = ContactGridView.DataKeys[r
string text = string.Format("{0}, {1} ({2})", key["LastName"], key["FirstName"], key["Email"]);
string value = key["Id"].ToString();
this.SelectedFiltersListBo
this.ContactGridView.DataB
}
protected void ContactObjectDataSource_Se
{
List<string> list = new List<string>();
foreach (ListItem item in this.SelectedFiltersListBo
{
list.Add(item.Value);
}
string filters = string.Join("|", list.ToArray());
e.InputParameters[0] = filters;
}
protected void RemoveFilterItemButton_Cli
{
if (this.SelectedFiltersListB
{
this.SelectedFiltersListBo
this.ContactGridView.DataB
}
}
}
}