Microos
Microos

Reputation: 1768

C# how to get HTML controls created dynamically

I generated a HTMLTextArea using string and Response.Write():

string area = "<textarea id=\"myArea{0}\" cols=\"30\" name=\"S1\" rows=\"5\" runat=\"server\"></textarea>";

Response.Write(String.Format(area,1));

After this, I don't know how to get the object of this myArea1.

Are there any way I can achieve this goal?

Upvotes: 0

Views: 691

Answers (3)

codeandcloud
codeandcloud

Reputation: 55288

The proper way to add System.Web.UI.HtmlControls. will be,

var newTextArea = new HtmlTextArea()
{
    ID = string.Format("myArea{0}", 1),
    Name = string.Format("S{0}", 1),
    Cols = 30,
    Rows = 5
};
Page.Controls.Add(newTextArea);

Then you can access it like,

var myTextArea = Page.FindControl("myArea1") as HtmlTextArea;

Upvotes: 1

Michal Klouda
Michal Klouda

Reputation: 14521

You don't really need to access the TextArea object itself if you are only interested in getting the user input, which you should be able to find in Request.Form collection on form submission.

Upvotes: 0

Null
Null

Reputation: 511

You can try this.

  HtmlTextArea txt = (HtmlTextArea)(Page.FindControl("myArea1"));
  string value = txt.Value;

Refer to this.

Upvotes: 0

Related Questions