views:

122

answers:

2

If I have a standard HTML textbox: I can retrieve the value using Request.Form.

But how do I go about populating this textbox from the server-side? I tried

Request.Form["txtTest"] = "blah"; 

but got a readonly error.

+2  A: 

If you want to have first class support for accessing the control by its id on the server side (as per .net controls) you would need to make it so it has a runat="server" tag.

Otherwise you can set the value dynamicaly by having a property in your code behind and pulling in the value from this on the aspx page using databinding e.g.

<input type=text value="<%=PropertyInCodeBehindClass %>" />

and

public string PropertyInCodeBehindClass
{
  get;
  set;
}
AJM
As a side note, which may not be obvious, if the control has the runat="server" attribute set you can simply refer to the control by txtText.Value instead of going through Request.Form["txtTest"]
Chris Lively
I am in a situation where controls are being dynamically created on the client via Javascript. For example, I have a textbox and if the user is typing and exceeds the maxlength I create one on-the-fly. These textboxes have a function call tied to them so if I had the default one be a server-side control and the rest be client-side, when I reference them from Javascript there is the problem of referencing them by their id's.I am trying to set the code in Page_Load because if the user creates 5 textboxes and saves to the DB, I need to be able to recreate those textboxes when the re-login.
Jaden
One obvious question - have you considered using a text area?I'm not sure I get the JavaScript issue, if you want want to access a server control via JavaScript you can either use the ClientID property of the control e.g. getElementById('<%=TextBox.ClientID%>').value or if your using JQuery you can use the ends with operator to get round the ASP.NET prefix on control Ids e.g. $("[id$='TextBoxId']");If you want make them all in the Page_load and make them straightforward HTML could you not just build up the HTML in the code behind in the Page_load method then expose the HTML as a property?
AJM
A: 

Remember that at the point when your server code runs, the client side textbox doesn't exist. The html page hosting the control was already submitted to the server as new request, and the web browser is expecting you to respond with a completely new page. Until that response arrives the browser will leave the page displayed, but that's just a convenient shell. The DOM that held your textbox is gone, and you haven't created a new one yet. You can't directly change your response by updating a property in the request.

This means you need to use the server-side representation of the control. If it's a server control, you might try txtTest.Text = "blah"; Otherwise, you need to the find where you generate that input tag and alter things appropriately.

Always two there are; no more, no less. A request and a response.

Joel Coehoorn
I get what you are saying. But if I add the runat="server" attribute, my JS function call blows up when it tries to find the ID
Jaden
That's because asp.net mangles it. To get around this you can set the value with server code as described by AJM or use a full server control and write it's ClientID property out to javascript somewhere. Even better, maybe you can avoid the postback altogether and keep it all in javascript.
Joel Coehoorn