RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 129, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


Customizing your Default Project Items in Visual Studio.NET

Did you know that you can edit the default items that are added to your solution?  Say you have something you do every single time you add a new web form and you just want it to add automatically.  This is actually a very simple thing to do.  To show you we are going to walk through some steps and actually do it.

 

  1. In a solution, add a web form.  Now go to the codebehind page.  This is where you can edit from what the default page looks like. 
  2. Find some working code that you would like to automatically add each time the page loads.  We are going to use my SetFocus script in this example.
  3. Now, go to this directory (this is for a default installation of VS.NET 2003, so make changes where needed): “C:\Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\DesignerTemplates\1033” and open the NewWebFormCode.vb.  If you have VS open, it should open in the same project. NOTE: You may want to back up the VB file before you open it.
    1. Directory
  4.  Now that you have the VB page open, you will notice that it is what you see when you add a new webform to your project.  Now you can start to make changes.  We will change them in the webform that is part of the project, and then copy them into this file
  5. We are going to change our Page Load Event to look like this:
      1. #Region "Page Load Events"

         

                Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

                'use care to modify this section

                '   To set the focus on a certain control

                '     Best used elsewhere, use with care.

                '     Remember to remove when you are not using, send exactly this: SetFocus()

                'SetFocus() 'Insert Control name here i.e. (txtName)

                'end section

         

                Try

                    

                   

         

                    If Not (Page.IsPostBack = True) Then

                        'carry out only when the page is loaded the first time

         

         

                    End If

                Catch ex As Exception

                    Throw ex

                End Try

            End Sub

         

        #End Region

  6. Now we also need to add a section above that will hold the script:

    1. #Region "Automation Scripts"

       

       

          ''' -----------------------------------------------------------------------------

          ''' <summary>

          '''     SetFocus(). Sets Focus on a control on the page.  Similar to something seen on a Windows Form.

          ''' </summary>

          ''' <param name="webControl">WebControl to focus on.</param>

          ''' <param name="htmlControl">HTML Control to focus on.</param>

          ''' <remarks>

          ''' </remarks>

          ''' <history>

          '''     [robz]      8/31/2004   Created

          ''' </history>

          ''' -----------------------------------------------------------------------------

          Private Sub SetFocus(Optional ByVal webControl As System.Web.UI.Control = Nothing, Optional ByVal htmlControl As System.Web.UI.HtmlControls.HtmlControl = Nothing)

              Dim setCursor As New System.Text.StringBuilder

              Dim removeScript As Boolean = False

              Dim controlname As String

              'check to see if both are sent

              If Not (webControl Is Nothing) And Not (htmlControl Is Nothing) Then

                  Dim ex As New Exception("You may only send one control, web or html. Not both.")

                  Throw ex

              ElseIf Not (webControl Is Nothing) Then 'if it is a webcontrol

                  controlname = webControl.ClientID.ToString

              ElseIf Not (htmlControl Is Nothing) Then 'if it is a htmlcontrol

                  controlname = htmlControl.ClientID.ToString

              Else 'nothing has been passed and the script should be removed.

                  removeScript = True

              End If

              'check to see if the script should be removed.

              If Not (removeScript = True) Then

                  'create the javascript

                  setCursor.Append("<script language='javascript'>")

                  setCursor.Append(vbCrLf & "function setCursor() {")

                  setCursor.Append(vbCrLf & "    if (document.getElementById('" & controlname & "') != null) {")

                  setCursor.Append(vbCrLf & "          document.getElementById('" & controlname & "').focus();")

                  setCursor.Append(vbCrLf & "    }")

                  setCursor.Append(vbCrLf & "}")

                  setCursor.Append(vbCrLf & " setCursor();")

                  setCursor.Append(vbCrLf & "</script>")

                  'register the script

                  RegisterStartupScript("setCursor", setCursor.ToString())

              Else

                  'register over the top of the script with nothing

                  RegisterStartupScript("setCursor", "")

              End If

          End Sub

       

       

      #End Region

  7. Now we can save the form.  Every time we add a new form from now on it will look something like this:
    1. Picture of New Web Form /></P></SPAN></LI></OL></o:p></OL>
<P class=MsoNormal style=
  8. And that should do it.  As you find other items you want to add to webforms by enhance your productivity and the form’s ability, just follow the same steps to go in and edit.  Keep in mind that some items may be contained in multiple locations, like the webform.aspx (which is in two locations).

 

 

posted on Wednesday, October 13, 2004 9:41 AM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: