gpt4 book ai didi

javascript - 自定义 AJAX 用户控制 JavaScript 构造函数 "element"未定义

转载 作者:行者123 更新时间:2023-12-03 01:51:55 25 4
gpt4 key购买 nike

我正在尝试创建一个也有客户端对象的自定义 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
}
}

最佳答案

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

关于javascript - 自定义 AJAX 用户控制 JavaScript 构造函数 "element"未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com