Reputation: 24699
I used Javascript on the client to update an ASP Textbox control which was rendered as an HTML textbox.
When I tried to access the value in the textbox control on the server, I wasn't too surprised that the Textbox's updated value was not accessible through the normal Text property of the TextBox server control, since the state of that value, I assume was probably saved off in the ViewState and the Server was unaware that Client script had changed it.
Is the best way to get the updated value using something like:
Request.Form(TextBox.UniqueID)?
I suspect that because the textbox is populated within a row of a GridView control and the row was added at run time when doing a DataBind, that I may be getting different results than if the TextBox were drawn on the control at Design time.
I confirmed that the value placed into the textbox by the client is accessible via this:
Request.Form( CType(Row.Cells.Item(4).FindControl("txtTotal"), TextBox).UniqueID)
but not accessible via this:
CType(Row.Cells.Item(4).FindControl("txtTotal"), TextBox).Text
the latter returning an empty string.
I think these two lines tell the whole story.
It seems ugly to have two properties to get the value of the textbox and even uglier when you consider that they return different results.
Is there a better way or is this just client side development in all its glory?
I'm always surprised when people say that web development now days is as easy as client (eg winform) apps now that ASP.NET keeps track of state. To me, web development is always more complex and costly, but I admittedly am not that skilled in web dev.
Upvotes: 3
Views: 4179
Reputation: 7713
Make sure you have runat="server" in your control. If you can't access it from the code-behind, that's usually the issue.
Also, make sure you aren't already doing something to that text box somewhere else, like in your page_load, or in other JavaScript on your page.
-Edit-
Something important to note: just because you can read something in Request.form doesn't necessarily mean you can read it as a .net object. Request.form is just reading the raw request information that was sent from the form.
You are trying to read your text box in the the middle of rows. What are these rows part of, a table, or some other sort of control? Is the control that contains the text box also an asp.net control that also has runat=server set?
-Another Edit-
You mentioned in another reply that your text box is in a datagrid. Where are you binding this grid? Are you re-binding it before you try to read the text box (like in Page_load)? If you are, then you are resetting the entire grid back to it's original state before reading it's values.
Upvotes: 0
Reputation: 11626
By any chance, are you marking your textbox as "readonly" and then trying to do an update ?
http://aspadvice.com/blogs/joteke/archive/2006/04/12/16409.aspx
Server ignores any updates if the textbox1.ReadOnly=true;
Upvotes: 5
Reputation: 18104
No, you should be able to read it from the property as you would normally regardless of whether the input was entered via keyboard or via javascript. I expect your problem lies elsewhere. Also to paraphrase Aaron "Make sure you are not resetting the value in your codebehind prior to trying to read the value" I suggest you step debug and examine it's value.
Upvotes: 1
Reputation: 29986
One alternative is to wrap your text box in an Update Panel and not use javascript but rather update it's value server side.
Upvotes: 0
Reputation: 499132
Take a look at the asp.net page lifecycle - it may give you the answer, as there isn't enough detail in your question.
If you are changing the textbox value via JS in the client, then when posting back (submitting the form) should give you the new value.
As noted by Josh's comment, if you are binding or updating the control during the Page_Load or one of the other early lifecycle events, that would clear it out. Difficult to say without looking at the code...
Upvotes: 0