470,862 Members | 1,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

Filtering and Editing in GridView (.NET 2.0)

I am writing a very simple web application using VS 2005 and a SQL Server
Database. I am just starting to experiment wth .NET 2.0 and I am trying out
the supposedly improved databinding.

I dragged a table from a datasource on to my web form and the GridView
cpntrol was created. I turned on paging, and Edit and Delete buttons. I ran
the app and everything worked great - not a single line of code written yet.

Then I wanted to add a simple feature where the user could filter out data
based on a value in a single column. I added a little code that set the
FilterExpression property of the SqlDataSource, and that seemed to work fine
as well. Here is the FilterExpression code:

SqldataSource1.FilterExpression = "LastName = '" + txtFilterValue.Text + "'";

This worked fine - I got a subset of the original data based on the last
name I entered into a text box.

The problem began if I tried to edit a record in the filtered data or go to
another page in the filtered data. Taking either of these actions resulted
in the restoration of the original unfiltered dataset. If I hit Edit on the
first record of the first page in the filtered dataset, I was switched back
to the first record in the unfiltered dataset and put in edit mode in the
GridView control. If I go to another page of data in the Filtered dataset,
the original dataset is resotred and I am sent to that page number in the
original dataset.

I tried taking the approach of changing the SelectCommand property of the
SqlDataSource object instead of using the FilterExpression property, but that
behaved the same way.

The only code I have is in the "Filter" button click event. I have no code
at all in the page_load event.

This seems like it should be simple, but I am obviously missing something.
I'm pretty sure I could go back to my current .NET v1.1 habits of doing
everything myself in code, but I want to give the new databinding a chance
before I abandoned it.

Anyone have any idea what I am doing wrong?


Bill Manring

Jan 8 '06 #1
2 7955
=?Utf-8?B?QmlsbCBNYW5yaW5n?= <Bi*********@discussions.microsoft.com>
wrote in news:09**********************************@microsof t.com:
I am writing a very simple web application using VS 2005 and a SQL
Server Database. I am just starting to experiment wth .NET 2.0 and I
am trying out the supposedly improved databinding.

I am having exact same problem - if you get an answer I would love to hear
it! andrius at warpmail dot net

Thanks in advance
Feb 5 '06 #2
Not sure if you fixed this yet, but I'll give you the answer.

If your trying to use the new ASP.NET 2.0 approach to a "codeless" Gridview you need to do just that.. make it "codeless". You adding the code to the Search button postback event I assume:

SqldataSource1.FilterExpression = "LastName = '" + txtFilterValue.Text + "'";

This is wrong. You have bypassed the "codeless" functionality of the control by doing this. It loses its state when the page is refreshed or repaged. The values you set are not saved in viewstate. So everytime your page posts back you lose your filter expression. You need to put the expression in the control. For example. Say i have a text box with a search button:

<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />

You don't need any code behind the search button. By clicking it you cause a postback to occur which rebinds the state of the control. in the gridview's datasource you need to add:

ConnectionString="<%$ ConnString %>"
FilterExpression="Description like '%{0}%'">
<asp:ControlParameter ControlID="txtSearch" Name="Description" PropertyName="Text" />

the "filter expression" and "filter parameters" are processed on each postback. As long as there is data in the textbox.. which is saved in viewstate, the grid will be filtered by the expression.

Hope this helps.
Gavin Stevens
Apr 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mike P | last post: by
reply views Thread by SAL | last post: by
2 posts views Thread by SK2010 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.