Need to tell that routine handles Load event?

A

AAaron123

I wrote mostly Vb.Net code.

Trying C# now.

I have:

AutoEventWireup="false"

and:

OnSelectedIndexChanged="lstFiles_SelectedIndexChanged"

and:

public void stifles_SelectedIndexChanged(Object sender, EventArgs e)

I think the above is ok without what in Vb is a Handles statement.

True?



But what about the following?

Seems I need something to say what it handles.

Don't I?

protected void Page_Load(object sender, EventArgs e)





Thanks
 
A

AAaron123

I'm going to try to do it better.
Still it is difficult to be complete and concise, but here goes.


In a .aspx file I have:
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master"
AutoEventWireup="false"

CodeFile="File Download.aspx.cs" Inherits="Admin_FileDownload" %>

....and...

<asp:ListBox ID="lstFiles" AutoPostBack="true"
OnSelectedIndexChanged="lstFiles_SelectedIndexChanged"

runat="server" Rows="15" />



in the code behind file I have:

public void lstFiles_SelectedIndexChanged(Object sender, EventArgs e)



I'm wondering if I need something like:
SelectedIndexChanged += lstFiles_SelectedIndexChanged;



or does the attribute actually wire the event. If it does not, what does it
accomplish?

And then there is the question about the following in the code behind.:
protected void Page_Load(object sender, EventArgs e)

It has no attributes in the .aspx file relating to it. Do I need to
subscribe to the event explicity?

Is it:

Page.Load+=Page_Load;

Placed where?



I have a C# book, maybe it too big, but I can't find the answers. I know he
must cover it but I might have to read all 1300 pages to find it.



I hope I did better



Thanks
 
A

AAaron123

I wired the Load event in a default constructor and now everything works as
I expect it should.

I'm glad you mentioned AutoEventWireup attribute applies only to the Page
and not the controls. I re-read the doc on @Control and it makes sense but I
know I missed that implication the first time. In fact, I read the first
site you gave below and, without your mention, I don't think I would have
come away with the knowledge that it was the page only that got affected.

Thanks

Peter Duniho said:
I'm going to try to do it better.

Much better. Thank you. You are right that you still didn't post a
complete example, but at least now you've made clear that you're writing
ASP.NET code, and talking about .aspx code as well as .cs code. As I
said, context is everything.

As for the questions...
[...]
I'm wondering if I need something like:
SelectedIndexChanged += lstFiles_SelectedIndexChanged;

or does the attribute actually wire the event. If it does not, what does
it
accomplish?

The AutoEventWireup attribute causes the framework to hook up events
automatically _if it's set to "true"_. When it's "false", you have to do
the work yourself. But (important): the AutoEventWireup attribute applies
to the page, not necessarily controls contained within.

If you have an event handler attribute (i.e. "OnSelectedIndexChanged=...")
declared in your ASP.NET control element, then AFAIK it _should_ in fact
get hooked up automatically, even if the containing page's AutoEventWireup
attribute is "false".
And then there is the question about the following in the code behind.:
protected void Page_Load(object sender, EventArgs e)

It has no attributes in the .aspx file relating to it. Do I need to
subscribe to the event explicity?

See above. If you've set AutoEventWireup to "false", then yes...you need
to explicitly subscribe events for the page.
Is it:

Page.Load+=Page_Load;

Placed where?

You can subscribe to the event in any code-behind code associated with the
page instance. In that case, the code would reside in the class for the
page itself, so it would be "this.Load += Page_Load", or even just "Load
+= Page_Load". You can put that in the constructor, in an override of the
OnInit() method, or any other suitable place where you expect code to be
executed on initialization (or later, if you don't need the event hooked
up immediately, but instead in response to something else that happens).
I have a C# book, maybe it too big, but I can't find the answers. I know
he
must cover it but I might have to read all 1300 pages to find it.

I recommend the MSDN web site. It's not always the most clear, and in
rare cases is even inaccurate, but it is _the_ definitive source of
information. It took me almost no time at all to find these links:
http://support.microsoft.com/kb/324151
http://support.microsoft.com/kb/814745
http://msdn.microsoft.com/en-us/library/ms178492(VS.85).aspx
http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx

Spend some time learning how to navigate around the MSDN web site, and I
think you'll find it's much easier to find information than trying to
browse through a book (especially one with a poor index, or incomplete
content).

Pete
 

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

Top