1

I inherited some JavaScript that I was told to integrate into our ASP.NET site. I thought this would be straightforward but it's turning out to be a bit of a challenge.

The code looks something like this:

<SELECT id="Question1" name="Question" onchange="updateQuestion();">                 
<OPTION value="notChosen">--Please Select One--</OPTION>
<OPTION value="in">India</OPTION>
<OPTION value="de">Germany</OPTION>                     
<OPTION value="fr">France</OPTION>                
<OPTION value="us">United States</OPTION>                     
<OPTION value="ch">Switzerland</OPTION>                
</SELECT> 

The goal is to get the value from this HTML control into ASP.NET, however this control itself is being dynamically generated by another chunk of javascript, so I can't just change this to an asp.net control. My solution was to add the onchange="updateQuestion();" method, this JS will take these SELECT tags and place the values into an ASP.NET control:

function updateSecQ() {
    var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');
    sQuestion.Value = "";
    var questions = document.getElementsByName('Question');
    for (question in questions) {
        if (questions[question].value != null)
            sQuestion.Value += questions[question].value + ",";
    }
    alert(sQuestion.Value);
}

As you can see, that's looking to update an ASP.NET control: <asp:HiddenField ID="sQuestion" runat="server" value="" />

This appears to all work, however when I goto the server side on the form submit I see that sQuestion.Value is still = "".

I'm not quite sure what I've done wrong here, any input would be much appreciated. Thank you for your time.

1
  • In which part of the page lifecycle are you trying to read sQuestion.Value? Commented Feb 3, 2011 at 21:29

4 Answers 4

7

Dunno about ASP.net, but I know in the browser DOM, a hidden field's value is value all lower case, not Value. It's the value attribute that the browser will submit, and by setting sQuestion.Value, you've failed to set its submitted value.

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

4 Comments

+1 Good spot! @Dio Javascript will just create new members of objects on the fly if they aren't already defined. Useful but dangerous! w3schools.com/js/js_objects.asp
Good catch. I apparently have some other error since the alert is now an empty string, but I'll figure that out. I really appreciate it.
...are you still alerting sQuestion.Value? - Or your getElementsByName is returning nothing. No I see it, its a select! So that's questions[question][questions[question].selectedIndex].value - you can rationalise that with a few temporary variables...
So: var select = questions[question]; var value = select[select.selectedIndex].value; if(!value) sQuestion.value += value + ',';
2

Is that JavaScript function on the same aspx page? if not I believe that

var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');

will fail.

If you use jQuery you can use:

$("[id *= 'substringOfTheASPId']").val("new value")

1 Comment

Good Suggestion. Presumably the script is in the aspx page though as the OP says the function works.
0

You set sQuestion.Value instead of sQuestion.value.

I found this in an answer to a similar question.

Comments

0

I don't believe .NET expects that value of a HiddenField to be manipulated on the clientside, so it probably just reads the ViewState value back everytime. You can use a regular hidden input and make it server control by adding the "runat" property like this:

<input type="hidden" id="myInputId" runat="server" />

In the code behind, you can read the value in a manner similar to what you have above. It will be of the type System.Web.UI.HtmlControls.HtmlInputHidden.

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.