runat=server

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

时间: 2024-10-01 08:44:45

runat=server的相关文章

ASP.NET中使用多个runat=server form

ASP.NET 在同一个页面不支持多个 runat=server forms,要解决这个问题,可以把每个 form 放在一个单独的 panel 控件中,这样用户就可以简单地通过单选按钮在不同 panel 间切换.代码如下:2FormExample.aspx <%@ Page language="c#" Codebehind="2FormExample.cs" AutoEventWireup="false" Inherits="_3

在 ASP.NET 中使用多个 runat=server form

asp.net|server ASP.NET 在同一个页面不支持多个 runat=server forms,要解决这个问题,可以把每个 form 放在一个单独的 panel 控件中,这样用户就可以简单地通过单选按钮在不同 panel 间切换.代码如下:2FormExample.aspx <%@ Page language="c#" Codebehind="2FormExample.cs" AutoEventWireup="false" In

服务端控件绑定后台变量值无效runat=&quot;Server&quot; &lt;%= **** %&gt;

一个诡异的问题.服务端控件绑定后台变量值无效. 这是一段Asp.Net代码 <div> <input type="text" name="name" runat="Server" value="<%=IMaxPage %>" /> </div> 后台代码 public partial class _Default : System.Web.UI.Page { protected

类型“ScriptManager”的控件“ScriptManager1”必须放在具有 runat=server 的窗体标记内

问题描述 各位大侠,当我把一段Script脚本放到Ajax的UpdatePanel里边的时候,出现了这个错误:类型"ScriptManager"的控件"ScriptManager1"必须放在具有runat=server的窗体标记内这个如何解决?请诸位提点方法.谢谢! 解决方案 解决方案二:up解决方案三:在放updatepanel之前一定要保证有scriptmanager控件,你要把这个控件加一下解决方案四:scriptmanager一般要紧接着<form&g

.net-类型“TextBox”的控件必须放在具有 runat=server 的窗体标记内。

问题描述 类型"TextBox"的控件必须放在具有 runat=server 的窗体标记内. 类型"TextBox"的控件"txt_UserName"必须放在具有 runat=server 的窗体标记内. 这个错误如何解决? 解决方案 你看下你的TextBox控件是否跟这个一致. 解决方案二: id 为txt_UserName 控件标记 必须加上 runat='"server " 解决方案三: 类型"TextBox&

jscript-VB6.0封ASP(不是ASPX)问题在类文件cls如何写runat=server代码

问题描述 VB6.0封ASP(不是ASPX)问题在类文件cls如何写runat=server代码 我用VB6.0封装ASP代码(不是ASPX),其中有一个代码需要为JSCRIPT,也就是解析JSON的代码 <script language="jscript" runat="server"> Array.prototype.get = function(x) { return this[x]; }; function parseJSON(strJSON)

JS动态创建&amp;amp;lt;select&amp;amp;gt;(无runat=“server”),后台搜索数据库绑定select。代码应该怎么写,新手,不会AJAX,AJSON

问题描述 实现JS创建一个<select>,没有runat="server"属性,然后option是根据后台搜索数据库出来的数据绑定生成,请问大神们怎么实现,新手不会AJAX,AJSON.求代码啊. 解决方案 解决方案二:既然你提到了Ajax,那么我就认为你是要动态的来加载下拉框的选项,这样的话,后台取数据,将数据返回前台,前台进行[绑定]解决方案三:新手就需要买书然后认真学习.等你能设计程序了,再问.解决方案四:新手就需要买书然后认真学习.等你能设计程序了,基本够一个程序

ASP.NET中 script runat server 的用法_实用技巧

本文实例讲述了ASP.NET中<script runat="server">的用法,分享给大家供大家参考.具体如下: 在ASP.NET的.aspx页面中,可以在<head>代码</head>标签中通过<script runat="server">添加代码.其实,这里的代码和.cs文件中的代码地位是相同的,都是在服务器端执行的. 例: 复制代码 代码如下: <script type="text/javas

asp.net 保存、修改没有 runat=server控件的控件值的一个解决方案_实用技巧

js: 复制代码 代码如下: function Save()//保存不是服务端控件的值 { var 1= document.getElementById('1Box').value; var 2 = document.getElementById('2Box').value; var TxtValue = 1 + "■" + 2; document.getElementById('3).value = TxtValue; return true; } window.onload=fun