Visual Studio 2005 Adding Handlers

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

Hi,

Im fairly new to 2005 and I was wondering.

In 2003, when you add a button, it also adds and AddHandler statement within
the initialisation routine to wire up the handler to the event.

In VS 2005, I cant find this construct anywhere, not even in the designer.cs
in my WebProject application.

Any ideas how / where this is declared ?

Cheers
 
Note also the OnClick property/attribute in the markup - i.e.

<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />

Marc
 
Thanks, I did know about the AutoWireup. Bu even if I specify False in the
page and or in the web.config, it still generates the wireup when compiled,
it seems that you cannot stop the autowireup in 2005 ( Although Im sure you
can ) I cant seem to, I want to be able to add the wireup code manually.

Any ideas ?
 
Ahhh. OK Gotcha, thats the new bit

If I wanted to add "Programatically" more events to the handler for a
button, for example in a calculator program, where would I do this as I dont
see the initialise component sub anymore. ?

Cheers
 
Has the markup got an OnClick="Something" entry? Try removing this;
then add your handlers through code and all should be well.

Marc
 
I did this, but the event does not seem to fire or at least the
Button1_Click is not being called.



public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

this.Button1.Click += new System.EventHandler(Button1_Click);

this.Button2.Click += new System.EventHandler(Button1_Click);

}

protected void Button1_Click(object sender, EventArgs e)

{

System.Web.UI.WebControls.Button senderButton;

senderButton = (System.Web.UI.WebControls.Button) sender;

this.TextBox1.Text = senderButton.ID;

}



}
 
Well, if using auto-wireup, just declare a suitable Page_{YourEvent}
method - but IMO an easier option is to override (as below) the base
Page methods; the IDE helps you out as soon as you type "override",
plus it doesn't incur the (very small) overhead of event subscription
and delegate invokation. But my main point here is that it is easy to
get right as the IDE stubs everything automatically:

protected override void OnInit(EventArgs e){
base.OnInit(e);
// your code
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
// etc
}

Marc
 
Excellent !

Thanks



Just Me said:
I did this, but the event does not seem to fire or at least the
Button1_Click is not being called.



public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

this.Button1.Click += new System.EventHandler(Button1_Click);

this.Button2.Click += new System.EventHandler(Button1_Click);

}

protected void Button1_Click(object sender, EventArgs e)

{

System.Web.UI.WebControls.Button senderButton;

senderButton = (System.Web.UI.WebControls.Button) sender;

this.TextBox1.Text = senderButton.ID;

}



}
 
The following works, but I can't recall if this is the best place to
subscribe... I haven't been doing much ASP.NET lately, so can't
remember 100%

protected override void CreateChildControls()
{
base.CreateChildControls();
Button1.Click += Button1_Click;
}
 

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

Back
Top