I can not get the value of an asp: hiddenField in C #, it is always empty

1

I have a strange problem here. I'm assigning value to asp:hiddenField via javascript (I already tested giving alert and it shows the correct value), but then when I try to get the value of it in C # the meuHiddenField.Value is empty, it returns "". What can it be?

    
asked by anonymous 05.02.2014 / 18:13

6 answers

1

I had the same problem and resolved to find the value of it that was sent along with form in the request.

var valor = Request.Form["meuHiddenField"];

or

var valor = Request.Form["ctl00$MainContent$meuHiddenField"]; // Se o campo estiver dentro de um <asp:Content ContentPlaceHolderID="MainContent">
    
05.02.2014 / 18:24
1

Another way to access the value of HiddenField , or any other server component, is by using the UniqueID :

string valorHiddenField = Request.Form[hdfCampo.UniqueID];

It is safer than using "ctl00$MainContent$meuHiddenField" because this name format is generated by ASP.NET based on ContentPlaceHolder name MasterPage .

    
07.02.2014 / 16:43
1

Is not isPostBack your page not adding a new value in hiddenfield and why is it empty? Do you have something that matters, does it fill something in this field? if you have put it inside the

if(!isPostBack){
 muda/cria valor hiddenfield
}
    
01.04.2014 / 15:17
0

It would be interesting for you to post a piece of your code so we can help you better.

As I can not comment because I still do not have enough reputation, here's a code sample below for a problem similar to yours and see if you can take advantage.

Asp.net

   <asp:HiddenField runat="server" ID="aspHiddenField" />
   <input type="hidden" id="inputHidden" value='<%= aspHiddenField.ClientID %>' />

JavaScript

   var inputHidden = document.getElementById('inputHidden');
   $("#" + inputHidden.value).val("texto");

C #

   if (!string.IsNullOrEmpty(aspHiddenField.Value))
   {
      //Seu código
   }
    
05.02.2014 / 18:25
0

Have you put the <script> tag on the head of the page?

Because if you put and are trying to assign a value will not work, the following error occurs:

  

Uncaught TypeError: Can not set property 'value' of null

Because you have not loaded the page completely, you will not be able to assign any value to a page.

To solve this you can by <script> at the end of <body> , example:

Page.aspx :

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <script>
        document.getElementById("<%= HiddenField1.ClientID.ToString() %>").value = 'Token';
    </script>
</asp:Content>

Code Behind :

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write(HiddenField1.Value.ToString());
}
    
05.02.2014 / 19:08
0

One of the reasons he may be losing value when passed to CS, is because of runat="server" with this tag it loses the same value to each post. Try to remove the runat and fetch the value by FindControl

    
01.04.2014 / 15:01