Groups | Blog | Home
all groups > asp.net building controls > june 2006 >

asp.net building controls : Handling a click event in custom server control


Jowita
6/16/2006 3:47:06 PM

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
sam
6/16/2006 6:19:26 PM
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
6/16/2006 6:21:51 PM
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
6/16/2006 8:16:40 PM

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


[quoted text, click to view]
Teemu Keiski
6/17/2006 6:55:23 PM
__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

[quoted text, click to view]

AddThis Social Bookmark Button