views:

434

answers:

4

I have a hidden field and I change the value just before submitting the form using Javascript. But in server side it is null or empty. Request.Form["hidAction"] is empty.

<script type="text/javascript" language="javascript">

    function DoChange() 
    {
        document.getElementsByName('hidAction').value = "filter";
        alert(document.getElementsByName('hidAction').value);
        document.forms[0].submit();
    }
 </script>
<body>
    <form id="form1" runat="server">
       <div>
            <select name="lst" onchange="DoChange();">
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
       </div>
       <input type="hidden" id="hidActionID" name="hidAction" value="tt" />
    </form>
</body>
</html>
+1  A: 

document.GetElementsByName returns an array of elements, even if there is only one. Why not used document.GetElementById and assign an ID to the hidden input? That should work just fine.

achinda99
Yes it is working. Thank you very much, I really appreciate it.
A: 

+1 on using the ID on the client side. Are you sure that the JS is updating the Input object before the submit? Your code looks pretty good.

Perhaps, you should use the ID instead of the name on the server side.

Request.Form["hidActionID"]

ssorrrell
A: 

I might be wrong, but there is no runat="server" on you hidden input, maybe it is for this reason that you can't access it on the server.

Martin
A: 

I'd do what you're doing differently, but going with what you have you could do this, assuming that there is only one element on the page with the name attribute 'hidAction' on the page:

function DoChange() 
{
    document.getElementsByName('hidAction')[0].value = "filter";
    alert(document.getElementsByName('hidAction')[0].value);
    document.forms[0].submit();
}

You could also do it this way, assuming there is only one form on the page:

function DoChange() 
{
    document.forms[0]['hidAction'].value = "filter";
    alert(document.forms[0]['hidAction'].value);
    document.forms[0].submit();
}

When you postback, Request.Form["hidAction"] shouldn't be empty now.

nickyt