Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I create a text box and button dynamically in C# .net windows application by using following code.

private void lblNewUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
       {
           //UserName
           Label lblUN = new Label();
           lblUN.Text = "UserName";
           lblUN.Font = new Font(lblUN.Font, lblUN.Font.Style | FontStyle.Bold);
           lblUN.BackColor = Color.Transparent;
           lblUN.ForeColor = Color.White;
           lblUN.AutoSize=true;
           lblUN.Location = new Point(30,300);
           this.Controls.Add(lblUN);
           //Password
           Label lblPWD = new Label();
           lblPWD.Text = "Password";
           lblPWD.Font = new Font(lblPWD.Font,lblPWD.Font.Style|FontStyle.Bold);
           lblPWD.BackColor = Color.Transparent;
           lblPWD.ForeColor = Color.White;
           lblPWD.AutoSize = true;
           lblPWD.Location = new Point(30,350);
           this.Controls.Add(lblPWD);
           // Save Button
           Button btnSAVE = new Button();
           btnSAVE.Text="Save";
           btnSAVE.Font = new Font(btnSAVE.Font,btnSAVE.Font.Style|FontStyle.Bold);
           btnSAVE.Size = new Size(50,25);
           btnSAVE.BackColor = Color.Blue;
           btnSAVE.ForeColor = Color.White;
           btnSAVE.Location = new Point(100, 450);
           this.Controls.Add(btnSAVE);
        }

Now when I press this Save button,I want to store the value which is entered in that text box into database. Kindly send me answer.Where shouid I use the following codes.


C#
btnobj1.Click+=new System.EventHandler(ButtonClickHandler);

private void ButtonClickHandler(object sender, EventArgs e) {
    Button button = sender as Button;
    if (button.Text == "Save") {
        //Do whatever
    }
}



thanks,
Viswanathan.M


[Edited]Code is blocked in "pre" tags[/Edited]
Posted
Updated 11-Aug-11 3:10am
v6
Comments
reid0588 11-Aug-11 6:48am    
where do you want to store the number? do you want it displayed somewhere or saved into a database?
Viswanthan.M. 11-Aug-11 6:54am    
into database.
[no name] 11-Aug-11 9:50am    
Check this CodeProjectForums.com

C#
btnobj1.Click+=new System.EventHandler(ButtonClickHandler);

private void ButtonClickHandler(object sender, EventArgs e) {
    Button button = sender as Button;
    if (button.Text == "Save") {
        //Do whatever
    }
}
 
Share this answer
 
Comments
Toniyo Jackson 11-Aug-11 6:55am    
Correct 5!
Viswanthan.M. 11-Aug-11 7:08am    
SqlCommand cmd=new SqlCommand("insert into login values('"++"')");

In this Line ,I don't know how to mention dynamically created text box. pls help me.
[no name] 11-Aug-11 7:30am    
Move the text box declaration line to the class level. Then you will be able to access it from anywhere in the class.
Herman<T>.Instance 11-Aug-11 7:39am    
correct
Viswanthan.M. 11-Aug-11 8:36am    
btnobj1.Click+=new System.EventHandler(ButtonClickHandler); i Can't use this line...(ButtonClickHandler)
you need to create the eventhandler for the button OnClick event.

btnobj1.Click += new EventHandler(btnobj1_Click);


and you should have the method
private void btnobj1_Click(Object sender, EventArgs e)
{
// here you can play with the txtobj3.Text
}
 
Share this answer
 
Comments
Viswanthan.M. 11-Aug-11 7:14am    
SqlCommand cmd=new SqlCommand("insert into login values('"++"')"); In this Line ,I don't know how to mention dynamically created text box. pls help me.
Herman<T>.Instance 11-Aug-11 7:46am    
string MyQuery = string.Format("insert into login values('{0}'", txtobj3.Text));

SqlCommand cmd=new SqlCommand(MyQuery);
Herman<T>.Instance 11-Aug-11 7:21am    
where have you declared the dynamically created textbox?
Viswanthan.M. 11-Aug-11 7:41am    
i have use one link button , in the click event of link button,
i create one text box and one button dynamically.
Herman<T>.Instance 11-Aug-11 7:42am    
see solution 1, answer Shameel! otherwise add code of complete class

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