Get HiddenField in JQuery inside Repeater - c#

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.

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 jquery tag, though your example seems to want to use inline ASP.NET script to use managed code. 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.
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

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.

Related

How to Find UserControls Control value in Jquery Asp.net?

I am using jQuery and ASP.net
I have a User Control and wanted to Set the values of this User control to the database, for that I need the user control's value. In the same way, I want to show the data from the database in the user control, for that I need to Get the values by jquery.
In my user control I have 4 TextBoxes and 2 Buttons (SET/UPDATE)
AutoCompleteSearch_New is ascx user control
Here is my tried code:
var ID = $('#<%= ((HiddenField)AutoCompleteSearch_New.FindControl("hdnvalue")).ClientID %>').val();
But I dont wan't to use hidden fields.
Can I directly find the control's value without using hidden fields?
It is similar to use value from a webform.
Here is the code for the same.
Var TextBoxValue = $('#YourTextBoxID').val();
Inspect element and get the Textbox ID and replace it with YourTextBoxID.
Or
Var TextBoxValue = $('#<%= YourTextBoxID.ClientID').val();
where YourTextBoxID is your asp:Textbox ID.
When ever you load user control in your aspx page, jquery consider it as a whole page which is combined of user control and rest of the aspx controls exist in the form. So you can directly get textbox value in your jquery
you can do that by adding ClientIDMode="Static" to the Control
and then use
var txtvalue= $("#TextBoxId").val();
Provided storing the values in Session is not an option, other options I see are :
javascript variable declaration in the user control markup
client-side decryption of the view state
custom data attributes on html elements
I'm afraid HiddenField is your best option. That's somewhat the same idea behind ViewState.
If you don't want the client to tamper with the value, you may go for a combination of the value, and a hash of the value concatenated to a secret key ( value+separator+Hash(value+secretkey) )
If you don't want the client to access the value, you may rely on encrypting it in the HiddenField
hey u can directly find control of textbox or hiddenfield without mention its type
var value=$('AutoCompleteSearch_New_hdnvalue').val();
here AutoCompleteSearch_New is the name of user control u load on page and
hdnvalue is the id of hidden field in user control AutoCompleteSearch_New
enjoy.. :-)

preserve UI state of asp.net controls after button click

I have an ASP.NET web app with a dropdown list and a number of ASP.NET textboxes/labels. when the user chooses a particular item from the dropdownlist the labels change text and some of them are hidden etc using jquery. However after clicking on a button the controls are being changed back as when the page is first loaded.
Please suggest
How i Can preserve the new labels name etc?
Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
Should i use hidden fields to store the value?
1: How i Can preserve the new labels name etc?
Yes,
You can use hidden field to store.
2: Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
No,
On page_load access hidden field, extract and use values.
3: Should i use hidden fields to store the value?
Yes,
You can use hidden field to store changes you made in GUI control using jQuery. Make that hidden field server accessible by making it runat server. On server end when you get postback access the hidden field, extract and assign values to respective controls. You can see how it would work in this post.
As you might be knowing , after clicking the button if it is submit type it will do the postback to the server , as HTTP is stateless , it is your responsbility to generate the label text and value again. Generally ASP.NET viewstate take care of doing so for input texts, but for labels you have to do by yourself. I can suggest the solution like this
1) Have a javascript method which will have the logic to change the value of label and hidden field based on the selected value of the dropdown
function MyFunction ( ddlSelectedValue)
{
if(ddlSelectedValue == "1")
{
$("#LABELID").html('YOUR VALUE');
// rest other logics
}
}
2) ASP.NET viewstate will help you to preserve the dropdown state after button click . So on add this jquery script on the page top
$(document).ready(function(){
var ddlSelectedValue = $("#DDLID").val();
MyFunction(ddlSelectedValue);
});
3) attach the the point 1 method to onchange of the dropdown also.
4) Using the hidden field is Ok , because hidden fields will serve your requirement in using in the server side as they will also be posted back.

get aspxcombobox css class name

Im adding dynamically some aspxcombobox controls to my page. Each time I add one I add specified cssclass to it. Is it then possible to get that controls cssclass from javascript ?
Thanks for any hints
the CssClass is the class field on an html element. So to get it from javascript easily
document.getElementById("MyElement").className;
If the editor is inside a NamingContainer, for example, WebUserControl, or TabControl or any template, I would suggest that you set the editor's ClientInstanceName property. This property was specially designed to allow our users access the corresponding client side java script objects. For example, if the ClientInstanceName is set to the combo value, the code would be:
var class = combo.GetMainElement().className;

Connect UserControl to a Datasource (ObjectDataSource in my case)

I'm trying to find a way to Databind a usercontrol to a datasource (an
ObjectDataSource in my case) like in the case below :
<uc1:AutoComplete ID="autoComCities" runat="server"
DataSourceID="objCitiesDS" DataValueField="Id" DataTextField="Name" />
I've try to find some good exemple with our lil' friend Google and I still
ain't find anything concret. Someone can show me or give me good links on
how I could do this. I mean, about how to handle it in my usercontrol ;o)
Thanks!
Edit
Note that in my case, it's not a Dropdownlist or any Bindable control in my usercontrol, its a normal asp.Net Textbox with some custom jQuery so when user type in the textbox, it shows "possibilities" under the textbox. So, I want to get values from the DataSrouce and put it into HTML so jQuery can Handles values and shows it to user.
You can't just use the ObjectDataSource, there is a lot of plumbing to get that to work. You can pass the ID and call the Select() method to perform the selection manually and then get the values from the data and render them out. You'll have to use reflection or DataBinder.GetPropertyValue method to extract the individual field values within each record.
HTH.

ASP.NET controls added dynamically not visible from the code behind

I'm adding a dynamically built set of checkboxes to an asp.net page from the code behind with something like this in a recursive fashion:
pnlPageAccessList.Controls.Add(myCheckboxControl);
The controls show up fine on the page, but they don't show up when I view source, nor can I access them from the code behind.
If I add the controls in the on_init method, they work. But I have some business rules driving changes to the list of controls itself that require I fire the add method elsewhere.
Has anyone seen this before? I'm away from work so I can't copy the exact code.
I have two terrible ideas for how to get it working. One involves some jQuery and a set of hidden controls holding a big array of integers; the other is run the method on_init AND on my other events so the controls at least show up. Both smell like ugly hacks. The second one I suspect won't work to read the values out of the checkboxes.
On the server side the page is recreated from scratch every postback, so if you add any controls dynamically, you have to re-add them on every postback.
As you add the controls at runtime they are not known at compile time, so there is no variables declared for the controls in the Page object. If you want to access the controls you either have to keep the reference from when you create the controls, or locate them in the Controls collection where you put them.
If you can set the ID for the checkbox controls you can use the FindControl method from code behind to retrieve the control instances.
#Anero is right that you can add an ID and use FindControl.
You can also use a checkbox list, and add checkboxes to that list. Then they're already in a predefined control in your markup and code-behind.
You don't say where the method has to be fired, but once they're added dynamically, they do have to be added on every postback. You probably have a little more flexibility than just adding them at the Init event, as long as you stay aware of where things like validation occurs (if it matters in this case), or where you want to handle the checkbox contents. You can defer as late as PreRender for getting checkbox content.
Well, looks like I'm going to have to do it clientside. Thanks for the answers. I'd be able to do it On_Init, but doing it clientside with a hidden control is gonna save me a lot of overhead and be more flexible moving forward.
If anyone's curious, here's the jQuery for finding all the checked checkboxes and putting their value attribute into a hidden control in a comma delimited list:
<script type="text/javascript">
$(document).ready(function () {
$('[id*=PagesPanel]').find(':checkbox').click(function () {
$('[id*=PagesPanel]').find(':checked').each(function () {
$('[id*=lblHiddenPageArray]').append($(this).val() + ", ");
});
});
});
</script>

Categories