asp.net+css+jQuery - 它们是如何协同工作的?
我想了解如何使用 jQuery 来处理 asp.net 和 css。 当我编写 asp.net 代码时,例如,我要添加到页面 DropDownList,当我在 Web 浏览器中打开页面的源代码时,我无法在源代码中看到它。我可以看到选择标签,而不是下拉列表。什么时候才能完成更改 asp.net 标签选择的“魔法”? 更重要的是,我看不到添加到 asp.net 标记中的 CSS 类名称。有一些不同的 CSS 类名称。但是当我在 IE 中打开开发人员工具时,我可以看到 CSS 类名称,它们与我的定义中的相同。 最后一件事。我应该在 jQuery 中使用什么标签名称来遍历在 asp.net 中开发的页面。我应该使用在浏览器页面的源代码中看到的标签,还是可以向 jQuery 询问有关 asp.net 标签的信息? CSS 类怎么样?为什么我在浏览器的页面源中看不到它们?可以在 jQuery 查询下使用我的 CCS 类名称吗? 请问有人可以解释一下这三种技术如何协同工作吗?
I would like to understand how can I use jQuery to work with asp.net and css.
When I'm writing asp.net code and for example I'm adding to a page DropDownList, I can't see it in the source when I'm opening source of a page in web browser. Instead of dropdownlist I can see select tag. When does the "magic" is done to change asp.net tag to select?
What is more I can't see my CSS classes names added to asp.net tags. There are some kind of differen CSS class names. But when I'm opening developer tools in IE, I can see CSS class names, which are same as in my definition.
And the last thing. What names of a tags sould I use in jQuery to traverse page which was developed in asp.net. Shoud I use a tags which I see in the source code of a page in a browser or can I ask jQuery about asp.net tags? What about CSS classes? Why I can't see them in a source of a page in a browser? Can use my names of a CCS classes under jQuery queries?
Please, can anybody explain me how does this three technologies work together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想知道的大部分“魔法”都是由 ASP.NET 控件完成的,这些控件旨在生成发送到浏览器的标记。
收到请求后,应用程序会迭代每个控件,调用其
Render
方法(继承自Control
类),该方法允许每个控件生成它们所代表的标记。按照您的示例,
DropDownList
控件会生成一个另一个是
GridView
,它使用的
GridViewRow
控件生成和各种 HTML 控件,例如用于
TableCell
,用于创建标记的其余部分。不,jQuery/JavaScript 不知道服务器端控件名称,只知道它们生成的标记。因此,您无需搜索
$('DropDownList')
,而是搜索$('select')
。“CSS 名称”是指 ID吗?抱歉,
CssClass
属性的值不应从服务器端更改为客户端,而应仅更改名称 -CssClass
更改为类。
另一方面,ID 带有前缀以确保其在整个页面中的唯一性,包括 MasterPage 和 ContentPlaceHolder 名称的前缀(如果使用)。出于这个原因,我会避免尝试使用 ID 将 CSS 应用到服务器端控件,而是使用类。
现在,ID 的末尾应该保留为您在服务器端提供的 ID,因此您仍然应该能够使用 属性以选择器结尾
[name$='value']
:否则,我会坚持使用类来查找您的控件正在寻找:
Most of "magic" you're wondering about is done by ASP.NET controls, which are designed to generate the markup that is sent to the browser.
When a request is received, the application iterates over each control, calling its
Render
method (inherited from theControl
class), which allows each control to generate the markup they represent.Following your example, the
DropDownList
control generates a<select>
tag. As aListControl
, it uses theListItem
controls to create the<option>
tags within.Another would be the
GridView
, which generates a<table>
usingGridViewRow
controls for<tr>
and various HTML Controls, such asTableCell
for<td>
and<th>
, to create the rest of the markup.No, jQuery/JavaScript have no knowledge of server-side control names, only the markup they generate. So, rather than searching for
$('DropDownList')
, you'd search for$('select')
.By "CSS Names," do you mean IDs? I'm sorry to ask, but
CssClass
attributes shouldn't change in value from server-side to client-side, just in name --CssClass
to justclass
.IDs, on the other hand, are prefixed to ensure their uniqueness throughout the page, including a prefix of the MasterPage and ContentPlaceHolder names, if they're used. For this reason, I'd steer away from trying to use IDs to apply CSS to server-side controls, using classes instead.
Now, the end of the ID should remain as the ID you gave in server-side, so you should still be able to find the element in jQuery using the Attribute Ends With Selector
[name$='value']
:Otherwise, I'd stick to classes to find the controls you're looking for:
“魔法”发生在 ASP.NET 页面生命周期的渲染事件中。 Asp.net 服务器控制所有渲染为标准 html 元素。最重要的区别是您可以在服务器端访问它们及其值。 WebControls 还有一个 CssClass 属性,该属性在呈现时成为 HTML 元素的类属性。
使用 jQuery 和 CSS 时,id 可能有点棘手。这是因为根据控件层次结构,它们可能具有 clientID,例如 ctl100_containerID_myControl,而不是 myControl。为了在 jQuery 中克服这个问题,当您引用一个控件时,您可以通过其 ClientID 来引用它,如下所示:
这是服务器端,它将在呈现后写入控件的客户端 ID。
The "magic" happens in the render event of the asp.net page lifecycle. Asp.net server controls all render as standard html element(s). The most important difference is that you can access them and their values on the server side. WebControls also have a CssClass property that when rendered becomes the class attribute of the HTML element.
The id can be a bit tricky when working with jQuery and CSS. This is because depending on the controls hierarchy they may have a clientID such as ctl100_containerID_myControl instead of myControl. To overcome this in jQuery when you reference a control you can refrence it by its ClientID like so:
This is serverside that will write the client side ID of the control after it is rendered.
ASP.NET:高级 Web 开发框架。当您在 .NET 中创建 Web 表单时,该框架将与 IIS 处理程序一起工作,并创建(希望如此)有效的 HTML,以便在回发期间与您的服务器端代码配合使用。
JQUERY:这将允许您执行客户端脚本,例如计算、验证,尤其是 AJAX 等。这基本上只是一个更简单且更易于阅读的 javascript 版本的包装器。
CSS:采用 HTML 并使其变得漂亮。
如果您知道自己在做什么,那么这三种技术可以很好地协同工作。
我不确定这是否是您正在寻找的内容,但听起来您可能想投资一些初学者文献。
ASP.NET: High-level web development framework. When you create a web form in .NET, the framework will work together with the IIS handlers and create (hopefully) valid HTML that will work with your server-side code during postbacks.
JQUERY: This will allow you to perform client-side scripting such as calculation, validation, and most notably AJAX, etc. This is basically just a wrapper for a simpler and easier-to-read version of javascript.
CSS: Takes the HTML and makes it pretty.
All three technologies work very well together if you know what you're doing.
I'm not sure if that's what you're looking for, but it sounds like you might want to invest in some beginner's literature.