背景:
当我们在窗体上添加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("TextD