respond to button click in custom server control

Discussion in 'ASP .Net' started by Martin, Apr 26, 2005.

  1. Martin

    Martin Guest

    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.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);
    }
    }
    }
    Martin, Apr 26, 2005
    #1
    1. Advertising

  2. 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.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);
    > }
    > }
    > }
    >
    >
    >
    >
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=, Apr 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    821
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    961
  3. Mufasa
    Replies:
    7
    Views:
    536
    Homer J. Simpson
    Aug 14, 2007
  4. mhylden

    how to make imagebutton respond to click but not return key

    mhylden, Sep 28, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    151
    mhylden
    Sep 28, 2005
  5. mhylden
    Replies:
    1
    Views:
    129
    mhylden
    Sep 28, 2005
Loading...

Share This Page