将代码移至 DotNetNuke 模块后 ClientID 无效
我正在将现有应用程序移植到 DotNetNuke 模块,并发现了奇怪的行为。 我在创建 javascript 时使用 ClientID,以便代码可以识别 HTML 元素。 通常这会创造一个类似这样的值:
"g_ctl00_ctl01_ctl00_ctl00_txtSearch"
我们都已经见过这个一百万次了,对吧? 好吧,将此代码移植到 DotNetNuke 模块并首次运行后,ClientID 属性将返回以下内容:
"dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl00_ctl00_txtSearch"
注意逗号和空格。 这会导致各种 JavaScript 错误。 例如,ASP.NET Login 控件现在输出无效的 javascript:
var dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired = document.all ? document.all["dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired"] : document.getElementById("dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired");
我的模块的程序集名称为 TechInfoSystems.GalleryServerPro.dll,默认命名空间为 GalleryServerPro.Web,用户控件位于名为 Gallery.cs 的类中,因此这部分解释了一些额外文本的来源,但是为什么它在那里? 我该如何确保 ClientID 不会输出逗号、空格或其他可能导致 JavaScript 出现问题的字符?
谢谢, 罗杰
I am porting an existing application to a DotNetNuke module and discovered bizarre behavior. I use ClientID when creating javascript so that the code can identify the HTML elements. Normally that creates a value something like this:
"g_ctl00_ctl01_ctl00_ctl00_txtSearch"
We've all seen this a million times, right? Well, after porting this code to a DotNetNuke module and running it for the first time, the ClientID property is returning this:
"dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl00_ctl00_txtSearch"
Notice the comma and the space. This is causing all kinds of javascript errors. For example, the ASP.NET Login control is now outputting invalid javascript:
var dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired = document.all ? document.all["dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired"] : document.getElementById("dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired");
My module has an assembly name of TechInfoSystems.GalleryServerPro.dll, the default namespace is GalleryServerPro.Web, and the user control is in a class named Gallery.cs, so that partially explains where some of that extra text is coming from, but why is it there? And what can I do to ensure that ClientID does not output commas, spaces, or other characters that can cause problems in javascript?
Thanks,
Roger
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我(有点)想通了。 我需要向要添加到 DotNetNuke 页面的用户控件添加一个 ID。 这是主视图用户控件 - 继承 PortalModuleBase 的控件。 用户控件完全包含在代码隐藏类中(无 .ascx),因此将其添加到 Init 事件中修复了所有问题:
this.ID = "gsp";
我的理论是,当以下情况成立时,就会出现问题:
“视图”用户控件仅使用代码隐藏文件(无 .ascx 文件)
用户控件未指定 ID。
注册不带 .ascx 的模块时,必须在模块定义中指定类和程序集。 就我而言,它是“GalleryServerPro.Web.Gallery,TechInfoSystems.GalleryServerPro”。 这看起来非常接近 DNN 最终插入到 clientID 字符串中的文本。 在用户控件中没有分配 ID 的情况下,DNN(或 ASP.NET)必须获取该字符串并使用它来构建 clientID。
不确定这是否是 DNN 错误,但我很高兴弄清楚它,现在可以继续...
I (sort of) figured it out. I needed to add an ID to the user control that is being added to the DotNetNuke page. This is the main view user control - the one that inherits PortalModuleBase. The user control is contained entirely in a code-behind class (no .ascx), so adding this to the Init event fixed everything:
this.ID = "gsp";
My theory is that the issue happens when these are true:
The "view" user control uses only the code-behind file (no .ascx file)
The user control does not have an ID specified.
When registering a module without the .ascx, one must specify the class and assembly in the Module Definitions. In my case, it is "GalleryServerPro.Web.Gallery, TechInfoSystems.GalleryServerPro". That looks very close to the text DNN ends up inserting into the clientID string. DNN (or ASP.NET) must be taking that string and using it to build the clientID in the absence of an ID assigned in the user control.
Not sure if this is a DNN bug, but I am happy to figure it out and now can move on...
您可以尝试使用类来引用您的控件吗?
有很多好的函数可以完成这样的事情。 我会推荐 jquery 作为开始,让 javascript 的世界变得更容易。
You could try referencing your control using a class instead?
There are plenty of good functions out there for doing such a thing. I would recommend jquery for a start, to make all things javascript a world easier.
我不知道为什么会发生这种情况(但我从未使用过 DNN),
您可以做的是自己控制受影响的服务器控件的 ClientID,例如,看起来您正在使用带有服务器 ID 的 TextBox 控件= txtSearch,您可以做的是创建一个继承自 TextBox 的类,并将其 ClientId 设置为等于服务器 ID(假设只有一个名为 txtSearch 的控件)
请参阅此处
这可能是DNN甚至会覆盖这一点,但可能值得一试
I have NO idea why this is happening (but then I've never used DNN)
what you could do is take control of the ClientID yourself for the affected servercontrols, for instance, it looks like you're using a TextBox control with server ID = txtSearch, what you could do is make a class that inherits from TextBox and setting its ClientId to be equal to the Server ID (assuming there's only one control called txtSearch)
see here
It might be that DNN would override even that, but it might be worth a shot