0

How can I get ClientSide(JavaScript) Value for My ASP.net Custom Control?
for example I want to get a value like this:

var selectedItemID = getElementById("<%=MyControl1.ClientId%>").value;

How can i set a specific Value in my control scripts to get it from ".value" property like above?

Additional Note: i want ".value" property(javascript) to get the dropDown control(one of my controls in my custom control) selected Value.

2
  • What exactly does your custom control render to? Does it render to HTML Textbox or lable Commented Feb 19, 2011 at 7:46
  • it is a search drop down control. the ".SelectedValue" property in serverSide returns ItemID and I want to get it from clientSide with ".value" property. is that possible? If I use "<%= MyControl1.SelectedValue%>" this will render once when the page loads and if user change the drop down Item it will not work. Commented Feb 19, 2011 at 8:17

4 Answers 4

1

You can have a custom attribute for your custom control while it is rendering and bind the necessary value. Then in the Clientside, you can get the custom attribute and get the corresponding value from it.

For ex: Say suppose you are adding a custom attribute to your control using the code below while rendering,

MyControl.Attribures.Add("attributeName","Value");

then you can get the value in the clientside using the code snippet below.

var controlValue = $("#"+"<%= MyControl1.ClientID %>").attr("attributeName");

This would give you the value that you stored in the custom attribute of the control.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sured but You can try this:

var control = $find("<%= MyControl1.ClientID %>");

may be following link usefull for you No error message displayed for custom validator

2 Comments

0

just do like this way using jquery:

$("<%= MyControl1.ClientID %>").val();

using javascript:

var Val=document.getelementbyid("<%= MyControl1.ClientID %>").value;

hope this help.

Comments

0

If your control rendered as an input, your code will work but if it is anything else, such as a span or label, you need to use .innerHTML instead of .value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.