Using .net 4 and c# I'm trying to get data from database into GridView and display the number of rows returned. I also have a dropdown list used as ControlParameters in SqlDataSource to filter results.
This works fine on initial page load, but when I select something from the dropdown the results are filtered, but rows count doesn't change. When I select another item from the dropdown, the rows count gets updated but with the number correct for the previous value.
<h5> Showing <asp:Label ID="WOs_count" runat="server" Text="Label"></asp:Label> Work Orders.</h5>
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem Value="%">All requests</asp:ListItem>
<asp:ListItem Value="BBTeam">BB Team</asp:ListItem>
</asp:DropDownList>
The Select statement:
<asp:SqlDataSource
ID="WorkList"
runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT TASKS.WOID AS wo_id, Tasks.Completed AS 'Completed Date', Tasks.Respons AS 'Technician', FROM Tasks WHERE (Tasks.Completed>=DATEADD(day,-60,GETDATE())OR Tasks.Completed IS NULL) AND (Tasks.Respons LIKE @Respons) ">
<SelectParameters>
<asp:ControlParameter name="Respons" ControlID="DropDownList1" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:SqlDataSource>
Then in the code behind I have this:
protected void Page_Load(object sender, EventArgs e)
{
WOs_count.Text = WO_list.Rows.Count.ToString();
}
I'm completely new to .net so wouldn't be surprised if I miss something obvious.
I think your issue is that the Page_Load event is firing before the grid is re-binding to the data, which is why the count is always one postback behind.
You could try updating the count in a later Page event, like OnPreRender(object sender, EventArgs e)
or updating the count in the DataBound event on your Grid View...
Add this to your ASPX :
<asp:GridView ID="WO_list" runat="server" OnDataBound="WO_list_DataBound" ... />
and then add this to the Code Behind (C#):
protected void WO_list_DataBound(EventArgs e){ WOs_count.Text = WO_list.Rows.Count.ToString(); }
Give this a try and let me know if either/both work.
This should fix it for you.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
WOs_count.Text = WO_list.Rows.Count.ToString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.