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);
}
Related
I'm trying to create a panel with somes button when you click on it, it show specific form inside the panel. The panel is blocked on the first form showed even when you try to remove it.
private void button1_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
dashboard.TopLevel = false;
panel1.Controls.Add(dashboard);
panel1.Controls.Remove(settings);
dashboard.FormBorderStyle = FormBorderStyle.None;
dashboard.Dock = DockStyle.Fill;
dashboard.Show();
settings.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
settings.TopLevel = false;
panel1.Controls.Remove(dashboard);
panel1.Controls.Add(settings);
settings.FormBorderStyle = FormBorderStyle.None;
settings.Dock = DockStyle.Fill;
settings.Show();
dashboard.Hide();
}
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();
I have created link button from code behind and binded with action like this
but this method Render is never invoked on click event. What I missed here ?
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Click += new EventHandler(Redirect);
pnlContent.Controls.Add(lbContent);
pnlMain.Controls.Add(pnlContent);
private void Redirect(object sender, EventArgs e)
{
}
Try this,
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Click += new EventHandler(Redirect);
Redirect(this, EventArgs.Empty);
private void Redirect(object sender, EventArgs e)
{
}
Are you missing runat="server" tag? however the compilator should complain about that.
Try this it will work.
In your aspx page
<form id="form1" runat="server">
<div>
</div>
</form>
And the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
Panel pnlMain = new Panel();
Panel pnlContent = new Panel();
LinkButton lbContent = new LinkButton();
lbContent.Text = "click";
lbContent.Click += new EventHandler(Redirect);
pnlContent.Controls.Add(lbContent);
pnlMain.Controls.Add(pnlContent);
this.form1.Controls.Add(pnlMain);
}
private void Redirect(object sender, EventArgs e)
{
}
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);
I want to create ajax dragpanel dynamically and add the controls and panel dynamically.I used the below code for create the control dynamically.But I can`t able to drag it.
enter code here
protected void Page_PreInit(object sender, EventArgs e)
{
DragPanelExtender drg = new DragPanelExtender();
drg.ID = "drg_1";
drg.DragHandleID = "pnl_1";
drg.TargetControlID = "pnl_2";
Panel p1 = new Panel();
p1.ID = "pnl_1";
Panel p2 = new Panel();
p2.ID = "pnl_2";
Label l1 = new Label();
l1.Text = "First";
TextBox t = new TextBox();
t.Text = "Enter text";
p2.Controls.Add(l1);
p2.Controls.Add(t);
p1.Controls.Add(p2);
Page.Controls.Add(p1);
}
How to I create drag controls inside the panel