472,968 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

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 8055
=?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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
by: Mike P | last post by:
I have a couple of columns in my gridview that I am adding formatting to in the RowDataBound event. But when I want to edit the rows when the RowDataBound event gets called, I get an error. I'm...
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1"...
by: kevinpond | last post by:
I have a Gridview displaying the results of a stored procedure. The stored procedure is constantly changing so the columns displayed in the data grid are constantly changing. I'd like to give...
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
by: needhelp1 | last post by:
I have gridview with a detailsview below. When I click on 'New' in brings up the DetailsView for inserting. When I click on 'Edit' in does not brink up the DetailsView. What am I doing wrong? I...
by: SAL | last post by:
Hello, I have a small Web app that has a couple of pages in it. Both pages have gridviews on them with basically the same ObjectDataSource. This object data source is a business class. One of the...
by: lidadil | last post by:
Hello Everyone, i'm having a gridview wich list clients details. i have setup a search text box and a dropdownlist for filtering. Everything is working except when i try to navigate the next...
by: SK2010 | last post by:
I have a gridview with edit and delete. When i click Edit, I donot get all the textboxes. I just get 1. on top of that, when I try to update anything in that textbox, I get object Reference not set...
by: DeanC | last post by:
I am having a problem when I am updating my rows. The data is being typed in is getting overriden by the data that was previously there. Does anyone know how to fix this? protected void...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.