Handling a click event in custom server control

Discussion in 'ASP .Net Building Controls' started by Jowita, Jun 16, 2006.

  1. Jowita

    Jowita Guest

    I'm using VS 2005.
    I created a sample composite control with a textbox and a button. I'm
    having problems getting the button event.

    The control implements IPostBackEventHandler. Is it necessary if I
    just want to handle the event internally in the control?

    I added this code to CreateChildControls:
    protected override void CreateChildControls()
    {

    Table t = new Table();
    t.Rows.Add(new TableRow());
    t.Rows[0].Cells.Add(new TableCell());

    titleLabel = new Label();
    titleLabel.Text = "Advanced Search";
    titleLabel.ForeColor = this.ForeColor;
    t.Rows[0].Cells[0].Controls.Add(titleLabel);

    Literal lit = new Literal();
    lit.Text = "<br/><br/>";
    t.Rows[0].Cells[0].Controls.Add(lit);

    queryLabel = new Label();
    queryLabel.Text = "Keyword:";
    queryLabel.ForeColor = this.ForeColor;
    t.Rows[0].Cells[0].Controls.Add(queryLabel);

    Literal lit2 = new Literal();
    lit2.Text = "&nbsp;&nbsp;";
    t.Rows[0].Cells[0].Controls.Add(lit2);

    queryTextBox = new TextBox();
    t.Rows[0].Cells[0].Controls.Add(queryTextBox);

    // This is when I thought I hooked up the event handler
    function to the button's Click event
    submitButton = new Button();
    submitButton.Text = "Submit";
    submitButton.Click += new
    EventHandler(this.submitButton_Click);
    t.Rows[0].Cells[0].Controls.Add(submitButton);

    Literal lit3 = new Literal();
    lit3.Text = "<br/><br/>";
    t.Rows[0].Cells[0].Controls.Add(lit3);

    nameLabel = new Label();
    t.Rows[0].Cells[0].Controls.Add(nameLabel);

    this.Controls.Add(t);


    base.CreateChildControls();
    }

    So I thought the event hook is at the following lines:
    submitButton = new Button();
    submitButton.Text = "Submit";
    submitButton.Click += new
    EventHandler(this.submitButton_Click);
    t.Rows[0].Cells[0].Controls.Add(submitButton);

    The event handler looks like this:
    private void submitButton_Click(object source, EventArgs e)
    {
    this.NameLabelText = "submitted!";
    OnChange(EventArgs.Empty);
    }


    I also implemented the following just in case:
    public event EventHandler Change;


    protected void OnChange(EventArgs e)
    {
    if (Change != null)
    {
    Change(this, e);
    }
    }

    // Define the method of IPostBackEventHandler that raises
    change events.
    public void RaisePostBackEvent(string eventArgument)
    {

    OnChange(new EventArgs());
    }




    NameLabelText is a property as follows:
    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue("Enter name:"),
    Description("The text for the name label.")
    ]
    public string NameLabelText
    {
    get
    {
    EnsureChildControls();
    return nameLabel.Text;
    }
    set
    {
    EnsureChildControls();
    nameLabel.Text = value;
    }
    }


    When I click on the button, the nameLabel control doesn't change the
    text as I would expect.
    Anyone knows what I'm missing?


    Thanks
    J
    Jowita, Jun 16, 2006
    #1
    1. Advertising

  2. Jowita

    sam Guest

    You don't need to do all that implement interface stuff with the button
    you wrote. Just erase all of it.

    What you have should work unless I'm missing something. Assign IDs to
    all of the controls on the way to hooking up the button (you can skip
    this step if you want to for now). Put a breakpoint in OnInit (you
    *are* calling EnsureChildControls() here arent' you?). Look at
    Page.Request.Form["__EVENTTARGET"]. It should equal
    "this.submitButton.UniqueID". Tell me what you get.

    -Sam
    sam, Jun 17, 2006
    #2
    1. Advertising

  3. Jowita

    sam Guest

    You don't need to do all that implement interface stuff with the button
    you wrote. Just erase all of it.

    What you have should work unless I'm missing something. Assign IDs to
    all of the controls on the way to hooking up the button (you can skip
    this step if you want to for now). Put a breakpoint in OnInit (you
    *are* calling EnsureChildControls() here arent' you?). Look at
    Page.Request.Form["__EVENTTARGET"]. It should equal
    "this.submitButton.UniqueID". Tell me what you get.

    -Sam
    sam, Jun 17, 2006
    #3
  4. Jowita

    Jowita Guest

    Sam,

    Thanks for all the help. I will remove IPostBackEventHandler code,
    since I don't need to raise any events.
    I implemented the Oninit() override, but I couldn't get that id. It
    was returning null.

    It turns out that my problems were elsewhere, however. I had another
    control on the page that prevented the postback altogether. Once I
    removed it, the event worked like a charm :)

    Thanks,
    J


    sam wrote:
    > You don't need to do all that implement interface stuff with the button
    > you wrote. Just erase all of it.
    >
    > What you have should work unless I'm missing something. Assign IDs to
    > all of the controls on the way to hooking up the button (you can skip
    > this step if you want to for now). Put a breakpoint in OnInit (you
    > *are* calling EnsureChildControls() here arent' you?). Look at
    > Page.Request.Form["__EVENTTARGET"]. It should equal
    > "this.submitButton.UniqueID". Tell me what you get.
    >
    > -Sam
    Jowita, Jun 17, 2006
    #4
  5. Jowita

    Teemu Keiski Guest

    __EVENTTARGET is populated for controls not able to post without calling
    __doPostBack. E.g essentially Button and ImageButton won't populate it but
    LinkButton would as it uses __doPostBack.

    But yes, if handling an event defined by a subcontrol, you don't need to
    implement any interface in the composite control itself. Just having the
    event handler wired, is enough.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "sam" <> wrote in message
    news:...
    > You don't need to do all that implement interface stuff with the button
    > you wrote. Just erase all of it.
    >
    > What you have should work unless I'm missing something. Assign IDs to
    > all of the controls on the way to hooking up the button (you can skip
    > this step if you want to for now). Put a breakpoint in OnInit (you
    > *are* calling EnsureChildControls() here arent' you?). Look at
    > Page.Request.Form["__EVENTTARGET"]. It should equal
    > "this.submitButton.UniqueID". Tell me what you get.
    >
    > -Sam
    >
    Teemu Keiski, Jun 17, 2006
    #5
    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:
    828
    Purvi T
    Oct 19, 2004
  2. Jonah Olsson
    Replies:
    1
    Views:
    570
    Scott Allen
    Apr 5, 2005
  3. Chu
    Replies:
    2
    Views:
    902
    Winista
    Apr 10, 2006
  4. Max2006
    Replies:
    2
    Views:
    1,563
    Walter Wang [MSFT]
    Sep 19, 2007
  5. James Tsao
    Replies:
    0
    Views:
    261
    James Tsao
    Oct 25, 2004
Loading...

Share This Page