8

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?

DanM7
  • 2,101
  • 3
  • 25
  • 45
user1782815
  • 185
  • 1
  • 3
  • 14
  • 1
    In your second example you still have to get the element by the client id unless you set the property on your hidden field `ClientIDMode="Static"` – Rick S Jun 16 '14 at 19:33

2 Answers2

9

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...
        }
    }

}
afzalulh
  • 7,835
  • 2
  • 24
  • 35
  • Perfect, I get the correct output now. Thanks. The issue now is how I'm going to get that value in the code-behind file. Creating a new Javascript function to display it won't help me much. Thanks. – user1782815 Jun 16 '14 at 20:12
  • The script will run after the code is executed and page is rendered. So, if you want to access the value, you have to return to the code. Add a button and in buttons click event access the hidden field value: `string myVal = HiddenField.Value;`. – afzalulh Jun 16 '14 at 20:29
  • Sorry, I'm not very familiar with ASP.NET. The only way is to force an postback to the page, to receive the updated value? What I need is the value to get fetched in the Load event on the Master Page, because the value is needed to add a reference to a WCF service. Thanks. – user1782815 Jun 16 '14 at 20:34
  • You want to access the value before it is entered in the hidden field. The javascript in the code will not run immediately, but the code in the masterpage will. So it will get empty value. Then page will render and javascript will run, hidden field will get a value. Are you sure you have to change the hiddenfield value using javascript and use it for the wcf service? – afzalulh Jun 16 '14 at 20:51
  • Thanks for the explanation. I don't need to use a HiddenField, but I need to somehow fetch the url to the WCF service using an exposed function in a C# WebBrowser which runs the ASP.NET page (getting this value by calling window.externa.myfunction(); in Javascript). If you have any other recommendations, I'd really appreciate it. – user1782815 Jun 16 '14 at 20:58
  • You can trigger a postback from the javascript. I will update my answer. – afzalulh Jun 16 '14 at 21:39
  • Thanks a lot! This works perfectly. I've been using way too much time on this issue. I wish we had some kind of donation system (feel free to send me your PayPal information, though). – user1782815 Jun 17 '14 at 06:42
5

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

brainless coder
  • 5,932
  • 1
  • 14
  • 34
  • Great, I might be one step closer. I did try that now, but I can't update the ClientID or set any value to it (since it's static I guess). The value is 'HiddenField'. – user1782815 Jun 16 '14 at 20:05