繁体   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