Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hye,

Is it possible to change/reset hiddenfield value to empty string from code behind?

I've tried methods below but none works;

1) hdnKey.Value = ""
2) Dim hdn As HiddenField = DirectCast(FindControl("hdnKey"), HiddenField)
hdn.Value = ""

Thanks.

---- UPDATE 1----

This is aspx codes:

XML
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
            <asp:HiddenField ID="hdnKey" runat="server" />
               <%-- Other controls here --%>
            </ContentTemplate>
</asp:UpdatePanel>


* Even if i move the hiddenfield outside the update panel, it still didnt work.


--- UPDATE 2 ----

I assign value to hiddenfield using javascript.
After that, I want to reset back the value to empty string.

<script>

        document.onkeydown = KeyDownHandler;
        document.onkeyup = KeyUpHandler;

        var CTRL = false;
        var SHIFT = false;
        var ALT = false;
        var CHAR_CODE = -1;


        var confirm_value = document.createElement("INPUT");
        var hdnKey = document.getElementById('&lt;%= hdnKey.ClientID %&gt;');
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        var a = hdnKey.value;

        function KeyDownHandler(e) {
            var x = '';
            if (document.all) {
                var evnt = window.event;
                x = evnt.keyCode;
            }
            else {
                x = e.keyCode;
            }
            DetectKeys(x, true);
        }

        function KeyUpHandler(e) {
            var x = '';
            if (document.all) {
                var evnt = window.event;
                x = evnt.keyCode;
            }
            else {
                x = e.keyCode;
            }
            DetectKeys(x, false);
        }

        function DetectKeys(KeyCode, IsKeyDown) {
            if (KeyCode == '16') {
                SHIFT = IsKeyDown;

                confirm_value.value = "16";
            }
            else if (KeyCode == '17') {
                CTRL = IsKeyDown;
                confirm_value.value = "17";
            }
            else if (KeyCode == '18') {
                ALT = IsKeyDown;
                confirm_value.value = "";
            }
            else {

                confirm_value.value = "";

            }

            document.forms[0].appendChild(confirm_value);
            document.forms[0].appendChild(hdnKey);
            hdnKey.value = confirm_value.value;

        }

    </script>
Posted
Updated 1-Jul-14 23:56pm
v7
Comments
DamithSL 1-Jul-14 23:19pm    
where you place your hidden field in aspx page? update the question with aspx page code.
normally hdnKey.Value = "" should work.
snamyna 2-Jul-14 0:27am    
Thanks for your response. I have update the question. I have tried that but didnt work :(
DamithSL 2-Jul-14 1:14am    
in which event you set the hidden field value? put button inside UpdatePanel2 and on click event clear the hidden field.
Prasad Avunoori 1-Jul-14 23:31pm    
Is that HiddenField exist in any other control, e.g. in Gridview or ListView?
snamyna 2-Jul-14 0:28am    
Thanks for your response. Hidden field is outside of any other controls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900