B
Biguana
Hi there,
Wondering if anyone can help as this is driving me nuts.
I have a c# page with a lot of dynamically created controls. Some are
user controls where the user can select which section to complete using
radiobuttons. I create the groups of radiobuttons (always with the
first one in the group selected) and give them event handlers (all
pointing to the same function), and then the page gets posted back
(either by a radio button or another control).
The problem is, on the postback all the selections (from the previous
creation code I guess) fire their events and all the associated code
which I really don't want. I can't see a way to distinguish the real
selection from the default selection events as they all fire. I want
it to be as if the controls were created at design time - then they
wouldn't fire events! I've tried adding the event handler before and
after the attachment to the placeholder, and in different stages of the
page lifecycle, all with the same results.
Simplified code for repeatability below. Thanks for any help you can
offer.
Tim
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder
PlaceHolder1;
private void Page_Load(object sender, System.EventArgs
e)
{
for (int i = 0; i< 10;i++)
{
for (int j = 0; j< 10; j++)
{
RadioButton rbTemp = new
RadioButton();
rbTemp.ID = i.ToString() + "_"
+ j.ToString();
rbTemp.GroupName =
i.ToString();
//rbTemp.CheckedChanged += new
EventHandler(MyHandler);
if (j==0 && !IsPostBack)
rbTemp.Checked = true;
rbTemp.AutoPostBack = true;
PlaceHolder1.Controls.Add(rbTemp);
((RadioButton)
PlaceHolder1.FindControl(i.ToString() + "_" +
j.ToString())).CheckedChanged += new EventHandler(MyHandler);
}
LiteralControl x = new
LiteralControl();
x.Text = "<br>";
PlaceHolder1.Controls.Add(x);
}
}
private void MyHandler(object sender,
System.EventArgs e)
{
}
// Put user code to initialize the page here
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the
ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender,
System.EventArgs e)
{
}
}
Wondering if anyone can help as this is driving me nuts.
I have a c# page with a lot of dynamically created controls. Some are
user controls where the user can select which section to complete using
radiobuttons. I create the groups of radiobuttons (always with the
first one in the group selected) and give them event handlers (all
pointing to the same function), and then the page gets posted back
(either by a radio button or another control).
The problem is, on the postback all the selections (from the previous
creation code I guess) fire their events and all the associated code
which I really don't want. I can't see a way to distinguish the real
selection from the default selection events as they all fire. I want
it to be as if the controls were created at design time - then they
wouldn't fire events! I've tried adding the event handler before and
after the attachment to the placeholder, and in different stages of the
page lifecycle, all with the same results.
Simplified code for repeatability below. Thanks for any help you can
offer.
Tim
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder
PlaceHolder1;
private void Page_Load(object sender, System.EventArgs
e)
{
for (int i = 0; i< 10;i++)
{
for (int j = 0; j< 10; j++)
{
RadioButton rbTemp = new
RadioButton();
rbTemp.ID = i.ToString() + "_"
+ j.ToString();
rbTemp.GroupName =
i.ToString();
//rbTemp.CheckedChanged += new
EventHandler(MyHandler);
if (j==0 && !IsPostBack)
rbTemp.Checked = true;
rbTemp.AutoPostBack = true;
PlaceHolder1.Controls.Add(rbTemp);
((RadioButton)
PlaceHolder1.FindControl(i.ToString() + "_" +
j.ToString())).CheckedChanged += new EventHandler(MyHandler);
}
LiteralControl x = new
LiteralControl();
x.Text = "<br>";
PlaceHolder1.Controls.Add(x);
}
}
private void MyHandler(object sender,
System.EventArgs e)
{
}
// Put user code to initialize the page here
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the
ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender,
System.EventArgs e)
{
}
}