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

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.. :-)

Related

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 HiddenField in JQuery inside Repeater

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.

To find Control in Other page

I'm creating a web application using C#.net
My webpage contains a Hidden Field Control. I need to use this control's value in another page. I declared a method and inside this method i passed the entire page as a parameter. In another classfile i defined this method. Inside the method i tried to access the Controls that was in the page created. I tried but i can't get the control.
Code:
HiddenField hdnTotal = page.FindControl("hdnTotal") as HiddenField;
Is anyother option to find this control in other class file.
Thanks In Advance!
Instead of passing a page as parameter did you try using PreviousPage property to find a control? On the page where you want the control from previous page you could find hidden file like this:
HiddenField hf = (HiddenField)PreviousPage.FindControl("hdnTotal");
The normal way to get data from one page to another is to POST the first page to the second page in hidden/form fields.
Is there a reason you're trying to pass the entire control instead of having the control (for example) just set the value of one or more hidden form fields?
You have several options: to use cross-page posting or to pass the value of that hidden field as a GET parameter to another page when redirecting. Also you can save the value in session or in cookie and access it later.

Updating label using jquery and code behind

I have a label on a page and I'm updating the text property of the label (with a calculated value) when text is changed in a textbox.
I'm updating the label like so:
$myLabel.text("123");
The text is being displayed correctly on the screen butwhen I try to save the text value to an object in the code behind (When I press a button) the text property of the label is "" and not "123".
Code behind:
var myLabel = myLabel.Text;
//the var myLabel is "" when it should be "123"
Any ideas as to why this would be?
Thanks in advance,
Zaps
Not sure if this is the correct way to do it but I got around the problem by using a hidden field.
I updated the label text as above:
$myLabel.text("hello");
but then I updated the hidden field value:
$('#<%= hiddenField.ClientID %>').val("hello");
I was then able to use the hidden field in the code behind:
var myLabel = hiddenField.Value.ToString();
This seems to work fine.
Why don't you check the value that was entered in the textbox. based on your description, that should be the same and it will be available. Otherwise, I think you need to post some more code to clarify what you are doing.
The value of the label text needs to be stored in ViewState, otherwise it will be overwritten on the postback triggered by the button click.
One option would be to also change the value of a hidden control. Any changes to this value will be available in the code behind on postback.
<asp:Hidden id="hiddenLabel" runat="server" />
Html controls like labels, spans, divs don't post their values to the server - while inputs do. ASP.NET maintains changes in controls by using ViewState.
When you change the value of a server control, it's state is often persisted there. If you change the value on the client side via JavaScript, the ViewState isn't changed, and this is why on PostBack you get the original Empty value.
In what function are you putting var myLabel = myLabel.Text;?
It won't work in the init function -- you need to give the page time to load from the viewstate. Best in the button push event handler.
Update:
You need to use an form input control (eg TextBox) not label. Labels are readonly.

ASP.NET Get Original Text of Label

Is there any way to get the original text from a Asp:Label on a page, after the text has been altered?
with the orginal text i mean the text that is hard coded into the asp.net markup.
There is no standard way to get it back after some chages, but you could do that in some your ways, for example add custom attributes to label
textLabel.Attributes.Add("data", textLabel.Text);
and then use it on your page.
Or cache label value using js code on page startup or statically.
Create a property that wraps the text assignment. Before the assignment take the current value and assign it to a hidden input or stick it in the session or viewstate.
Create a property that retrieves the previous value from the hidden input or session or viewstate.
Could get fancy and extend the label to add a PreviousValue property. Not sure how this would work in practice though.

Categories