简体   繁体   English

从代码隐藏获取HTML控件

[英]Reach Html control from Code-behind

I would like to know how I can reach attributes in code behind (if possible) added to the HTML control using jQuery! 我想知道如何在使用jQuery添加到HTML控件后面的代码中(如果可能)访问属性!

I have this ASP.NET-control: 我有这个ASP.NET控件:

 <asp:TextBox ID="myID" CssClass="class1 class2 " runat="server"></asp:TextBox>

In jQuery I set it to: 在jQuery中,我将其设置为:

var myInput = $(control).find('#<%= myID.ClientID %>');
myInput.addClass('class3');

My problem is that I am not able to see in code behind that I have added class3 ? 我的问题是我看不到添加了class3代码?

It's not possible. 这是不可能的。 You use client code to add class, buy during the post operation only the form values posted, not the entire html. 您使用客户端代码添加类,在发布操作期间仅购买发布的表单值,而不购买整个html。 Check it with fiddle for example. 例如,用小提琴检查它。 That is not how http work. 那不是http的工作方式。

A page only post name value pairs of the form element to the server. 页面仅将表单元素的名称/值对发布到服务器。 So the code behind on the postback has only viewstate and new name value collection of form elements. 因此,回发中的代码仅包含viewstate和表单元素的新名称值集合。 So there is no straight way that a code behind may now what modification you have done using jquery in the dom except the values in the form element. 因此,除了form元素中的值之外,没有一种直接的方法可以使背后的代码现在可以使用dom中的jquery进行修改。 And if the class is added at compile time or in code behind, this information is maintained in view state and page with the help of view state restores the control properties. 并且,如果该类是在编译时或在后面的代码中添加的,则此信息将保持在视图状态,并且在视图状态的帮助下页面将恢复控件属性。 If it is really needed in your current task then you can put name of the class in any hidden element using jquery and get the values of the hidden element in code behind. 如果当前任务确实需要它,则可以使用jquery将类的名称放在任何隐藏的元素中,然后在代码中获取该隐藏的元素的值。

myID.Attributes["class"]

尚未实际尝试执行此操作,但这就是我要尝试的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM