【问题标题】:ASP.NET Label doesn't update when CascadingDropDown selectedindex is changed更改 CascadingDropDown selectedindex 时 ASP.NET 标签不更新
【发布时间】:2014-10-03 17:04:53
【问题描述】:

当 CascadingDropDown 索引更改时,我无法更新 asp.net 标签。请看下面的代码。

aspx代码:

<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>

<asp:DropDownList ID="ddl2" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddl2IndexChanged"></asp:DropDownList>

<asp:UpdatePanel>
    <ContentTemplate>
        <asp:Label ID="lbl1" Text="HelloWorld" runat="server"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddl2" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>    

<asp:DropDownList ID="ddl3" runat="server"></asp:DropDownList>

<ajaxToolKit:CascadingDropDown ID="cdd1" runat="server" Category="MachineType"
TargetControlID="ddl2" ParentControlID="ddl1" PromptText="Select Machine Type"
LoadingText="Loading Machine Types" ServicePath="CascadingDropDown.asmx"
ServiceMethod="getMachineTypes"></ajaxToolKit:CascadingDropDown>

<ajaxToolKit:CascadingDropDown ID="cdd2" runat="server" Category="Machine"
TargetControlID="ddl3" ParentControlID="ddl2" PromptText="Select Machine"
LoadingText="Loading Machines" ServicePath="CascadingDropDown.asmx"
ServiceMethod="getMachines"></ajaxToolKit:CascadingDropDown>

后面的代码:

protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
    lbl1.Text = DateTime.Now.ToString();
}

AutoEventWireup 为真。 EnableEventValidation 为真。验证请求为真。 EnablePageMethods 为真。 EnablePartialRendering 为真。 ddl2 的 AutoPostBack 为真。 DropDowns 得到完美更新。但标签没有。 ddl2 的 SelectedIndexChanged 不会触发。我猜是因为 CascadingDropDown。

我还尝试编写一个静态 WebMethod 并从 JavaScript 调用它。

在后面的代码中:

[WebMethod]
public static void UpdateLabel()
{
    Page page = (Page) HttpContext.Current.Handler;
    Label lbl = (Label) page.FindControl("lbl1"); // lbl is always null.
    lbl.Text = DateTime.Now.ToString();
}

在 JavaScript 中:

function updateLabel() {
    PageMethods.UpdateLabel();
}

在 aspx 中:

<asp:DropDownList ID="ddl2" runat="server" onchange="javascript:updateLabel()">
</asp:DropDownList>

通过上述操作,我可以调用静态 WebMethod。但是,我找不到标签。它总是返回 null。 :(

我怎样才能做到这一点?我错过了什么吗?

谢谢!

【问题讨论】:

    标签: asp.net label updatepanel postback cascadingdropdown


    【解决方案1】:

    让它像这样工作。

    在 JavaScript 中:

    function updateLabel() {
        PageMethods.UpdateLabel(updateLabelName);
    }
    
    function updateLabelName(response) {
        var lbl = document.getElementById('<%=lbl1.ClientID%>');
        lbl.innerHTML = response;
    }
    

    在后面的代码中:

    [WebMethod]
    public static string UpdateLabel()
    {
        return DateTime.Now.ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多