Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a value to a HiddenField in ASP.NET 4.5

I'm having some issues setting a value to a HiddenField in ASP.NET 4.5.

From what I've seen I've tried the following without any luck:

In ASPX:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;            
    }
</script>

In code-behind:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.ClientID + "');", true);

This alerts garbage in the ClientID.

The other solution I've tried is the following.

In .ASPX:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('HiddenField').value = vv;            
    }
</script>

One issue here is that .value does not exist in the IntelliSense, only .ValueOf.

In code-behind:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.Value + "');", true);

Nothing happens, probably an error in the JavaScript, since no alert is shown.

Can anyone point me to the right direction, please?

like image 209
user1782815 Avatar asked Jun 16 '14 19:06

user1782815


People also ask

How do you maintain the hidden field value on a PostBack?

Inside the Page Load event of the ASP.Net Page, the value of the Hidden Field set using JavaScript is fetched from the Request. Form collection using the UniqueID property. The fetched value is again set to the Label control, this makes the Label retain its value on PostBack.

What is HiddenField?

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.


2 Answers

Your first markup is good:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;
    }
</script>

Change the code to this (check the second line):

 ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
 ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert(document.getElementById('" + HiddenField.ClientID + "').value);", true);

And the output should be like this:

enter image description here

EDIT : In your scenario, you can run javascript to get a value and force a postback to use the value in your code. I would change my markup to this:

<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;
        __doPostBack('<%=HiddenField.ClientID%>', '')
    }
</script>

And in code my Page_Load is like below:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        // Register JavaScript which will collect the value and assign to HiddenField and trigger a postback
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true); 
    }
    else 
    {
        //Also, I would add other checking to make sure that this is posted back by our script
        string ControlID = string.Empty;
        if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
        {
            ControlID = Request.Form["__EVENTTARGET"];
        }
        if (ControlID == HiddenField.ClientID) 
        { 
            //On postback do our operation
            string myVal = HiddenField.Value;
            //etc...
        }
    }

}
like image 140
afzalulh Avatar answered Sep 22 '22 09:09

afzalulh


In the hidden field tag add clientid static like this -

<asp:HiddenField ID="HiddenField" runat="server" value="" ClientIDMode="Static" />

This way ASP.Net will not replace it with dynamic ID and always have the id that you provided, so it will now have ID HiddenField. Then your second attempt should work.

More can be found here -

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx

like image 34
brainless coder Avatar answered Sep 24 '22 09:09

brainless coder