简体   繁体   中英

GridView not updating from SelectedIndexChange event of a DropDownList within an UpdatePanel

My problem:

An UpdatePanel holds a DropDownList and a GridView. The GridView populate baseds on a value that is stored in a Label and a value that is selected from a DropDownList. It does NOT populate on PageLoad; it only populates when a query string is present or when a button is pressed, so there is no code in the !IsPostBack . The GridView is populated with the initial SelectedValue of the DropDownList, and I want the GridView to repopulate when the SelectedValue of the DropDownList changes.

The thing is... IT DOES CHANGE! But only once. The GridView initially populates with data using the value of the default SelectedValue of the DropDownList. When the SelectedValue is changed, the data in the GridView changes. But only once. After changing the DropDownList value the second time or more, nothing happens.

<asp:UpdatePanel runat="server" ID="theUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="True">
    <ContentTemplate>
        <asp:DropDownList runat="server" ID="theDropDownList" OnSelectedIndexChanged="theDropDownList_OnSelectedIndexChanged" EnableViewState="true" AutoPostBack="true" />
        <asp:GridView ID="theGridView" runat="server" AutoGenerateColumns="False">
            <Columns>
                ...
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

theUpdatePanel's UpdateMode is Conditional, so it updates when its children PostBack. ChildrenAsTriggers is true, so children cause it to update.

theDropDownList's AutoPostBack is true, so it will postback on change. EnableViewState is true (false makes it not even load). SelectedIndexChange links to the right function, and I know that it's getting called by putting a break inside it during debug mode.

Here is the SelectedIndexChanged method:

public void theDropDownList_OnSelectedIndexChanged(object sender, EventArgs e)
{
    //get value that is stored in theLabel - I know this value is correct every time
    int theValueFromTheLabel = Int32.Parse(theLabel.Text);

    //populate theGridView with data from the DB
    theGridView_Populate(theValueFromTheLabel);

    //update theUpdatePanel (it works for the first change whether this line is here or not)
    theUpdatePanel.Update();
}

I've tried both with and without the updatepanel.Update(); method, and it's the same result: the first change of theDropDownList's value works, and every following change does nothing.

The function to actually populate the GridView is pretty simple:

protected void theGridView_Populate(int theValueFromTheLabel)
{
    //get value from theDropDownList - this value does change when theDropDownList's value changes
    int theValueFromTheDropDownList = Int32.Parse(theDropDownList.SelectedValue);

    //get data from DB - the data here does change every time theDropDownList's value changed
    ComplexClassController controller = new ComplexClassController();
    List<ComplexClass> data = controller.GetData(theValueFromTheLabel, theValueFromTheDropDownList);

    //load theGridView - this changes the data, but doesn't refresh theGridView to be able to see it
    theGridView.DataSource = data;
    theGridView.DataBind();
}

Any ideas? I must be misunderstanding the events behind UpdatePanel.

what is valueLabel? It doesn't look like it ever gets changed?

Why not just do ((DropDownList)sender).selectedValue to get your value to redatabind the gridview with?

Kev

It does not seems to be any problem of getting value from dropdown list the reason might be in your giving data source of your combo

place load of combo in !postback of page load event some thing like this

 protected void Page_Load(object sender, EventArgs e)
    {
    if(!ispostback){
       Loadcomobo();
    }
    }

if its not the case so can you place your page load event code here thanks...

我仍然不确定为什么它会一次又一次地更新,但是通过将DropDownList和GridView放在自己的UpdatePanels中而不是在同一UpdatePanels中,已解决了该问题。

Try adding a trigger to the update panel

<triggers>
    <asyncpostback ControlID="theDropDownList" />
</triggers>

Or something like that before the

</asp:UpdatePanel>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM