简体   繁体   English

使用JavaScript ASP.NET获取服务器控件上的子控件

[英]Get child controls on server control using javascript ASP.NET

I've create a basic composite server control that contains a button. 我创建了一个包含按钮的基本复合服务器控件。

<Custom:Class1 ID="testClass" ClientInstanceName="test1" runat="server"></Custom:Class1>

I would like to be able to get to the child controls using javascript for example: 我希望能够使用javascript来访问子控件:

var myButton = testClass.FindControl('btnTest'); var myButton = testClass.FindControl('btnTest');

Is there anyway to do this? 反正有这样做吗?

Create a client side object to represent your server side control (javascript class). 创建一个客户端对象来表示您的服务器端控件(javascript类)。 Put a collection of references to the child controls on the client side object. 将对子控件的引用集合放在客户端对象上。 Then on the server side OnPreRender event create or load a script to define your client side object and at the same time pass the collection of references to its constructor. 然后在服务器端的OnPreRender事件上创建或加载脚本以定义您的客户端对象,同时将引用集合传递给其构造函数。

Example of how to embed a javascript file containing the clientside object definition (put this somwhere above the namespace declaration: 如何嵌入包含客户端对象定义的javascript文件的示例(将此名称放在名称空间声明上方:

[assembly: WebResource("myNS.stuff.clientSideObj.js", "application/x-javascript")]
namespace myNS.stuff
{

Example of how to register the WebResouce (OnPreRender): 如何注册WebResouce(OnPreRender)的示例:

 ClientScriptManager cs = this.Page.ClientScript;// Get a ClientScriptManager reference from the Page class.
 Type csType = this.GetType();// Get the type from this class.

 //Register an embedded JavaScript file. The JavaScript file needs to have a build action of "Embedded Resource".
 String resourceName1 = "myNS.stuff.clientSideObj.js";
 cs.RegisterClientScriptResource(csType, resourceName1);

Example of creating a script to declare an instance of your client side object (OnPreRender): 创建脚本以声明客户端对象(OnPreRender)实例的示例:

String childControlIDsList= getChildControlList();//I am not writing this one.. just look up javascript arrays.
String uniqueScriptKey = "myKey";
StringBuilder theScript = new StringBuilder();
theScript.AppendLine("var myObj = new clientSideObj(" + childControlIDsList + ");");
theScript.AppendLine("window['myClientControl'] = myObj;") //create a client side reference to your control.
cs.RegisterStartupScript(csType, uniqueScriptKey, theScript.ToString(), true);

I will leave the client side object definition up to you... Hope that helps! 我将把客户端对象的定义留给您...希望有帮助!

If you're using .net 4.0 you can set the the ' ClientIDMode ' property on the button to Static then set the ID property to something like ID="myButton" and access it with with jquery like so 如果您使用的是.net 4.0,则可以将按钮上的“ ClientIDMode ”属性设置为“ Static然后将ID属性设置为类似ID =“ myButton”的内容,并使用jquery进行访问,如下所示

 $("#myButton")

If you dont use jquery you can use 如果您不使用jQuery,则可以使用

document.getElementById("myButton")

If you are using the control multiple times in the form you could prefix the button id with your custom controls id. 如果您在表单中多次使用控件,则可以在按钮ID之前添加自定义控件ID。 Remember to set your custom controls ClientIDMode property to static as well. 切记还要将自定义控件的ClientIDMode属性也设置为static。

<asp:Button ID="<%=Me.Id%>_myButton "  ClientIDMode="Static" runat="server"/>

Not entirely sure this is even good practice or if it will work but you can try it.( remember to set your custom controls id 不能完全确定这是否是一种很好的做法,或者是否可以使用,但是您可以尝试一下。(切记设置您的自定义控件ID

This is for Sharepoint visual web controls, but the same general process applies: 这适用于Sharepoint可视化Web控件,但适用相同的一般过程:

http://lemonharpy.wordpress.com/2011/07/20/expose-clientid-to-javascript-in-sharepoint-2010-visual-web-control/ http://lemonharpy.wordpress.com/2011/07/20/expose-clientid-to-javascript-in-sharepoint-2010-visual-web-control/

Essentially, in the code behind on the page you get a reference to the ClientId on the page, and then expose that up as a javascript variable that you can prepend onto your jQuery selectors. 本质上,在页面后面的代码中,您可以引用页面上的ClientId,然后将其作为javascript变量公开,您可以将其附加在jQuery选择器上。

I feel this is a little hacky - but I think it gets the job done... 我觉得这有点麻烦-但我认为它可以完成工作...

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

相关问题 ASP.NET 使用 javascript 获取控件列表中每个控件的 Id - ASP.NET Get the Ids of each control in a list of controls using javascript ASP.Net在FormView控件中访问子控件 - ASP.Net Accessing child controls in a FormView control 以编程方式将子控件添加到asp.net复合控件 - Programically adding child controls to a asp.net composite control 访问模板化用户控件ASP.NET中的子控件 - Accessing child controls that are in a templated user control ASP.NET asp.net服务器控件 - asp.net server controls 在ASP.NET Web窗体中,我想使用javascript在子Repeater中进行控制 - In ASP.NET Web Forms I want to get control inside child Repeater with javascript 在ASP.NET中使用HTML控件比使用ASP.NET控件有效吗? - Is using HTML controls in asp.net effective than using asp.net control.? asp.net中的用户控件为子控件添加前缀字符串,导致在javascript中引用它们时出现问题 - user controls in asp.net prefixing string to child controls causing problem to refer them in javascript 带有嵌套控件的 ASP.Net 用户控件 - ASP.Net user control with nested controls 如何使用JavaScript或JQuery获取ASP.net中生成的控件的ID - How to get IDs of Generated controls in ASP.net using JavaScript or JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM