Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Handling a click event in custom server control

Reply
Thread Tools

Handling a click event in custom server control

 
 
Jowita
Guest
Posts: n/a
 
      06-16-2006

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

 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      06-17-2006
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

 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      06-17-2006
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

 
Reply With Quote
 
Jowita
Guest
Posts: n/a
 
      06-17-2006

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


 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      06-17-2006
__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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
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
When user press F5 after a button click event, the click event fires again! Max2006 ASP .Net 2 09-19-2007 05:37 AM
Wire up a click event within another click event doesn't work... Chu ASP .Net 2 04-10-2006 10:43 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



Advertisments