Handling a click event in custom server control

J

Jowita

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
 
S

sam

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
 
S

sam

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
 
J

Jowita

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
 
T

Teemu Keiski

__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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,529
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top