0

I have a Repeater control, and I need to access a HiddenField inside. I can't use:

<%= Control.ID.ClientID %>

And I can't use the class or cssclass attributes either?

So my question is rather simple, how can I access my HiddenField control inside my repeater?

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. Then in code behind I can access the HiddenField values and make a propor databind.

DNRN
  • 2,397
  • 4
  • 30
  • 48

3 Answers3

2

HiddenField control elements are rendered to inputs 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:

$("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. Please clarify your intentions and ultimate goal.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I can use the above to find the Control indeed. But I found an other solution. I used the property 'ClientIDMode="Static', then I could use the ID as identifier in JQuery. See my own answar for an implmentation – DNRN Feb 13 '13 at 10:08
0

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.

Edit-1

I will suggest you to put a class say ddl over the multiselectable dropdown.
Bind jquery on change method on this multiselect.
Using jquery parent and prev selectors you can set the values in the hidden field.

Exmaple

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

This is just for and idea of my approach.

Edit 2

Here is a good example of how to use css-class over a hidden field.
Jquery Hidden Field

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • 'System.Web.UI.WebControls.HiddenField' does not have a public property named 'CssClass' or 'class' – Harshit Tailor Feb 13 '13 at 09:05
  • thanks @HarshitTailor ok I was unaware about this. But he can wrap it into a container and put a class over that. and then it can be easily done. – शेखर Feb 13 '13 at 09:14
  • I found another solution, you can see the implementation in my own answare. But thanks for you answar :) – DNRN Feb 13 '13 at 10:19
0

I found a solution which in fact is pretty easy :)

I used the ClientIDMode property on the HiddenField inside the repeater:

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

The ClientID value is set to the value of the ID property. Then it's easy in the JQuery script to access the value:

$("#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:

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.

DNRN
  • 2,397
  • 4
  • 30
  • 48
  • If you can use static identifiers then definitely do (ASP.NET's verbose ids have been a bane of the web since their conception), it will make this task and others easier and more natural. Only note is that this is only available in .NET 4.0 onward, so be sure you're hosting will support it. – Grant Thomas Feb 13 '13 at 10:10
  • I wasn't aware it was a 4.0 feature probably therefor I havn't seen it before. My host is supporting 4.0 so this isn't a problem :) – DNRN Feb 13 '13 at 10:13