最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Updating TextBox on the client with Javascript and reading the value from the Server - Stack Overflow

programmeradmin6浏览0评论

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 plex and costly, but I admittedly am not that skilled in web dev.

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 plex and costly, but I admittedly am not that skilled in web dev.

Share Improve this question edited Dec 10, 2009 at 20:33 Chad asked Dec 10, 2009 at 19:46 ChadChad 24.7k54 gold badges202 silver badges332 bronze badges 1
  • 1 Are you sure you don't have something in Page_Load that is resetting the textbox value? – Josh Stodola Commented Dec 10, 2009 at 19:49
Add a ment  | 

5 Answers 5

Reset to default 5

By any chance, are you marking your textbox as "readonly" and then trying to do an update ?

http://aspadvice./blogs/joteke/archive/2006/04/12/16409.aspx

Server ignores any updates if the textbox1.ReadOnly=true;

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.

Take a look at the asp 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 ment, 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...

One alternative is to wrap your text box in an Update Panel and not use javascript but rather update it's value server side.

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

发布评论

评论列表(0)

  1. 暂无评论