473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

respond to button click in custom server control

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);
}
}
}

Nov 19 '05 #1
1 2597
hi martin,

Its all got to do with the Sequence of events happening in the control life
cycle.
Comment this line in the onInit method - Page.RegisterRequiresPostBack(this);
and your control should work.

Or by any chance if u want to handle postback change events here's how u
need to tackle it.

Basically RegisterRequiresPostBack tells the containing page that our
control needs to be notified when post-back happens. In the .NET framework,
the callbacks are accomplished through events and delegates. The Page class
has a method RegisterRequiresPostBack that needs to be called and handed a
reference to our control. This method call registers our control with Page
for receiving events.

For our control to receive and register events you have to implement the
IPostBackDataHandler, this will include two stubs for handling postback data.
Include whatever code you might want to handle when the page is posted back
in these two events (ie: when the posted data is changed, like in a textbox
where the text is changed after loading and the page is posted back to the
server, this postdatachanged event is fired)

public void RaisePostDataChangedEvent()
{
// TODO: Add AtoZcontrol.RaisePostDataChangedEvent implementation
}

public bool LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
// TODO: Add AtoZcontrol.LoadPostData implementation
return false;
}

You might have got some idea on this, for more information refer the MSDN
docs for Page.RegisterRequiresPostBack(this);

Hope this explanation might have helped you,
Regds
Kannan.V
"Martin" wrote:
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);
}
}
}

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: panik | last post by:
Hi, I have a Control, a SearchBox class, that is derived from UserControl. It is placed inside another UserControl (a page structure) that is placed inside a custom page class (derived from...
2
by: Sedef | last post by:
Hi, i'm trying to create a custom Button user control which will be derived from System.Web.UI.WebControls.Button. the normal server side Button class creates some client side javascript code for...
1
by: Divya | last post by:
Hello This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I...
2
by: prasadrmarathe | last post by:
Hi, I have created one custom buton server control which will shown on it the nuber of time it was clicked as its text. I have added the custom control in my another projects tool box but i...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
1
by: tranky | last post by:
Hi boys.. ....i've a question for you! I've a custom control with a button. Well... I need to create that: <mytagprefix:mytag runat="server" onButtonClick="Button_Click"> And in the code...
3
by: Kannan | last post by:
Hi, I am trying to created Outloook Add-in Com in outlook using C#. I have seen this URL for developing this sample http://support.microsoft.com/?kbid=302901 When I executed this program it...
3
by: Jay | last post by:
I am on the 2.0 framework and have run the c:\windows\microsoft.net \framework\v1.1.4322\aspnet_regiis.exe -c and had no success. About half of the buttons on my webforms are firing and the other...
19
Frinavale
by: Frinavale | last post by:
I'm in the middle of implementing a custom Ajax enabled Server Control. At this point I need help finding the answer to an Ajax Framework question...here it goes: I have a Server Control that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.