簡體   English   中英

GridView無法從UpdatePanel中的DropDownList的SelectedIndexChange事件更新

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

我的問題:

UpdatePanel包含一個DropDownList和一個GridView。 GridView基於存儲在Label中的值和從DropDownList中選擇的值進行填充。 它不會填充在PageLoad上。 它僅在存在查詢字符串或按下按鈕時填充,因此!IsPostBack沒有代碼。 GridView使用DropDownList的初始SelectedValue填充,並且我希望當DropDownList的SelectedValue更改時重新填充GridView。

事情是...它在改變! 但是只有一次。 GridView最初使用DropDownList的默認SelectedValue的值填充數據。 更改SelectedValue時,GridView中的數據也會更改。 但是只有一次。 第二次或更多次更改DropDownList值后,什么都沒有發生。

<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的UpdateMode是有條件的,因此它在其子項PostBack時更新。 ChildrenAsTriggers是真實的,因此孩子會導致它更新。

theDropDownList的AutoPostBack為true,因此它將在更改時回發。 EnableViewState為true(false甚至無法加載)。 SelectedIndexChange鏈接到正確的函數,並且我知道在調試模式下通過在其中插入一個中斷來調用它。

這是SelectedIndexChanged方法:

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();
}

我已經嘗試了無論有沒有updatepanel.Update(); 方法,結果是相同的:theDropDownList值的第一個更改有效,而隨后的每個更改均不執行任何操作。

實際填充GridView的函數非常簡單:

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();
}

有任何想法嗎? 我一定會誤解UpdatePanel背后的事件。

什么是valueLabel? 看起來它從未改變過嗎?

為什么不只使用((DropDownList)sender).selectedValue來獲取您的值以重新綁定網格視圖?

凱夫

從下拉列表中獲取價值似乎沒有任何問題,原因可能在於您的組合的給定數據源中

將組合加載放置在頁面加載事件的回發中!

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

如果不是這樣,那么您可以在此處放置頁面加載事件代碼嗎,謝謝...

我仍然不確定為什么它會一次又一次地更新,但是通過將DropDownList和GridView放在自己的UpdatePanels中而不是在同一UpdatePanels中,已解決了該問題。

嘗試將觸發器添加到更新面板

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

或類似的東西之前

</asp:UpdatePanel>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM