I can’t get the value of an Asp:hiddenField in c#, it’s always empty

Asked

Viewed 2,938 times

1

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

6 answers

1


I had the same problem and solved seeking the value of it that was sent along with the 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">
  • I tried to do with request request.form["Maincontent_meuhiddenfield"]; and also returned "";

  • Try Request.Form["ctl00$MainContent$meuHiddenField"]

  • I’ve tried that too and nothing; It’s very strange!

  • What value does the Request.Form.AllKeys is coming?

  • ctl00$ConteudoPrincipal$tcNavegacao$tpPesquisa$hdnStatus

  • His field is the hdnStatus? If it is you need to do Request.Form["ctl00$ConteudoPrincipal$tcNavegacao$tpPesquisa$hdnStatus"]

  • Funcionouuuuuuuu I found out. was analyzing the code here I noticed that was calling the button click before assigning the value to hiddenField hehe.

  • Quiet, thanks for the return!

Show 3 more comments

1

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

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

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

1

will not be in the isPostBack your page is not adding a new value in hiddenfield and so this empty ? you have something that mounts, fills something in that field ? if you have put it inside the

if(!isPostBack){
 muda/cria valor hiddenfield
}

0

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

As I can’t comment because I don’t have enough reputation yet, below is a code sample 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
   }
  • <div runat="server" style="visibility: Hidden"> <Asp:Button ID="btnUm" runat="server" Onclick="btnUm_Click" /> <Asp:Hiddenfield id="hdnUm" runat="server" /> </div>

  • Function test(tst)ː Document.getElementById('ctl00_ConteudoPrincipal_wucTreeviewDinamicaTELA_btnUm'). click(); Document.getElementById('ctl00_ConteudPrincipal_wucTreeviewDinamicaTELA_hdnUm'). value = tst; Alert(Document.getElementById('ctl00_ConteudoPrincipal_wucTreeviewDinamicaTELA_hdnUm').value); }

  • protected void btnUm_Click(Object Sender, Eventargs e) { String varTeste = Request.Form["ctl00$Conteudoprincipal$wucTreeviewDinamicaTELA$hdnUm"]; }

  • above are the 3 parts HTML javascript and C#

  • Funcionouuuuuuuu I found out. was analyzing the code here I noticed that was calling the button click before assigning the value to hiddenField hehe.

0

You put the tag <script> at the head of the page?

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

Uncaught Typeerror: Cannot set Property 'value' of null

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

To solve this you can put the <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());
}

0

One of the reasons why it may be losing value when passed to Cs is because of runat="server" with this tag it loses value even at each post. Try to remove the runat and fetch the value by Findcontrol

Browser other questions tagged

You are not signed in. Login or sign up in order to post.