473,651 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.WebC ontrol,
System.Web.UI.I NamingContainer
{
//public event buttonClicked() ;
private string text = "My A to Z Control";
Panel pnl;
string[] alphabet;

[Bindable(true),
Category("Appea rance"),
DefaultValue("" )]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}
protected override void OnInit(EventArg s e)

{
base.OnInit (e);
if (Page != null)
{
Page.RegisterRe quiresPostBack( this);
}

}
protected override void CreateChildCont rols()
{
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(bt nSubmit_Click);
pnl.Controls.Ad d(btn);
}
base.CreateChil dControls();
}

private void btnSubmit_Click (object sender, EventArgs e)
{
System.Web.Http Context.Current .Trace.Write("G OT
MESSAGE","btnSu bmit_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(HtmlText Writer output)
{
pnl.RenderContr ol(output);
}
}
}

Nov 19 '05 #1
1 2622
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.RegisterRe quiresPostBack( 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 RegisterRequire sPostBack 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 RegisterRequire sPostBack 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
IPostBackDataHa ndler, 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 RaisePostDataCh angedEvent()
{
// TODO: Add AtoZcontrol.Rai sePostDataChang edEvent implementation
}

public bool LoadPostData(st ring postDataKey,
System.Collecti ons.Specialized .NameValueColle ction postCollection)
{
// TODO: Add AtoZcontrol.Loa dPostData implementation
return false;
}

You might have got some idea on this, for more information refer the MSDN
docs for Page.RegisterRe quiresPostBack( 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.W ebControls.WebC ontrol,
System.Web.UI.I NamingContainer
{
//public event buttonClicked() ;
private string text = "My A to Z Control";
Panel pnl;
string[] alphabet;

[Bindable(true),
Category("Appea rance"),
DefaultValue("" )]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}
protected override void OnInit(EventArg s e)

{
base.OnInit (e);
if (Page != null)
{
Page.RegisterRe quiresPostBack( this);
}

}
protected override void CreateChildCont rols()
{
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(bt nSubmit_Click);
pnl.Controls.Ad d(btn);
}
base.CreateChil dControls();
}

private void btnSubmit_Click (object sender, EventArgs e)
{
System.Web.Http Context.Current .Trace.Write("G OT
MESSAGE","btnSu bmit_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(HtmlText Writer output)
{
pnl.RenderContr ol(output);
}
}
}

Nov 19 '05 #2

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

Similar topics

0
1287
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 Page). All controls are added programmatically (Controls.Add()) In the SearchBox Control, I have an ImageButton, with an ImageClickEventhandler attached to it. The EventHandler is defined in the SearchBox class.
2
3281
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 its onclick event. it's something like: <input type="submit" name="Button1" value="Button" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="Button1" /> what i want to do is to be able to...
1
2724
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 have assigned to the button , is not getting invoked. Could anyone please go thru the code and let me know what I am missing here? Thanks in advance My custom control is as follows using System using System.Web using System.Web.UI using...
2
1881
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 am unable to drag the custom control on my webform. can u please tell what the problem is . i have provided the code below
11
4211
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. How I stop the buttons from posting back when they are clicked? Thanks
1
1504
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 behind of this page, i find the Button_Click function.
3
1990
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 created new custom button called "My Custom Button" in the Outlook menu bar. But when I tried to create one more button called "Forward Mail" (code is
3
5502
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 half are not, primarily anything on the Master is firing but those in the content pane are not. This was working fine yesterday!! I've reviewed all the code changes and can't seem to find a culprit. Here's one example of what I'm trying to...
19
4550
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 extends from a Panel and implements the IScriptControl interface: Public Class MyCustomControl Inherits Panel Implements IScriptControl This Server Control consists of a collection of Labels, Panels, Buttons, Literals, other Ajax...
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.