Answered by:
gridview not getting refreshed when dropdown selection is changed

Question
-
User154499744 posted
I have 2 gridviews showing some data filtered by a selection in a drop down list. The filtering happens as expected in both gridviews when I change the item in the dropdownlist. This works, but the next scenario is giving me trouble.
Here is what I did next. I placed a multiview with 2 views in it. And I put one of the gridviews in one view and the other gridview in the other view. When I do this, it seems only the gridview that is in the active view gets updated when I change the dropdownlist item. If I go to the view that wasn't active when I changed the dropdownlist item, that gridview is still showing the dataset that pertains to the value in the dropdownlist previous to the new selection.
I hope my description of the scenario makes sense. If so, how can I make it so that all gridviews get updated as I change the dropdownlist?
Monday, May 23, 2016 7:46 PM
Answers
-
User61956409 posted
Hi NewKid1nTown,
As far as I know, we put a Button control that moves to a specific View in each View, so you could try to rebind GridView on Button Click event.
<div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> <asp:ListItem Text="FEB" Value="FEB"></asp:ListItem> <asp:ListItem Text="APR" Value="APR"></asp:ListItem> <asp:ListItem Text="MAY" Value="MAY"></asp:ListItem> </asp:DropDownList> <br /> <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> <asp:View ID="View1" runat="server"> View 1<br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Values" HeaderText="Values" SortExpression="Values" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [ChartInfo] WHERE ([Name] LIKE '%' + @Name + '%')"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Name" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:Button ID="Button1" runat="server" CommandArgument="View2" CommandName="SwitchViewByID" Text="Go to View2" OnClick="Button1_Click" /> </asp:View> <asp:View ID="View2" runat="server"> View 2<br /> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Values" HeaderText="Values" SortExpression="Values" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [ChartInfo] WHERE ([Name] LIKE '%' + @Name + '%')"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Name" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:Button ID="Button2" runat="server" CommandArgument="View1" CommandName="SwitchViewByID" Text="Go to View 1" OnClick="Button2_Click" /> </asp:View> </asp:MultiView> </div>
protected void Button1_Click(object sender, EventArgs e) { SqlDataSource2.DataBind(); GridView2.DataBind(); } protected void Button2_Click(object sender, EventArgs e) { SqlDataSource1.DataBind(); GridView1.DataBind(); }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 24, 2016 5:43 AM
All replies
-
User61956409 posted
Hi NewKid1nTown,
As far as I know, we put a Button control that moves to a specific View in each View, so you could try to rebind GridView on Button Click event.
<div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> <asp:ListItem Text="FEB" Value="FEB"></asp:ListItem> <asp:ListItem Text="APR" Value="APR"></asp:ListItem> <asp:ListItem Text="MAY" Value="MAY"></asp:ListItem> </asp:DropDownList> <br /> <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> <asp:View ID="View1" runat="server"> View 1<br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Values" HeaderText="Values" SortExpression="Values" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [ChartInfo] WHERE ([Name] LIKE '%' + @Name + '%')"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Name" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:Button ID="Button1" runat="server" CommandArgument="View2" CommandName="SwitchViewByID" Text="Go to View2" OnClick="Button1_Click" /> </asp:View> <asp:View ID="View2" runat="server"> View 2<br /> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Values" HeaderText="Values" SortExpression="Values" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [ChartInfo] WHERE ([Name] LIKE '%' + @Name + '%')"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="Name" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:Button ID="Button2" runat="server" CommandArgument="View1" CommandName="SwitchViewByID" Text="Go to View 1" OnClick="Button2_Click" /> </asp:View> </asp:MultiView> </div>
protected void Button1_Click(object sender, EventArgs e) { SqlDataSource2.DataBind(); GridView2.DataBind(); } protected void Button2_Click(object sender, EventArgs e) { SqlDataSource1.DataBind(); GridView1.DataBind(); }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 24, 2016 5:43 AM -
User154499744 posted
Yes. That's what I had to do also. But what confuses me is that I didn't have to do that when the gridviews were on the active view. It's only when a gridview got moved to a view that isn't active, I had to use DataBind(). So it works, but a bit confusing.
Tuesday, May 24, 2016 6:34 PM