Why is the control's in panel gets overwrite everytime? - c#

Everytime the button's click event get fired, the label (or any other control) in panel gets overwrite by the new one! Here is the button event.
protected void Button3_Click(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.ID = "name";
lbl.Text = Profession.SelectedItem.ToString();
Panel1.Controls.Add( lbl);
}
It everytime remove the previous label and add new label with the selected item in DropDownList

Label is getting initialized on every click that is the problem
protected void Button3_Click(object sender, EventArgs e)
{
Label lbl = new Label();//here on every click new label initialized
lbl.ID = "name";
lbl.Text = Profession.SelectedItem.ToString();
Panel1.Controls.Add(lbl);
}
Replace above code by
Label lbl = new Label();
protected void Button3_Click(object sender, EventArgs e)
{
lbl.ID = "name";
lbl.Text = Profession.SelectedItem.ToString();
if(!Panel1.Controls.Contains(lbl)) //Check here if label already added
Panel1.Controls.Add(lbl);
}

Look at the scope of your Label. You are creating a new instance of Label on every click. Take this on class level
Label lbl = new Label();

Related

How do I store first control in flowLayoutPanel c#?

I am new to C# and I am using windows forms.
I have flowLayoutPanel and I programmatically add some buttons to it .
What I am trying to do is: I want to save the first button located in the flowLayoutPanel into ButtonToSave object.
flowLayoutPanel1.FlowDirection= FlowDirection.LeftToRight
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button ButtonToSave = new Button();
ButtonToSave = "First button in flowLayoutPanel1"
}
Anyone knows how to save the first button located in flowLayoutPanel1 into ButtonToSave when StoreTheFirstButton event is raised?
Thank you
Try code below. You may want to look at my response at following posting (How to (create and) add components to a Table Layout?) :
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += new EventHandler(StoreTheFirstButton_Click);
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
Button ButtonToSave = button;
//ButtonToSave = "First button in flowLayoutPanel1";
}

Troubles with dynamic c# asp.net controls update

I'm currently working on a website using c# and asp.net. For this purpose, I need to create dynamic controls but I enconter some issues. I already read official documentation and searched for lots of tutorial but unfortunately, no one allowed me to fix this problem.
Here is a very simplified example of what I'm trying to do;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateControls();
else
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
The aim here would just be to change the buttons' text when clicking on one of them. "Page_Load" method is correctly called as well as the "UpdatePage" one, but in the latter, Button1 and Button2 controls have disappeared (they are not in the panel controls anymore) and an NullPointer exception is obviously raised.
Would anybody have an explanation ? I know I probably missed something about page life cycle but could not find any clear solution anywhere.
Thanks a lot !
You are creating the controls the first time the page is loaded, but the Page_Load event is too late to add controls to the page and have WebForms know about that.
On the initial page load, somewhere between the OnInit and Page_Load, WebForms makes a note of what controls are currently on the page and sets them up in the view state and all of that stuff, so that they next time you post back it knows those controls should be there. If you don't add your controls until Page_Load, WebForms isn't really paying attention any more to what you're adding to the page, so they next time you post back it doesn't know to put those controls on the page.
Move your CreateControls call into the OnInit method. This will tell WebForms at the appropriate time to create the controls (about the same time that any controls from the .aspx markup get added, although slightly later). Then WebForms will be aware of those controls and will apply any view state necessary (if it's a postback), and then finally on Page_Load you can muck with the control data with your UpdatePage call.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
Think of OnInit as "put all of the controls on the page and wire up event handlers".
Think of Page_Load as "put data into the controls that are already there".
The controls that you are creating dynamically will be lost on postback. Try this:
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
Will try it:
protected String TextButton1
{
get { return (String) ViewState["TextButton1"]; }
set { ViewState["TextButton1"] = value; }
}
protected String TextButton2
{
get { return (String)ViewState["TextButton2"]; }
set { ViewState["TextButton2"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
UpdatePage();
}
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
button1.Click += new System.EventHandler(_ClickEvent1);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
button2.Click += new System.EventHandler(_ClickEvent2);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
}
protected void _ClickEvent1(object sender, EventArgs e)
{
TextButton1 = "test";
Button b = (Button) sender ;
b.Text = TextButton1;
}
protected void _ClickEvent2(object sender, EventArgs e)
{
TextButton2 = "test";
Button b = (Button)sender;
b.Text = TextButton2;
}

Dynamcially Creating a table inside a panel in C#.Net

Here is my code
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
}
I cant see the my label text when i click the button
Any help appreciate
Thanks
You must add your panel inside of any control which exists on your page.
You have to add the Panel to some control in your web page or your top level form element if you don't have anywhere else to put it.
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
this.Form.Controls.Add(panel1); // YOU ARE MISSING THIS
}
You need to add the Panel to the page:
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
//Do this
SomeControlOnYourPage.Controls.Add(panel1);
}

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

Why won't my SelectedIndexChanged event fire in Button_Click?

protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
When this code executes in the Button1_Click event, db_SelectedIndexChanged doesn't execute. However, when I place the same Button1_Click code block in the Page_Load event, db_SelectedIndexChanged executes.
What may be the reason behind this?
Try to put
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
In the Page_Load event.
Don't wrap db.SelectedIndexChanged += db_SelectedIndexChanged; in !Page.IsPostBack as the events need to be wired up on each load
You are creating a dynamic control. The event will not fire unless you create the control in the PreInit method of the page.
protected void Page_PreInit(object sender, EventArgs e)
{
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
tc.Controls.Add(db);
}
Check Page Life cycle for more info.
protected void Button1_Click(object sender, EventArgs e)
{
TableRow tb = new TableRow();
TableCell tc = new TableCell();
DropDownList db = new DropDownList();
db.Items.Add("Bangalore");`
db.Items.Add("Mandya");
db.Items.Add( "Hassan");
tc.Controls.Add(db);
tb.Controls.Add(tc);
Table1.Controls.Add(tb);
db.SelectedIndexChanged += db_SelectedIndexChanged;
db.AutoPostBack = true;
db_SelectedIndexChanged(null,null); // use this line, i hope it will work now.
}
private void db_SelectedIndexChanged(object sender, EventArgs e)
{
label.text = "welcome";
}
You can try this one.

Categories