【问题标题】:update panel does not work更新面板不起作用
【发布时间】:2014-07-30 20:17:45
【问题描述】:

我试图获取下拉列表的值并将其插入到更新面板内的标签中,如下所示:

    <asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList><br />
            <asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
            <asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
            <asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>

在代码后面我有

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblEpisode.Text = DropDownList1.SelectedValue.ToString();
    }

但我不知道为什么它不更新标签!标签的文字保持不变!!!有人可以发现问题吗?

【问题讨论】:

    标签: asp.net asp.net-ajax updatepanel


    【解决方案1】:

    您需要在您的 Event 中调用更新面板的Update() 方法,如下所示。

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblEpisode.Text = DropDownList1.SelectedValue.ToString();
        udpTutorialDropDown.Update();
    }
    

    一切顺利!

    更新

    您必须在下拉列表中添加AutoPostBack="true" 属性。而且,忽略我之前的指示。 IE。调用更新面板的 Update() 方法。仅当您拥有 UpdateMode="Conditional"

    时才需要

    这应该工作:)

    【讨论】:

    • 我为什么要更新我的下拉列表?我试图从中获取价值并将其传递给标签!同样,当我将 SelectedIndexChanged 更改为 DropDownList1_SelectedIndexChanged 时,我会收到以下错误:在 UpdatePanel 'udpTutorialDropDown' 中的触发器的关联控件 'DropDownList1' 上找不到名为 'DropDownList1_SelectedIndexChanged' 的事件
    • 您不是在更新您的 DDL,而是在更新更新面板中的内容/控件。是的,我的错误应该是 EventName="SelectedIndexChanged",对不起
    • 而且,如果您的更新面板不在另一个更新面板中,您必须设置它的 UpdateMode 属性。例如。 UpdateMode="条件"
    • 实际上它在另一个更新面板中。因为有一个主更新面板,我在其中加载了一个 ascx 文件,在 ascx 文件中我有更新面板“udpTutorialDropDown”
    • 那么您将不需要 UpdateMode="Conditional"。只需在事件处理程序中调用 .Update() 方法。试一试
    【解决方案2】:

    尝试删除触发器以获得类似的东西

    <br />
    <asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
    <asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
            <asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
            <asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
        </ContentTemplate>
    

    【讨论】:

    • 如果我删除了触发器,那么我应该将下拉列表放在更新面板中,但它仍然无法工作
    • 您使用的是“上传者”表单吗?已知此表单与更新面板有冲突。
    【解决方案3】:

    您还必须将下拉列表放入现有的更新面板...

    【讨论】:

      猜你喜欢
      • 2012-08-30
      • 1970-01-01
      • 2017-01-31
      • 2010-10-13
      • 2018-05-05
      • 2011-11-07
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多