1

I've been looking for a reason why this doesn't work, but I can't find one. In my asp.net application I create a a bunch of hidden inputs in c# and then try to modify them in javascript before I call them back to the server.

My c# code:

    hidden3 = new HtmlInputHidden();
    hidden3.ID = "total";
    hidden3.Value = index.ToString();
    this.Controls.Add(hidden3);

my javascript code:

     mod = document.getElementById("total");
     mod.value = newVal;

I can call the value back fine but it doesn't change. I have also added alerts for the original value and then the value after changing values and they both show up fine. However the code is never changed so when I pull the values

To get the value back I am using this;

    HtmlInputHidden hiddenControl = (HtmlInputHidden)FindControl("total");
2
  • Maybe you are missing it for a postback. Can you post your page code? Regards Commented Jun 3, 2011 at 21:18
  • How are you trying to "call the value back"? Are you using Request["total"], or are you are dynamically adding back the hidden inputs, then trying to pull the value from that? Commented Jun 3, 2011 at 21:29

2 Answers 2

2

Have you verified that the resulting input tag as the ID of "total"? By default, in Webforms, the actual client-side ID is prefixed with the parent's Id (and a delimiting character); this helps to ensure that IDs are unique. One way to get the real client-side Id is to pull the value from the ClientID property of the control, but you should only look at that value once it has been put in a Controls collection.

Sign up to request clarification or add additional context in comments.

2 Comments

Brian is right. If you are using ASP.NET 4.0, you can set ClientIDMode = "Static", so that the id remains as "total"
I was able to get the value using alert(document.getElementById("total").value); but i will try that out anyways
1

These controls are dynamically created and they have to be created in each postback. However, these should be built before Page_Load preferably in Page_Init event handler. If these are created in Page_Load, the view state has already been processed and the control can't be set from the posted value.

3 Comments

Where are you creating these controls?
Page_Load, I create them with a table that i use so I only have to query the database once.
Move the create code in Page_Init. Take a look at the article geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.