【问题标题】:GridView not updating from SelectedIndexChange event of a DropDownList within an UpdatePanelGridView 未从 UpdatePanel 中 DropDownList 的 SelectedIndexChange 事件更新
【发布时间】:2014-04-12 06:51:41
【问题描述】:

我的问题:

UpdatePanel 包含一个 DropDownList 和一个 GridView。 GridView 根据存储在 Label 中的值和从 DropDownList 中选择的值进行填充。它不会在 PageLoad 上填充;它仅在存在查询字符串或按下按钮时填充,因此!IsPostBack 中没有代码。 GridView 填充了 DropDownList 的初始 SelectedValue,我希望 GridView 在 DropDownList 的 SelectedValue 更改时重新填充。

问题是……它确实改变了!但只有一次。 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>

UpdatePanel 的 UpdateMode 是有条件的,因此它会在其子项 PostBack 时更新。 ChildrenAsTriggers 为 true,因此子项导致它更新。

theDropDownList 的 AutoPostBack 为 true,因此它会在更改时回发。 EnableViewState 为真(假使它甚至不加载)。 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(); 方法和不使用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 背后的事件。

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu updatepanel


    【解决方案1】:

    什么是价值标签?好像没变过?

    为什么不直接使用 ((DropDownList)sender).selectedValue 来获取重新绑定 gridview 的值?

    凯夫

    【讨论】:

    • Label 的值不会改变。只有 DropDownList 的值会改变。我很好地提取了这两个值,并且 GridView 的数据进来并正确绑定,但它只是没有显示。
    【解决方案2】:

    从下拉列表中获取价值似乎没有任何问题,原因可能在于您提供组合的数据源

    在页面加载事件的回发中放置组合负载

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

    如果不是这样,您可以在此处放置页面加载事件代码 谢谢...

    【讨论】:

    • GridView 不会在 PageLoad 上加载。它仅在存在查询字符串或按下按钮时加载。所以没有 !IsPostBack 的代码。
    【解决方案3】:

    我仍然不确定为什么它会更新一次,然后再也不更新,但问题已通过将 DropDownList 和 GridView 放在它们自己的 UpdatePanel 中而不是同一个更新面板中得到解决。

    【讨论】:

      【解决方案4】:

      尝试向更新面板添加触发器

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

      或者之前的类似的东西

      </asp:UpdatePanel>
      

      【讨论】:

        猜你喜欢
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多