Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, is that possible to create controls and events in .cs file ? if so, how it is . please help me .

my code is like this

C#
this.Controls.Add(new TextBox { Text = "", ID = txtname.Text });
   this.Controls.Add(new Button { Text = "Submit", ID = "btnsubmit"}),


here i am creating the textbox control . but it was not getting the textbox value and also how can i create event to button

in Advance ,
Thank you.
Posted
Updated 16-Mar-15 23:58pm
v2
Comments
CHill60 17-Mar-15 5:55am    
Too vague. What are you trying to do and what have you tried so far?
GopiNath Tharra 17-Mar-15 6:01am    
i am trying to create controls from .cs file like the U.I with the filed of text box and with a button . how could i do that ?

Yes, sure.

In the top right corner of this site, you will find a search box; in there, you could try to type something like "c# controls" or "c# events", and be presented with tons of examples that you will be able to learn from. Don't hesitate, most of us have been learning a lot with this method.

QA are not meant to give you a full picture of controls and events in .NET world.
 
Share this answer
 
private TextBox txt;

protected void Page_Load(object sender, EventArgs e)
{
    this.txt = new TextBox { Text = "", ID = "SomeID" };
    this.Form.Controls.Add(this.txt);
    Button btn = new Button { Text = "Submit", ID = "btnsubmit" };
    btn.Click += btn_Click;
    this.Form.Controls.Add(btn);
}

protected void btn_Click(object sender, EventArgs e)
{
    string data = this.txt.Text;
}
 
Share this answer
 
Comments
GopiNath Tharra 17-Mar-15 6:25am    
thanks for your time. i am not able to get the value which in the textbox(UI side) to my code . briefly i want to do for loop the value which is entered into the textbox .
F-ES Sitecore 17-Mar-15 6:34am    
If you're looking to dynamically add controls then it's not a trivial task, you have to re-create the controls you have added each postback. There is an article with sample code that covers this in-depth here

http://forums.asp.net/t/1965764.aspx?Dynamically+creating+controls

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900