runat=server

80酷酷网    80kuku.com

  server背景:
当我们在窗体上添加web control例如label时,vs.net会自动添加runat=server把它当成服务器控件,但是当我们添加自定义的控件时,我们就无法自动得到runat=server我们必须每个空间都重复添加runat=server。
我们现在要做的就是做一个宏在vs.net中,它可以自动添加runat=server在我们指定的控件上,现在已经存在的runat=server他会忽略不计,不会重复添加。
'This macro checks the specified elements if they have runat=server in
'them and if not then automatically adds runat=server in them
Sub AddRunAtServer()
      'Create an Undo context object so all the changes can be
      'undone by CTRL+Z
      Dim oUnDo As UndoContext = DTE.UndoContext
      oUnDo.Open("Comment Line")         
       
      'Supress the User Interface. This will make it run faster
      'and make all the changes appear once
      DTE.SuppressUI = True
       
      Try
      'Make a call to UpdateDocument()
            UpdateDocument("<asp:")
            UpdateDocument("<form")
            UpdateDocument("<script")
            UpdateDocument("<body")
           
            'Finally Close the undo
            oUnDo.Close()
      Catch oException As system.Exception
            Dim lcErrMsg As String
            lcErrMsg = "An error occured while running this program." _
                              & " Please make sure that you are specifying the" _
                              & " correct parameters."
            MsgBox(lcErrMsg)
           
             'Undo the changes made to the document
            oUnDo.SetAborted()
            DTE.SuppressUI = False
      Finally
            'Rest the Supress UI
            DTE.SuppressUI = False
      End Try
End Sub
   
'This method is used internally to do the actual work for adding
'runat=server for a specified element type
Private Sub UpdateDocument(ByVal tcStringToSearch As String)
       
      'Get a reference to the currently open document
      Dim oDoc As TextDocument
      oDoc = DTE.ActiveDocument.Object("TextDocument")
       
      'Create editpoints for starting and ending positions of the doc
      Dim lnStartPos As EditPoint = oDoc.StartPoint.CreateEditPoint
      Dim lnEndPos As EditPoint = oDoc.EndPoint.CreateEditPoint
       
      'This is the string that we will search and a placeholder string
      Dim lcSearchStr As String = tcStringToSearch
      Dim lcString As String
       
      'Define the private variables used in this process
      Dim lnStrPos As Integer = 0
      Dim lnRunAtPos As Integer = 0
      Dim lnClosingPos As Integer = 0
      Dim lnEmptySpot As Integer = 0
       
      Do While True
      'Get the string and remove all the carriage returns as they
      'are ignored by the EditPoint object
            lcString = LCase(lnStartPos.GetText(lnEndPos))
            lcString = Replace(lcString, Chr(13), "")
           
            'Get the first position of item we are looking for
            lnStrPos = InStr(lcString, lcSearchStr)
           
            If lnStrPos = 0 Then
                'If we do not find the item, exit
                 Exit Do
            Else
                'We found the item that we were looking for
               
                'Shorten the string starting from the new position
                lcString = lcString.Remove(0, lnStrPos _
                                                                              + Len(lcSearchStr))
               
                'Now move the EditPoint to that position as well
                lnStartPos.CharRight(lnStrPos + Len(lcSearchStr))
               
                'Now we have the subsized string, let us check for the
                'first occurance of > is more than the runat
                lnClosingPos = InStr(lcString, ">")
                lnRunAtPos = InStr(lcString, "runat")
               
                'The closing tag's position always HAS to be more
                ' than the runat's position
                If lnRunAtPos = 0 Or lnRunAtPos > lnClosingPos Then
                    'At this point we found that Runat=server is
                    ' missing in this element/object
                   
                    'Locate the first blank spot to make the insertion.
                    lnEmptySpot = InStr(lcString, " ")
                   
                    'Make sure that the blank spot is within the
                      'boundries
                    If lnEmptySpot > lnClosingPos Then
                        'Special handling required
                        'In this case we want to place just before
                        ' the closing position i.e. ">"
                        'However, it is possible that the closing is
                        ' done using />
                        If lcString.Substring(lnClosingPos - 2, 1) = _
                                                      "/" Then
                            lnStartPos.CharRight(lnClosingPos - 2)
                            lnStartPos.Insert(" ")
                        Else
                            lnStartPos.CharRight(lnClosingPos - 1)
                            lnStartPos.Insert(" ")
                        End If
                    Else
                        lnStartPos.CharRight(lnEmptySpot)
                    End If
                   
                    'Once the blank spot is determined and the
                    ' EditPoint is positioned, Make the insertion
                    lnStartPos.Insert("runat=server ")
                End If
            End If
        Loop
    End Sub

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: