Button event does not execute
-
Hi Guys, I am loading buttons on a page load event dynamically. The buttons are added to a placeholder control on a aspx page. The problem I am having is that the dynamically created button event below (topic_Click) doesn't get fired. Bascially, I am trying to add topics to aobjective. Could you please help me. I would highly appreciate it.
protected void Page_Load(object sender, EventArgs e)
{if (Session\["s"\] == null) { AddObjective(); AddTopic(); } else if (Session\["s"\] != null) { List list = new List(); list = Session\["s"\] as List; foreach (var item in list) { AddtoPlaceHolder(item); } } }
private void AddObjective()
{
Panel panel = new Panel();
Button objective = new Button();
objective.Text = "Add objective";
objective.Click += new EventHandler(objective_Click);
panel.Controls.Add(objective);
placeholder.Controls.Add(panel);AddtoPlaceHolder(panel); }
void AddtoPlaceHolder(Panel panel)
{
if (Session["s"] == null)
{
List list = new List();
list.Add(panel);
Session.Add("s", list);
}
else if(Session["s"] !=null)
{
List list = Session["s"] as List;
list.Add(panel);
Session.Add("s", list);
}
}void objective_Click(object sender, EventArgs e)
{
if (Session["s"] != null)
{
AddObjective();
}
}void topic_Click(object sender, EventArgs e)
{
throw new NotImplementedException(); =======> Problem doesn't execute
}private void AddTopic()
{
Panel paneltopic = new Panel();
paneltopic.ID = Guid.NewGuid().ToString();
Button topic = new Button();
topic.ID = Guid.NewGuid().ToString();
topic.Text = "Insert topic";
topic.Click += new EventHandler(topic_Click);
paneltopic.Controls.Add(topic);
placeholder.Controls.Add(paneltopic);AddtoPlaceHolder(paneltopic);
}
-
Hi Guys, I am loading buttons on a page load event dynamically. The buttons are added to a placeholder control on a aspx page. The problem I am having is that the dynamically created button event below (topic_Click) doesn't get fired. Bascially, I am trying to add topics to aobjective. Could you please help me. I would highly appreciate it.
protected void Page_Load(object sender, EventArgs e)
{if (Session\["s"\] == null) { AddObjective(); AddTopic(); } else if (Session\["s"\] != null) { List list = new List(); list = Session\["s"\] as List; foreach (var item in list) { AddtoPlaceHolder(item); } } }
private void AddObjective()
{
Panel panel = new Panel();
Button objective = new Button();
objective.Text = "Add objective";
objective.Click += new EventHandler(objective_Click);
panel.Controls.Add(objective);
placeholder.Controls.Add(panel);AddtoPlaceHolder(panel); }
void AddtoPlaceHolder(Panel panel)
{
if (Session["s"] == null)
{
List list = new List();
list.Add(panel);
Session.Add("s", list);
}
else if(Session["s"] !=null)
{
List list = Session["s"] as List;
list.Add(panel);
Session.Add("s", list);
}
}void objective_Click(object sender, EventArgs e)
{
if (Session["s"] != null)
{
AddObjective();
}
}void topic_Click(object sender, EventArgs e)
{
throw new NotImplementedException(); =======> Problem doesn't execute
}private void AddTopic()
{
Panel paneltopic = new Panel();
paneltopic.ID = Guid.NewGuid().ToString();
Button topic = new Button();
topic.ID = Guid.NewGuid().ToString();
topic.Text = "Insert topic";
topic.Click += new EventHandler(topic_Click);
paneltopic.Controls.Add(topic);
placeholder.Controls.Add(paneltopic);AddtoPlaceHolder(paneltopic);
}
protected void topic_Click(object sender, EventArgs e)
{
throw new NotImplementedException(); =======> Problem doesn't execute
}private void AddTopic()
{
Panel paneltopic = new Panel();
paneltopic.ID = Guid.NewGuid().ToString();
Button topic = new Button();
topic.ID = Guid.NewGuid().ToString();
topic.Text = "Insert topic";
topic.Click += new System.EventHandler(this.topic_Click);
paneltopic.Controls.Add(topic);
placeholder.Controls.Add(paneltopic);AddtoPlaceHolder(paneltopic);
}
-
protected void topic_Click(object sender, EventArgs e)
{
throw new NotImplementedException(); =======> Problem doesn't execute
}private void AddTopic()
{
Panel paneltopic = new Panel();
paneltopic.ID = Guid.NewGuid().ToString();
Button topic = new Button();
topic.ID = Guid.NewGuid().ToString();
topic.Text = "Insert topic";
topic.Click += new System.EventHandler(this.topic_Click);
paneltopic.Controls.Add(topic);
placeholder.Controls.Add(paneltopic);AddtoPlaceHolder(paneltopic);
}
-
if you copy and past the code from another page visual studio may change the name of the button check the the ButtonID if its correct with the code
You should probably tell the OP that, and not me. The button ID has nothing to do with generating a postback to the server. In fact, you don't need the .ID attribute in code behind, it's there to support java script and css on the client side. His problem is that he didn't generate a new event object, and just made a reference to an uninitialized event object.
-
You should probably tell the OP that, and not me. The button ID has nothing to do with generating a postback to the server. In fact, you don't need the .ID attribute in code behind, it's there to support java script and css on the client side. His problem is that he didn't generate a new event object, and just made a reference to an uninitialized event object.