首页 > 开发 > 综合 > 正文

runat=server

2024-07-21 02:16:54
字体:
来源:转载
供稿:网友
背景:
当我们在窗体上添加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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表