1

我正在尝试创建一个也具有客户端对象的自定义 AJAX 用户控件。我按照此链接中的说明操作:https ://msdn.microsoft.com/en-us/library/bb398906.aspx ,但我一直遇到问题。

在我的构造函数中,“元素”总是未定义的。这是我的整个 JS 文件:

Type.registerNamespace("UserControls");

/**
 * The class for the stock window user control.
 * @param {element} element - The stock window element.
 */
UserControls.StockWindow = function(element)
{
    UserControls.StockWindow.initializeBase(this, [element]);
};

UserControls.StockWindow.registerClass("UserControls.StockWindow", Sys.UI.Control);

这里是我的 ASPX 供参考:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StockWindow.ascx.cs" Inherits="StockPicker.UserControls.StockWindow"%>
<%@ Register tagprefix="custom" src="~/UserControls/ModalWindow.ascx" tagname="ModalWindow" %>

<div id="<%= ID %>">
    <%-- Hide the modal window initially. --%>
    <div id="ModalWindowContainer" style="display: none;" runat="server">
        <custom:ModalWindow ID="ModalWindowForStock" runat="server" />    
    </div>
</div>

这是我的 ASPX.CS:

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Services;

namespace StockPicker.UserControls
{
    /// <summary>
    /// A class to hold pop-up windows describing stocks.
    /// </summary>
    public partial class StockWindow : UserControl, IScriptControl
    {
        #region Protected Methods
        /// <summary>
        /// Handles the logic to execute when the page
        /// </summary>
        /// <param name="sender">The object that triggered the load event.</param>
        /// <param name="eventArguments">The event arguments for the load event.</param>
        protected void Page_Load(object sender, EventArgs eventArguments)
        {
        }

        protected override void OnPreRender(EventArgs eventArguments)
        {
            ScriptManager.GetCurrent(Page).RegisterScriptControl(this);
            base.OnPreRender(eventArguments);
        }

        protected override void Render(HtmlTextWriter htmlWriter)
        {
            ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
            base.Render(htmlWriter);
        }
        #endregion

        #region IScriptControl Interface Methods
        /// <summary>
        /// Creates a description for how to create the control in the JavaScript.
        /// </summary>
        /// <returns>The script descriptors.</returns>
        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            // DEFINE HOW TO CREATE THE CONTROL.
            const string QualifiedJavaScriptName = "UserControls.StockWindow";
            ScriptControlDescriptor controlCreationDescription = new ScriptControlDescriptor(
                QualifiedJavaScriptName,
                this.ClientID);

            // RETURN HOW TO CREATE THE JAVASCRIPT OBJECT.
            List<ScriptControlDescriptor> controlDescriptors = new List<ScriptControlDescriptor>();
            controlDescriptors.Add(controlCreationDescription);
            return controlDescriptors;
        }

        /// <summary>
        /// Gets the JavaScript file for the control.
        /// </summary>
        /// <returns>The JavaScript file for the control.</returns>
        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            // Return the JavaScript file for the control.
            ScriptReference controlScriptFile = new ScriptReference();
            controlScriptFile.Path = ResolveClientUrl("~/StockWindow.js");
            List<ScriptReference> controlScripts = new List<ScriptReference>();
            controlScripts.Add(controlScriptFile);
            return controlScripts;
        }
        #endregion
    }
 }
4

1 回答 1

2

经过大量调试后,我发现由于某种原因在 ASPX 中创建控件时需要使用“ClientID”而不是“ID”。希望这对某人有帮助!

于 2018-05-25T01:12:32.217 回答