在 UpdatePanel 中的 ASP 文本框上自动回发不起作用

AutoPostBack on ASP TextBox in UpdatePanel not working

本文关键字:不起作用 UpdatePanel 中的 ASP 文本      更新时间:2023-09-26

我有一个 TextBox 控件,该控件在设置背景颜色的窗体上带有 ColorPickerExtender。 我希望当用户更改颜色时发生两件事:

  • 控件的颜色更改
  • 我得到一个即时的 AJAX 事件,所以我可以在页面的其他部分进行更改

颜色更改在 JavaScript onColorChanged()下工作正常。 我的事件处理程序还会在完全回发和从其他控件生成的部分/AJAX 回发期间触发。 但是,它本身不会立即生成回发。

以下是.aspx文件的相关行:

<%@ Page Title="" Language="C#" MasterPageFile="~/FingerTipDisplay.Master" AutoEventWireup="true" CodeBehind="EditorLayoutv3.aspx.cs" Inherits="FingerTipDisplay.config.EditorLayoutv3" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit" %>

<asp:UpdatePanel ID="upGeneralLayoutData" runat="server">
    <ContentTemplate>
        <asp:Label runat="server" Text="Background color:" CssClass="ContentBodyText" ToolTip="Select the background color for this layout"></asp:Label>
        <asp:TextBox ID="txtLayoutBackgroundColor" runat="server" ToolTip="Select the background color for this layout" CssClass="ColorPickerExtenderTextBox" Width="50" OnTextChanged="txtLayoutBackgroundColor_TextChanged" AutoPostBack="True"></asp:TextBox>
        <ajaxControlToolkit:ColorPickerExtender TargetControlID="txtLayoutBackgroundColor" runat="server"  OnClientColorSelectionChanged="onColorChanged" />
    </ContentTemplate>
</asp:UpdatePanel>

以下是来自站点管理员的脚本管理器:

<asp:ScriptManager ID="smFingerTipDisplay" runat="server">
</asp:ScriptManager>

下面是事件处理程序:

protected void txtLayoutBackgroundColor_TextChanged(object sender, EventArgs e)
{
    moLayout.BackgroundColor = txtLayoutBackgroundColor.Text;
    if (moLayout.BackgroundColor.Substring(0, 1) != "#")
    {
        moLayout.BackgroundColor = "#" + moLayout.BackgroundColor;
    }
    // TODO: update preview image
}

下面是 JavaScript:

onColorChanged = function (oSender) {
    /// <summary>Callback to use for when a color picker extender changes colors.  This
    /// sets the foreground and background of the TextBox control to the selected color.
    /// <param name='oSender' type='Object'>The control that changed</param>
    oSender.get_element().style.color = "#" + oSender.get_selectedColor();
    oSender.get_element().style.backgroundColor = "#" + oSender.get_selectedColor();
}

解决了这个问题。 事实证明,当ColorPickerExtender更改相应TextBox中的文本时,它根本不会引发TextChanged事件。 但是,手动键入TextBox会产生所需的效果,因此我只是将其添加到onColorChanged处理程序中:

oSender.get_element().onchange();

现在它起作用了。 感谢大家的帮助。