简体   繁体   English

在Repeater中的JQuery中获取HiddenField

[英]Get HiddenField in JQuery inside Repeater

I have a Repeater control, and I need to access a HiddenField inside. 我有一个Repeater控件,我需要访问里面的HiddenField。 I can't use: 我不能用:

<%= Control.ID.ClientID %>

And I can't use the class or cssclass attributes either? 我也不能使用class或cssclass属性?

So my question is rather simple, how can I access my HiddenField control inside my repeater? 所以我的问题很简单,如何在我的转发器中访问我的HiddenField控件?

My scenario is that I populate a multiselectable dropdown, and I need to know in an update function which elements I have selected, for this I use the HiddenField to store the Id's. 我的场景是我填充了一个多选下拉列表,我需要在更新函数中知道我选择了哪些元素,为此我使用HiddenField来存储Id。 Then in code behind I can access the HiddenField values and make a propor databind. 然后在代码后面我可以访问HiddenField值并制作一个propor databind。

HiddenField control elements are rendered to input s of type hidden , so, albeit not thoroughly understanding your vague scenario with limited application, you can access them in jQuery with a selector like so: HiddenField控件元素被渲染为hidden类型的input ,因此,虽然没有完全理解你的应用有限的模糊场景,你可以使用选择器在jQuery中访问它们:

$("input[type=hidden]")

Depending on your situation you might want to constrain that selector yet more. 根据您的情况,您可能希望更多地约束该选择器。

However, this is focusing on your inclusion of the tag, though your example seems to want to use inline ASP.NET script to use managed code. 但是,这侧重于包含标记,尽管您的示例似乎希望使用内联ASP.NET脚本来使用托管代码。 Please clarify your intentions and ultimate goal. 请澄清您的意图和最终目标。

You can use the class selector 您可以使用类选择器
Put a class over the hidden field. 在隐藏的字段上放一个班级。
And on change event of multiselectable find the appropriate hidden field and set it's value. 并且on change multiselectable变量事件的on change事件中找到适当的隐藏字段并设置它的值。

Edit-1 编辑-1

I will suggest you to put a class say ddl over the multiselectable dropdown . 我建议你把一个类说ddl放在multiselectable dropdown
Bind jquery on change method on this multiselect. 在这个多选中绑定关于更改方法的jquery。
Using jquery parent and prev selectors you can set the values in the hidden field. 使用jquery parentprev选择器,您可以在隐藏字段中设置值。

Exmaple 〔实施例

     $(".ddl").change( function(){
        $(this).parent().next().find("input:hidden").val($(".ddl").val());
    });

This is just for and idea of my approach. 这只是为了我的方法和想法。

Edit 2 编辑2

Here is a good example of how to use css-class over a hidden field. 以下是如何在隐藏字段上使用css-class一个很好的示例。
Jquery Hidden Field Jquery隐藏的领域

I found a solution which in fact is pretty easy :) 我找到了一个实际上非常容易的解决方案:)

I used the ClientIDMode property on the HiddenField inside the repeater: 我在转发器内的HiddenField上使用了ClientIDMode属性:

<asp:HiddenField ID="HiddenField_Pladser" runat="server" Value="Selected" ClientIDMode="Static" />

The ClientID value is set to the value of the ID property. ClientID值设置为ID属性的值。 Then it's easy in the JQuery script to access the value: 然后在JQuery脚本中很容易访问该值:

$("#HiddenField_Pladser").val();

And from the code behind I know the ItemIndex from the repeater, which makes it easy to create a data collection whit this code: 从后面的代码我知道来自转发器的ItemIndex,这使得使用此代码创建数据集合变得容易:

var data = from RepeaterItem item in rpPladser.Items
                   let hidden = ((HiddenField)item.FindControl("HiddenField_Pladser"))
                   select new
                              {
                                  selected = hidden.Value
                              };

Please leve a comment if this implementation have flaws or somehow is bad. 如果这个实现有缺陷或某种程度上是坏的,请发表评论。

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

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