7

What is the easiest way to create code-behind (webforms) event handlers for say a button from HTML source view?

In VB.NET it is quite easy to switch to code behind page and use the object and events combo boxes along the top to select and create.

In c# those are missing (and I really don't like design view).

2 Answers 2

6
  • Make sure the Properties window is open.
  • Click anywhere in the element in source view.
  • Click the lightning symbol (events) in the Properties window.
  • Find the event you want to create a handler for.
  • Double click it.
Sign up to request clarification or add additional context in comments.

3 Comments

In c# while in html source view the lightening bolt is not visible. So when looking at the aspx page in html view(source), then looking at properties window the bolt is not there.
I just checked - you're right that it's not there at first, but all you have to do is quickly switch to design view and back, and it appears for all controls. I guess since you've already accepted an answer that you're no longer interested in solving this though?
Too bad the switch to Design View is needed.
2

I agree that this is less trivial with C# then with VB. My personal preference is to simply add a function with the following signature (always works):

protected void MyButtonName_Clicked(object sender, EventArgs e)
{
    Button btn = (Button) sender;  // remember, never null, and cast always works
    ... etc
}

Then, inside the code view of the HTML/ASP.NET part (aka the declarative code) you simply add:

<asp:Button runat="server" OnClick="MyButtonName_Clicked" />

I find this faster in practice then going through the several properties menus, which don't always work depending on focus and successful compile etc. You can adjust the EventArgs to whatever it is for that event, but all events work with the basic signature above. If you don't know the type, just place breakpoint on that line and hover over the e object when it breaks to find out the actual type (but most of the time you'll know it beforehand).

After a few times doing this, it becomes a second nature. If you don't like it, wait a moment for VS2010, it's been made much easier.

Note: both VB and C# never show the objects or events of elements that are placed inside naming containers (i.e., GridView, ListView). In those cases, you have to do it this way.

4 Comments

Essentially, you are saying you have to hand code it. I was trying to avoid this. A little surprised they didn't make it as easy as it is with vb.net. I figured there was a better way and that I just didn't know it. I really dont like design view (I guess that is old school). I really hope it is better in vs.net 2010.
I was as surprised as you are when I first found out and with each new version I hope they add it. There are many reasons why it isn't as "easy" as with VB.NET. One reason is possibly that C# coders tend to be more precise. Several objects can use the same handler. The handler can be in a different class, they can be inherited etc. etc. Dropdowns as with VB don't aid, but are in the way then (but adding them wouldn't hurt either, would it?). C# coders don't like to be befuddled, I guess. But VS2010 gets it (a lot?) better.
PS: there's another way, that's half-way automatic: in the ctor, add a MyButtonName.Click += YourHandlerName (when the handler doesn't exist yet), click the helper icon: it will insert the stub-handler automatically in the code for you.
No, but I got it from some discussions online. You can try it yourself, the beta download is free and can co-exist with your current VS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.