Hi,
I have produced a custom server control that simple outputs a row of 26
buttons, one button for each letter of the english alphabet.
now what I would like to do is catch the button click inside the server
control when it is clicked on.
however when I click on a button at present I get an error saying that the
An error has occurred because a control with auto-generated id
'AtoZcontrol1' could not be located to raise a postback event. To avoid this
error, explicitly set the ID property of controls that raise postback
events.
I attempted to follow this advice but to no avail.
I have include my code below in the hope that somebody could point out my
error or offer me any advice.
many thanks in advance.
cheers
martin.
namespace serverControls
{
/// <summary>
/// Summary description for AtoZcontrol.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:AtoZcontrol runat=server></{0}:AtoZcontrol>")]
public class AtoZcontrol : System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
{
//public event buttonClicked();
private string text = "My A to Z Control";
Panel pnl;
string[] alphabet;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
if (Page != null)
{
Page.RegisterRequiresPostBack(this);
}
}
protected override void CreateChildControls()
{
alphabet = new string[26];
alphabet[0] = "A";
alphabet[1] = "B";
alphabet[2] = "C";
alphabet[3] = "D";
alphabet[4] = "E";
alphabet[5] = "F";
alphabet[6] = "G";
alphabet[7] = "H";
alphabet[8] = "I";
alphabet[9] = "J";
alphabet[10] = "K";
alphabet[11] = "L";
alphabet[12] = "M";
alphabet[13] = "N";
alphabet[14] = "O";
alphabet[15] = "P";
alphabet[16] = "Q";
alphabet[17] = "R";
alphabet[18] = "S";
alphabet[19] = "T";
alphabet[20] = "U";
alphabet[21] = "V";
alphabet[22] = "W";
alphabet[23] = "X";
alphabet[24] = "Y";
alphabet[25] = "Z";
pnl = new Panel();
Button btn;
for (int i = 0 ; i < alphabet.Length ; i++)
{
btn = new Button();
btn.ID = "btn_" + i.ToString();
btn.Text = alphabet[i].ToString();
btn.Click += new EventHandler(btnSubmit_Click);
pnl.Controls.Add(btn);
}
base.CreateChildControls();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
System.Web.HttpContext.Current.Trace.Write("GOT
MESSAGE","btnSubmit_Click");
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
pnl.RenderControl(output);
}
}
}