ASP.NET - 让jquery混乱我的ASPX文件的最佳方法是什么?

时间:2021-10-25 20:08:57

I'm starting to use more and more jquery. I'm working with dialogs, and each dialog (including javascript) is taking upwards of 20-30 lines of code. There's a few pages where I'll have over 5 dialogs. This gets ugly fast. I haven't even begun to get into sliding interfaces or calendar controls yet.

我开始使用越来越多的jquery。我正在使用对话框,每个对话框(包括javascript)占用的代码超过20-30行。有几页我将有超过5个对话框。这变得很难看。我还没有开始进入滑动界面或日历控件。

I have more of a PHP background... and in these situations i'd be moving things to an "include" file. I can see that working for items such as jquery-dialogs in ASP.NET, but when I start adding jquery features to actual ASP.NET form items.. that won't be an option (as far as I know).

我有更多的PHP背景......在这些情况下,我会把事情转移到“包含”文件。我可以看到在ASP.NET中使用诸如jquery-dialogs之类的项目,但是当我开始将jquery功能添加到实际的ASP.NET表单项时...这不是一个选项(据我所知)。

How can I keep the extra javascript and extra floating divs from cluttering up my ASPX files too bad?

如何保持额外的javascript和额外的浮动div使我的ASPX文件混乱太糟糕?

I'm prepared to partition the ASPX file into well documented, distinct sections... this just results in a very "tall" file. Any better solutions?

我准备将ASPX文件划分为记录良好的不同部分...这只会产生一个非常“高”的文件。更好的解决方案?

Thanks.

3 个解决方案

#1


Let's see...

  1. Since you are using jQuery, you must know the script src syntax already to include javascript files, so I assume this is not the problem :).

    既然你正在使用jQuery,你必须知道脚本src语法已经包含javascript文件,所以我认为这不是问题:)。

  2. For those pesky crazy looking control ID that ASP.NET create... cache them in a JavaScript global variable at the end of the page... like:

    对于ASP.NET创建的那些讨厌的疯狂外观控件ID ...将它们缓存在页面末尾的JavaScript全局变量中......如:

    <script>
      ControlIDs = {
        SaveButton = '<%=SaveButton.ClientID%>',
        BlahTextBox = '<%=BlahTextBox.ClientID%>'
      }
    </script>
    

And in your separate jQuery code file, do...

在你单独的jQuery代码文件中,做...

$(ControlIDs.SaveButton).Whatever()

3. Instead of using the ASP.NET control ID, maybe you want to try using the CSS Class Name as selector in jQuery to get to a particular control (treat class name as control Id), might not be an ideal idea, but it could work.

3.而不是使用ASP.NET控件ID,也许你想尝试在jQuery中使用CSS Class Name作为选择器来获取特定控件(将类名称视为控件ID),可能不是一个理想的想法,但它可以工作。

Any of these should allow you to do some degree of Javascript separation.

任何这些应该允许你做一定程度的Javascript分离。

#2


You could put the javascript code into separate js file(s). You then reference the js file(s) in your page.

您可以将javascript代码放入单独的js文件中。然后,您可以在页面中引用js文件。

#3


I'm not sure how you're structuring your .NET and your markup, but I'd be using external JavaScript files to handle all of that, no inline JavaScript. That's how we work in here, and no issues so far, with a nice separation of markup, CSS, JavaScript, and .NET.

我不确定你是如何构建你的.NET和你的标记,但我会使用外部JavaScript文件来处理所有这些,没有内联JavaScript。这就是我们在这里工作的方式,到目前为止没有任何问题,标记,CSS,JavaScript和.NET的分离很好。

If you have specific types of dialogs, then use classnames on your markup, then in the JavaScript include files use classes as your selectors to create the dialogs.

如果您有特定类型的对话框,则在标记上使用类名,然后在JavaScript包含文件中使用类作为选择器来创建对话框。

#1


Let's see...

  1. Since you are using jQuery, you must know the script src syntax already to include javascript files, so I assume this is not the problem :).

    既然你正在使用jQuery,你必须知道脚本src语法已经包含javascript文件,所以我认为这不是问题:)。

  2. For those pesky crazy looking control ID that ASP.NET create... cache them in a JavaScript global variable at the end of the page... like:

    对于ASP.NET创建的那些讨厌的疯狂外观控件ID ...将它们缓存在页面末尾的JavaScript全局变量中......如:

    <script>
      ControlIDs = {
        SaveButton = '<%=SaveButton.ClientID%>',
        BlahTextBox = '<%=BlahTextBox.ClientID%>'
      }
    </script>
    

And in your separate jQuery code file, do...

在你单独的jQuery代码文件中,做...

$(ControlIDs.SaveButton).Whatever()

3. Instead of using the ASP.NET control ID, maybe you want to try using the CSS Class Name as selector in jQuery to get to a particular control (treat class name as control Id), might not be an ideal idea, but it could work.

3.而不是使用ASP.NET控件ID,也许你想尝试在jQuery中使用CSS Class Name作为选择器来获取特定控件(将类名称视为控件ID),可能不是一个理想的想法,但它可以工作。

Any of these should allow you to do some degree of Javascript separation.

任何这些应该允许你做一定程度的Javascript分离。

#2


You could put the javascript code into separate js file(s). You then reference the js file(s) in your page.

您可以将javascript代码放入单独的js文件中。然后,您可以在页面中引用js文件。

#3


I'm not sure how you're structuring your .NET and your markup, but I'd be using external JavaScript files to handle all of that, no inline JavaScript. That's how we work in here, and no issues so far, with a nice separation of markup, CSS, JavaScript, and .NET.

我不确定你是如何构建你的.NET和你的标记,但我会使用外部JavaScript文件来处理所有这些,没有内联JavaScript。这就是我们在这里工作的方式,到目前为止没有任何问题,标记,CSS,JavaScript和.NET的分离很好。

If you have specific types of dialogs, then use classnames on your markup, then in the JavaScript include files use classes as your selectors to create the dialogs.

如果您有特定类型的对话框,则在标记上使用类名,然后在JavaScript包含文件中使用类作为选择器来创建对话框。