从 IE 中的 ASP.net 树节点调用 Jquery

发布于 2024-12-29 10:57:21 字数 1094 浏览 2 评论 0原文

我在从 ASP.NET 树节点控件调用 jquery 函数时遇到问题。 这是 aspx 文件中的树节点代码。 我使用的方法是在“navigateUrl”属性中放置一个javascript链接。

下面是代码:

<asp:TreeNode Value="Level1" 
                NavigateUrl="javascript:$('#mainContentContainer').load('report.aspx?level=1');"
                Text="Level 1"
                Target="Content"
                expanded="false">

这在 CHROME 中完美运行。当我单击树节点“Level 1”时,report.aspx 内容会按预期加载到 mainContentContainer div 中。

然而,在 Internet Explorer(v8 和 9)中,它只是打开另一个浏览器窗口并尝试加载 JavaScript 链接,就好像它是 URL 一样。由于某种原因,地址栏仅显示链接的这一部分:“javascript:$('”并且内容窗口给出此错误“Internet Explorer无法显示网页”

我已经搜索了这个问题,我发现的所有解决方案都建议使用进行 JavaScript 调用的“OnClick”属性或将“元素的“target”属性设置为“_self”

这些选项都不适合我。我无法为标签提供“OnClick”属性,因为它不是此 asp.net

类型 的有效属性。 “System.Web.UI.WebControls.TreeNode”没有名为“onclick”的公共属性 是浏览器中出现的错误(作为标准 .net 错误网页返回)

我尝试了 target="_self" 选项,唯一的变化是打开的新浏览器窗口出现错误。当前浏览器只会显示[object Object],地址栏没有任何变化。

我读到的另一个解决方案是在 javascript 调用之后放置“retun false”,但这会给出“函数之外的返回语句”javascript 错误。

请告诉我是否有从 asp.net 树节点控件调用 jquery.load 的解决方案。

I am having problems calling a jquery function from an asp.net treenode control.
Here is the treenode code in the aspx file.
The method I am using is to put a javascript link in the "navigateUrl" attribute.

Below is the code:

<asp:TreeNode Value="Level1" 
                NavigateUrl="javascript:$('#mainContentContainer').load('report.aspx?level=1');"
                Text="Level 1"
                Target="Content"
                expanded="false">

This works perfectly in CHROME. The report.aspx content loads into the mainContentContainer div as expected when I click on the treenode "Level 1".

In Internet Explorer however (v8 and 9), it just opens another browser window and attempts to load the JavaScript link as if it were a URL. For some reason the address bar only shows this portion of the link: "javascript:$('" and the content window gives this error "Internet Explorer cannot display the webpage"

I have searched for this problem and all solutions I found suggest either using the "OnClick" attribute to make the JavaScript call or to set the "target" attribute of the " element to "_self"

Neither of these options worked for me. I can not give the tag an "OnClick" attribute because it is not a valid attribute for this asp.net control.

Type 'System.Web.UI.WebControls.TreeNode' does not have a public property named 'onclick'
is the error that appears in the browser (returned as the standard .net error webpage)

I tried the target="_self" option and the only change was that instead of a new browser window opening with an error. The current browser will just display [object Object] without any change in the address bar.

Another solution I read was to put "retun false" after the javascript call but this gives a "return statement outside of function" javascript error.

Please let me know if there is a solution to call jquery.load from an asp.net treenode control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2025-01-05 10:57:21

您可以考虑以下解决方案,即在文档加载后或树渲染后使用 jQuery 来绑定单击事件处理程序。因此,您只需使用报告的实际 url,而不是 NavigateUrl 属性中的 javascript:

<asp:TreeNode Value="Level1" 
  NavigateUrl="report.aspx?level=1"
  Text="Level 1"
  Target="Content"
  Expanded="false">

通常,这会导致导航离开该页面并转到报告页面。但我们可以使用以下 jQuery 代码将单击事件处理程序添加到链接来防止这种情况发生:

$("#tree a").click(function(e) {
  // Get the url (report.aspx?level=...)
  var url = $(this).attr("href");

  // Load the report
  $('#mainContentContainer').load(url);

  // Don't navigate away
  e.preventDefault();
});

您可能必须使用更具体的选择器来仅将此单击处理程序添加到树中特定级别的节点。在这种情况下,您可以使用 LevelStyles 然后在 jQuery 中使用不同的选择器:

<div id="tree">
  <asp:TreeView runat="server">
    <LevelStyles>
      <!-- Add class report only to nodes at the first level -->
      <asp:TreeNodeStyle CssClass="report"/>
    </LevelStyles>
    ...
  </asp:treeView>
</div>

现在你的 jQuery 选择器将是:

$("#tree .report a").click(function(e) { ... });

You can consider the following solution, which is to use jQuery after the document is loaded or after the tree is rendered to bind a click event handler. So instead of the javascript: inside the NavigateUrl attribute, you just use the actual url of the report.

<asp:TreeNode Value="Level1" 
  NavigateUrl="report.aspx?level=1"
  Text="Level 1"
  Target="Content"
  Expanded="false">

Normally, this would result in a navigation away from the page and to the report page. But we can prevent this using the following jQuery code to add the click event handler to the link:

$("#tree a").click(function(e) {
  // Get the url (report.aspx?level=...)
  var url = $(this).attr("href");

  // Load the report
  $('#mainContentContainer').load(url);

  // Don't navigate away
  e.preventDefault();
});

You may have to use a more specific selector to only add this click handler to nodes at a certain level in your tree. In that case, you can give these nodes a class using LevelStyles and then use a different selector in jQuery:

<div id="tree">
  <asp:TreeView runat="server">
    <LevelStyles>
      <!-- Add class report only to nodes at the first level -->
      <asp:TreeNodeStyle CssClass="report"/>
    </LevelStyles>
    ...
  </asp:treeView>
</div>

Now your jQuery selector would be:

$("#tree .report a").click(function(e) { ... });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文