Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > respond to button click in custom server control

Reply
Thread Tools

respond to button click in custom server control

 
 
Martin
Guest
Posts: n/a
 
      04-26-2005
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);
}
}
}



 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=
Guest
Posts: n/a
 
      04-26-2005
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);
> }
> }
> }
>
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fire both event button click and textchanged when button is click and text is changed Amy ASP .Net 0 06-01-2006 02:33 PM
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Building Controls 1 05-28-2004 06:23 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net 2 05-25-2004 11:45 PM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Datagrid Control 0 05-25-2004 01:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57