I set html textarea in asp.net page with out runat="server" and I need set the text in string variable by C# in code behind
6 Answers
An ASP.NET TextBox control with the TextMode property set to MultiLine will generate an Html TEXTAREA control.
So instead of crafting Html TEXTAREA manually, you can just drop a TextBox control and have:
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
and then in your code behind:
TextBox2.Text = "... blah blah blah...";
Comments
Use Request.Form Collection http://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx to retrieve values..
7 Comments
HttpRequest.Form collection is readonly(for logical reasons).I guess your trying to accomplish is to populate your textbox with the variable on the server side with out runat="server" set to the control.
You can do that with ajax and simple jquery.
Lets say your have this on your YourPage.aspx
<input id="txtMyTextBox" type="text" />
and next is a simple jquery
function getMytextValue() {
$.ajax({
url: "/MainPage.aspx/YourServerSideFunction",
type: "post",
data: "{ }",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (data) {
//Get your value from JSON data.d;
$('#txtMyTextBox').val(data.d);
},
error: function (request, status, err) {
//Do something here for error;
}
});
}
And now just call the jquery on what ever event you like.
PS: dont forget to create a [WebMethod] on your code behind.
Hope this help you! :D
Comments
couldn't you just save the value to a variable in your codebehind file and then on the webform itself just reference the variable by some manner such as <%= varName =>??
The above seem like a lot of work just to reference a variable that you can set quite easily in C# and then pull in the page load.
Comments
You can set data in code behind by using ViewData and can access them in html as explained in above link.
runat="server". It's not so bad ;)