runat=server
2024-07-21 02:16:54
供稿:網(wǎng)友
 
背景:
當(dāng)我們在窗體上添加web control例如label時,vs.net會自動添加runat=server把它當(dāng)成服務(wù)器控件,但是當(dāng)我們添加自定義的控件時,我們就無法自動得到runat=server我們必須每個空間都重復(fù)添加runat=server。
我們現(xiàn)在要做的就是做一個宏在vs.net中,它可以自動添加runat=server在我們指定的控件上,現(xiàn)在已經(jīng)存在的runat=server他會忽略不計,不會重復(fù)添加。
'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