Visual Studio keyboard shortcuts for creating Event handler stubs - asp.net

When you edit a simple page in the design view, you can add an event on most components by simply double-clicking the relevant event. This does the binding and generates the function declaration in the codebehind for you.
In larger projects where the pages are complex, the design view can be extremely slow. Is there a way to quickly generate the codebehind stubs directly from the .aspx source?

Just begin typing the code to create your component.
For example, this is the code to add a button to your .aspx file:
<asp:Button ID="ButtonAction" class="btn panel-black" runat="server" Text="" OnClick="Function_Name_Here" />
When you get to typing the function name for the OnClick method...
for example, this part of the code:
OnClick="Function_Name_Here"
... Visual Studio will display a little pop-up window with the option to "<create new event>".
Select this option and the codebehind event handler stub will be generated for you.

In the markup view the Properties window is still available: you may have it hidden.
When it's shown and the cursor is in the markup for a particular control, you get that control's properties and events like you would in Design view. Similarly, in the event tab you can double-click the event (i.e Click) to automatically generate an empty declaration in code and hook up the event without ever having to go into the Design view.

I've been able to do this with ReSharper, but I've been using it for so long that I don't recall whether Visual Studio has this capability natively.

Related

Can I have a modal dialog from asp.net gridview edit click?

I am wondering if it is possible to have a modal dialog (like JQuery) by clicking Edit button on a asp.net built in gridview control. If yes, can anymore point me out the brief process of how it could be done. Please see the picture below for clarification.
Thanks.
I see few ways to do that:
Using OnEditCommand property (assuming you are using <asp:EditCommandColumn to draw that edit link):
You can show popup using serverside handler (for instance, popup included into ajaxcontroltoolkit.dll which allows to show popup from serverside easily on page reload)
Another option:
Make your own column with edit link for each item. It can have OnClientClick handler which will open jQuery popup directly on client (but you will need to get row info for current line from server somehow: with your own ajax call or, suppose it will be better, using webservice with webmethod)
Second option could be modified: instead of creating own column, you may add click even handler with that same jquery on default edit link with return false, so it will prevent form submition.
I never did something like this personally and even newer saw implementations of such thing, but I would select some option from those listed above. I do not think that there is some really simply, built in way of doing that.
UPD:
Here is an example of opening popup with own edit button and modalpopupextender from ajax control toolkit (similar like in my first option except that they are using own edit button, which I think could be easily replaced by default one and OnEditCommand even handler) :
http://www.c-sharpcorner.com/UploadFile/krishnasarala/edit-gridview-row-with-model-popup-extender-in-Asp-Net-ajax/

How to add Page Event in VS 2010 Web Developer Express

Such a simple question but if I want to add run some code in the OnPreRenderComplete Event for a Asp.Net page that inherits from a Master Page using Visual Studio Web Developer Express 2010 how can I do it?
Right clicking on the aspx page to get to properties allows me to select all the web controls in the drop down. Once I select one I can just click on the events tab and click in the event I want to have the empty method added to the .cs and register it for the event. But I don't see an option for Page.
What am I missing?
Not sure whether this feature is available in Web Developer Express but in Visual Studio you can add/select page event handler via:
Right mouse click on .aspx in solution explorer
Open "View Component Designer"
From properties windows - select "event" icon (yellow colored icon)
Double click on event name or type handler name by hand.
If AutoEventWireup page is set to "true" then the aspx page also automatically runs methods (special naming convention of page events) when certain events are raised. These names are Page_Init, Page_Load etc.
MSDN LINK : How to: Create Event Handlers in ASP.NET Web Pages
OnPreRenderComplete is a virtual method in the Page class that can be overridden with the override keyword.
protected override void OnPreRenderComplete(EventArgs e) {
// your code...
base.OnPreRenderComplete(e);
};
Don't forget, like I did, to call the base classes implementation...
actually I just remembered that I asked this very same question a while ago
and someone showed me this rather obscure way of doing it:
open the page in design view
right click on the background of the page
select view component designer
on the properties page you can now select the lightning bolt
double click an event name to generate the code

dropdownlist events does not show in properties window asp.net Visual Studio 2010

I recently found that dropdownlist events are not shown in properties window.
I was disappointed.
I already hate asp.net designer in visual studio. its slow and full with ugly things like this.
please help me. I need to generate events automatically not to write it by hand.
It seems that the Properties Dropdown changes after viewing 'Split' or 'Design' view of the .aspx file at least once. In my case, I always left it in 'Source' mode and never saw the asp.net properties (and events) in Properties Dropdown.
The Design/Split/Soruce buttons are in the lower left of the text editor window pane.
I haven't found a good answer for this anywhere on the web so here it is:
After playing around I noticed that the Events options in Properties disappears after you have started editing the file. If you save or Ctrl+s then Visual Studio will reevaluate the page and realize its an actual ASP tag and give you the full properties with the events choices again.
You do not have to use Designer.
The solution is very easy.
It is just simple,it just that you can not see the events in the source view, you have to just switch to the design view to see the events and double click them to generate events.
and again come to design view
If you want to create OnSelectedIndexChanged Event you can double click your DropDownList in design view.
if not try writing this :
myDropDownList.SelectedIndexChanged +=
and then press tab 2 times. so it will create event handler for you. then you need to copy your handler name and paste it in your control markup.
I have found this too, only it happens on every control.
I found that if you are using "Source View", the events button is not shown in the properties window, but if you use design view and single click the control in design view (or "Both" view) then the events button is then shown in the properties window.
I suspect this is a bug in VS10? It's quite annoying as I don't like the Design view in VS, I always prefer Source view.
Can't speak for earlier versions of VS, but for 2013, after adding the element, Build the project. Sometimes I have to switch to Design view and back to Source view once on the page, but then the events appear:
Click the lightning bolt icon for the events.
Double click SelectedIndexChanged and it creates this in the codebehind:
protected void ddlChoice_SelectedIndexChanged(object sender, EventArgs e) {
}
And adds the event handler to the markup:
<asp:DropDownList runat="server" ID="ddlChoice" OnSelectedIndexChanged="ddlChoice_SelectedIndexChanged" />
When using VS2012 this can be done without actually going into Design view. I know the question is specifically for VS2010, but I can't test this answer myself.
In source view simply start adding the appropriate attribute, for example OnServerValidate on a CustomValidator. As soon as you type the = sign the very first item in the list is <Create new event>, which has the same function as double clicking the control.
At the top of your code page there are two comboboxes. Choose your dropdownlist from the one on the left (It says "(General)" by default) and then choose your event from the one on the right one.
It will create the event for you and so you can just add your code.
Click on the 'Split' view in MS Visual Studio - select a button in the 'Design' view and then look into property window now. You should be able to see Events now. Good luck.

Why does Visual Studio sometimes add a number when creating page events in codebehinds?

When writing up a codebehind in Visual Studio for ASP.NET web forms applications, I often use the dropdowns at the top of the window to autogenerate page event handlers (e.g. Page_Load, Page_PreRender). I've noticed that sometimes Visual Studio likes to add numbers to these function names like "Page_Load1" or "Page_PreRender2".
Programatically speaking, this has no effect on the code. But stylistically, I find it a bit ugly. Is there any way to get rid of this behavior?
VS will do this if the event handler already exists, but has been disconnected from the event.
You can reproduce this behavior as follows.
Create a form with only a button (leave the default name of Button1
Double-click it to create the event handler Button1_Click
What you may not realize is that in addition to the code-behind, there is also a file for the designer and the .resx. I'm not sure which one has what, and I am not at a PC with Visual Studio, but in one of those files, there is a bunch of code defining the button, including a line that starts with
Button1.Click += new EventHandler
or something along those lines.
At this point all is good.. NOW to disconnect the button from the event...
In the form designer, delete the button
At this point, the Button1_click event handler still exists in code behind but is not tied to an event (because the button whose event it was tied to has been deleted.)
Drag a new button onto the form (still named Button_1)
At this point, there is a new Button_1, but the event handler is still not tied to your original Button1_Click event. So if you double-click on the button, a new eventhandler will be created (Button1_Click1())
I assume it would be happening because you've already got a method named Page_Load in the current scope: Visual Studio has to differentiate between your existing method and the new one somehow.
Note that if you remove an event handler, Visual Studio only deletes the method in the code-behind file if it's empty: if you've modified the code, then it won't delete it from the code-behind file.

asp.net custom web user control -- button

Is it possible to create a custom button -- web user control? I want certain java scripts to trigger when buttons are clicked. If so, are there any articles out there that explain the basics?
I completely understand that I can load javascript via .js link or dynamically at page load, but I would like to just drop a control on the page without manually adding code to every page on every one of my projects.
There is the IScriptControl interface you can implement. In MSDN are samples for that.
It will take care of aumatic script including from a resource whenever it is shown on a page.
Add a new Web User Control to your project by going to the menu item Project | Add New, then selecting Web User Control. Then just drop a button on it like this:
<asp:Button ID="Button1" runat="server"
onclientclick="alert("Javascript Here.")" Text="Button" />
You can then use that in your project. Replace the onclientclick with the javascript you would like to run.

Resources