This sounds like something that should be real simple, but I can't seem to find the answer.
I have a page with 2 drop-downs used to filter a GridView. Each dropdown has an 'All' ListItem with a value of '%', and appears as the default selection in each list (the rest of the items in the dropdown come from a DB). Everything works great, but by default
the GridView shows all records. As this data set can get a bit large, I'd prefer if the user had to select an filter value from the dropdown before the GV is populated.
If I remove the 'All' ListItem, the dropdowns default to the first item in the list, which is OK, but then selecting 'All' is no longer an option.
Any thoughts on how to implement what I'm looking for? Thanks!
If you are populating your gridview from vode-behind (C#/VB.net) the simplest solution would be to remove "GridView1.DataBind()" from the page_load (or wahtever method you are usign to load gridview when the page loads first) and by removeign the "All" from
your drop-downs.
And as you mentioned if the dropdwns work okay (depending on your logic, to flourish the gridview ) you may not need to disturb the selection changed event handler of the drop down.
I am not populating the GV from the CodeBehind - I simply have it bound to a Datasource. The DDL is used as a filter parameter for the data source. I'm not a very experienced codewriter, and though I have a few things in my CodeBehind, I try to keep things
simple and use what's given to me whenever possible. ;)
I was able to get the result I wanted by adding another ListItem with a 'dummy' value, like this:
Now, when the page comes up by default, the "<Select a Dept>" value is the default, but there is no "abcde" dept, so the gridview is empty. The rest of the page (header, footer, etc... from the Master Page) loads quickly (whereas previously the page would
be blank until the data was loaded). Is there anything wrong with this approach?
Sarathi125 - When you say to use a stored proc, are you refering to a SP on the database server (I'm using an Oracle back end, querying a view, if it matters).
I would change the way you are binding the gv to the code behind. If you have a lot of data you can hook up the page to an ajax panel with a timer. Give the page a second to load and then have the timer populte your dataTable/dataSet. That way the user
is not waiting for all of the data from the database call and then bind everything on the page load, this makes the page look more responsive. This way the user sees the page right away and in the background your page is fetching the data via the timer event.
The upside to that is once you have all of the data you keep it in the session/cache and can use your drop down to select/filter from your cashed data. That way you're not going back and forth every time to the database you are filtering your data; this
is much faster. Additionally, since your gridview and dropdown are in the ajax panel, there is no page flicker when you are filter against the data and bind to the gv.
HTH,
Sean Gahan
http://SeanGahan.Net
Marked as answer by LMychajluk on Dec 19, 2012 01:28 PM
LMychajluk
Member
2 Points
9 Posts
Open a page to a blank GridView?
Dec 17, 2012 03:02 PM|LINK
This sounds like something that should be real simple, but I can't seem to find the answer.
I have a page with 2 drop-downs used to filter a GridView. Each dropdown has an 'All' ListItem with a value of '%', and appears as the default selection in each list (the rest of the items in the dropdown come from a DB). Everything works great, but by default the GridView shows all records. As this data set can get a bit large, I'd prefer if the user had to select an filter value from the dropdown before the GV is populated.
If I remove the 'All' ListItem, the dropdowns default to the first item in the list, which is OK, but then selecting 'All' is no longer an option.
Any thoughts on how to implement what I'm looking for? Thanks!
aarsh
Participant
1543 Points
427 Posts
Re: Open a page to a blank GridView?
Dec 17, 2012 03:12 PM|LINK
If you are populating your gridview from vode-behind (C#/VB.net) the simplest solution would be to remove "GridView1.DataBind()" from the page_load (or wahtever method you are usign to load gridview when the page loads first) and by removeign the "All" from your drop-downs.
And as you mentioned if the dropdwns work okay (depending on your logic, to flourish the gridview ) you may not need to disturb the selection changed event handler of the drop down.
Demo Code:
You may refer "Gridview with a dropdown filter in the header" example from http://wildclick.wordpress.com/codelib/codelib-aspnet/ if you face any problems.
sarathi125
Star
13599 Points
2691 Posts
Re: Open a page to a blank GridView?
Dec 17, 2012 03:13 PM|LINK
Hi,
Step 1: Remove the "All" from the dropdown
Step 2: Bind the gridview based on the DropDownList Items(First Item).
Step 3: Insert the "All" dropdownlist item into the dropdownlist.
Else
First dont load anything in the gridview after page loads then based on the dropdown selection bind gridview.
Else
For the "All" item if the rows count is more than 1000 then use stored procedure which will load the gridview page by page 20 rows...
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
LMychajluk
Member
2 Points
9 Posts
Re: Open a page to a blank GridView?
Dec 17, 2012 04:08 PM|LINK
Thanks for the quick replies, guys.
I am not populating the GV from the CodeBehind - I simply have it bound to a Datasource. The DDL is used as a filter parameter for the data source. I'm not a very experienced codewriter, and though I have a few things in my CodeBehind, I try to keep things simple and use what's given to me whenever possible. ;)
I was able to get the result I wanted by adding another ListItem with a 'dummy' value, like this:
<asp:DropDownList ID="ddlDept" DataSourceID="dsDeptList" AutoPostBack="true" DataValueField="DEPT_ID" runat="server" Width="130px" Font-Size="11px" AppendDataBoundItems="true" ViewStateMode="Disabled"> <asp:ListItem Text="<Select a Dept>" Value="abcde"></asp:ListItem> <asp:ListItem Text="<All>" Value="%"></asp:ListItem> </asp:DropDownList>Now, when the page comes up by default, the "<Select a Dept>" value is the default, but there is no "abcde" dept, so the gridview is empty. The rest of the page (header, footer, etc... from the Master Page) loads quickly (whereas previously the page would be blank until the data was loaded). Is there anything wrong with this approach?
Sarathi125 - When you say to use a stored proc, are you refering to a SP on the database server (I'm using an Oracle back end, querying a view, if it matters).
Thanks again!
Sean Gahan
Member
85 Points
14 Posts
Re: Open a page to a blank GridView?
Dec 17, 2012 07:18 PM|LINK
I would change the way you are binding the gv to the code behind. If you have a lot of data you can hook up the page to an ajax panel with a timer. Give the page a second to load and then have the timer populte your dataTable/dataSet. That way the user is not waiting for all of the data from the database call and then bind everything on the page load, this makes the page look more responsive. This way the user sees the page right away and in the background your page is fetching the data via the timer event. The upside to that is once you have all of the data you keep it in the session/cache and can use your drop down to select/filter from your cashed data. That way you're not going back and forth every time to the database you are filtering your data; this is much faster. Additionally, since your gridview and dropdown are in the ajax panel, there is no page flicker when you are filter against the data and bind to the gv.
Sean Gahan
http://SeanGahan.Net
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: Open a page to a blank GridView?
Dec 19, 2012 05:29 AM|LINK
Please check if it is what you're looking for:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."> <Columns> <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" /> <asp:BoundField DataField="time" HeaderText="time" SortExpression="time" /> <asp:BoundField DataField="min" HeaderText="min" SortExpression="min" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>" ProviderName="<%$ ConnectionStrings:TestDBConnectionString1.ProviderName %>"> <SelectParameters> <asp:ControlParameter Name="Property" ControlID="ddlDept" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource> <asp:DropDownList ID="ddlDept" AutoPostBack="true" DataValueField="DEPT_ID" DataTextField="DEPT_ID" runat="server" Width="130px" Font-Size="11px" AppendDataBoundItems="true" ViewStateMode="Disabled" DataSourceID="SqlDataSource2" onselectedindexchanged="ddlDept_SelectedIndexChanged"> <asp:ListItem Text="<Select a Dept>" Value="abcde"></asp:ListItem> <asp:ListItem Text="<All>" Value="%"></asp:ListItem> </asp:DropDownList>protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e) { if (ddlDept.SelectedValue == "adbc") { //do not select any redords from source SqlDataSource1.SelectCommand = ""; } if (ddlDept.SelectedValue == "%") { //select All redords from source SqlDataSource1.SelectCommand = "SELECT [id], [time], [min] FROM [name]"; } else { SqlDataSource1.SelectCommand = "SELECT [id], [time], [min] FROM [name] where [id]=@Property"; SqlDataSource1.SelectParameters["Property"].DefaultValue = ddlDept.SelectedValue; } }Feedback to us
Develop and promote your apps in Windows Store