locked
Gridview in Non Active View of Multiview Not Getting Refreshed RRS feed

  • Question

  • User154499744 posted

    It would be easier to explain with this example. Would someone please try this code and read through my explanation below?

    Here is the page markup

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MutiviewTest.aspx.cs" Inherits="Fund_PnL_Allocations_And_Returns.MutiviewTest" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>MultiviewTest</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList" runat="server" AutoPostBack="true"
                OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
                <asp:ListItem>California</asp:ListItem>
                <asp:ListItem>New York</asp:ListItem>
                <asp:ListItem>Nevada</asp:ListItem>
                <asp:ListItem>Utah</asp:ListItem>
            </asp:DropDownList>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Go to View 1</asp:LinkButton>
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Go to View 2</asp:LinkButton>
            <hr />
            <%--<asp:MultiView ID="MultiView" runat="server">
                <asp:View ID="View1" runat="server">--%>
                    <h2>View 1</h2>
                    <asp:Label ID="Label1" runat="server" Width="2.0in" BackColor="LightGray">DEFAULT VALUE</asp:Label><br />
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource">
                        <Columns>
                            <asp:BoundField DataField="ItemValue" HeaderText="ItemValue" ReadOnly="True" SortExpression="ItemValue" />
                            <asp:BoundField DataField="LengthOfItemValue" HeaderText="LengthOfItemValue"
                                ReadOnly="True" SortExpression="LengthOfItemValue" />
                        </Columns>
                    </asp:GridView>
    <%--            </asp:View>
                <asp:View ID="View2" runat="server">--%>
                    <h2>View 2</h2>
                    <asp:Label ID="Label2" runat="server" Width="2.0in" BackColor="LightGray">DEFAULT VALUE</asp:Label><br />
                    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource">
                        <Columns>
                            <asp:BoundField DataField="ItemValue" HeaderText="ItemValue" ReadOnly="True" SortExpression="ItemValue" />
                            <asp:BoundField DataField="LengthOfItemValue" HeaderText="LengthOfItemValue"
                                ReadOnly="True" SortExpression="LengthOfItemValue" />
                        </Columns>
                    </asp:GridView>
    <%--            </asp:View>
            </asp:MultiView>--%>
    
            <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:FinanceDB %>"
                SelectCommand="SELECT @Item AS ItemValue, LEN(@Item) AS LengthOfItemValue">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList" Name="Item" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>
    
        </div>
        </form>
    </body>
    </html>

    And here is the code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Fund_PnL_Allocations_And_Returns
    {
        public partial class MutiviewTest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                //MultiView.ActiveViewIndex = 0;
            }
    
            protected void LinkButton2_Click(object sender, EventArgs e)
            {
                //MultiView.ActiveViewIndex = 1;
            }
    
            protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
            {
                Label1.Text = DropDownList.SelectedValue;
                Label2.Text = DropDownList.SelectedValue;
            }
        }
    }

    If I conveyed everything correctly, you should get a page with a dropdown list with 2 identical sets of label and gridview that give you basic information about the item you chose. Try changing the dropdown list item and you should see the labels and the gridviews getting updated.

    Once you confirmed this, please try this. This is where I am confused. Uncomment the views in the markup and the code behind for activating the views on the 2 link buttons. After you do this, you should get one set of the label and the gridview on View 1 and the other set of the label and the gridview on View 2. Now change the dropdown list item. How do the labels and the gridviews behave for you? I am noticing that the both labels get updated as the dropdown list item is changed. But the gridview doesn't behave the way it did before the multiview was added. Now, only the gridview that is on the active view gets updated. Do you get this behavior also?

    Thursday, May 26, 2016 5:36 PM

All replies

  • User1559292362 posted

    Hi NewKid1nTown,

    It would be easier to explain with this example. Would someone please try this code and read through my explanation below?

    I create a demo by using your code.

    How do the labels and the gridviews behave for you?

    Two labels have changed the value with dropdownlist's value.

    But the gridview doesn't behave the way it did before the multiview was added. Now, only the gridview that is on the active view gets updated. Do you get this behavior also?

    As I said, two labels have changed the value with dropdownlist's value. we could only see the changing value on the active view, but the another view has changed too, which we could see the changing value when we switch it to active.

    Best regards,

    Cole Wu

    Friday, May 27, 2016 5:19 AM
  • User154499744 posted

    Cole, I want to make sure I understand what you see when you add the multiview. The reason why I have the label on each view is to verify that the changes in the dropdown is being seen by each view, regardless of if a view is active or not. But I am not sure if I observe the same for the gridview.

    (1) When the page first loads, the dropdown item is selected on the first item. (In my case, its California.) When I go into View 1 and View 2, the label and the gridview on both views are set to California. Nothing unusual here.

    (2) This next part is where I am confused. While being on one view (say view 1), change the drop down to something else, say New York. The label and the gridview on view 1 changes accordingly. But what do you see in the gridview when you go to the other view? Did it change or did it stay on the previous value? For me, the gridview on the inactive view does not change, which is what is confusing to me.

    I can add a Databind() on the click event of the link buttons that take me into each view to make sure the gridviews are updated, but this seems unnecessary.

    Friday, May 27, 2016 12:51 PM
  • User1559292362 posted

    Hi NewKid1nTown,

    (1) When the page first loads, the dropdown item is selected on the first item. (In my case, its California.) When I go into View 1 and View 2, the label and the gridview on both views are set to California. Nothing unusual here.

    When the page first loads, the dropdown item is selected on the first item. (In my case, its California.) When I go into View 1 and View 2, the label' text is Default Value. please see the images above.

    (2) This next part is where I am confused. While being on one view (say view 1), change the drop down to something else, say New York. The label and the gridview on view 1 changes accordingly. But what do you see in the gridview when you go to the other view? Did it change or did it stay on the previous value? For me, the gridview on the inactive view does not change, which is what is confusing to me.

    please see the image above, it changes when I go to the other view. I'm not sure what causes that your app doesn't change the value, could you please share a simple sample about the issue via OneDrive, so that we could reproduce your issue on my side and try to find a solution to solve it.

    Best regards,

    Cole Wu

    Saturday, May 28, 2016 1:42 AM
  • User154499744 posted

    I can't see the image you posted. Well, this is difficult to communicate. I decided to get around this problem by putting Databind() in the click event of linkbutton that takes me into each view. I didn't think putting Databind() would be necessary because changing the drop down should refresh the gridviews, but maybe I am wrong about that assumption. But whatever the case maybe, it seems doing Databind() each time I go into the view ensures my gridview is reflecting the dropdown item.

    Friday, June 3, 2016 2:53 PM