简体   繁体   English

C#获取动态创建的标签的ID

[英]C# Get id of dynamically created labels

I need to be able to set the IDs of dynamically generated labels instead of letting SharePoint prefix my labels with a long cryptic id of its own. 我需要能够设置动态生成的标签的ID,而不是让SharePoint为我的标签加上自己的长隐式ID前缀。 Is this possible or is there another property of label that I can use as a unique identifier in a separate method? 这可能吗,或者标签的另一个属性可以用作单独方法中的唯一标识符?

Label animal = new Label();
animal.ID = cat;

The id for this label will be something like: ctl00_m_g_e0c173c0_edf3_4a99_a1dd_7bef33144c0b_ctl00_cat 该标签的ID将类似于: ctl00_m_g_e0c173c0_edf3_4a99_a1dd_7bef33144c0b_ctl00_cat

I need it to be cat. 我需要它成为猫。

要强制客户端ID与服务器ID相同,请使用:

animal.ClientIDMode = ClientIDMode.Static;

You cannot set the ClientId of a server control. 您不能设置服务器控件的ClientId。 It is a read only property. 这是一个只读属性。

The cryptic id you see is actually your control heirarchy. 您看到的神秘ID实际上是您的控件层次结构。

If you're attempting to access the control with javascript and are using jQuery, you can just the various regex selectors: 如果您尝试使用javascript访问控件并使用jQuery,则可以使用各种正则表达式选择器:

$("label[id$='cat']")

That will look for a label element with an id that ends with 'cat' 它将寻找一个以'cat'结尾的id的label元素

If your javascript is in the aspx file, and not a js file, you can also access it like so: 如果您的JavaScript位于aspx文件中,而不是js文件中,则您也可以像这样访问它:

$('#<%=cat.ClientID %>')

That will just inject that long id created by Sharepoint into your js. 那只会将由Sharepoint创建的长ID注入到您的js中。 Key is it needs to reside in the aspx file, not a js file. 关键是它需要驻留在aspx文件中,而不是js文件中。

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

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