CSDN博客

img colayungnew

服务器控件的学习-生成用于回发的客户端脚本

发表于2004/10/22 15:10:00  845人阅读

只有两个 HTML 窗体元素(“按钮”(Button) 和“图像按钮”(ImageButton))引起窗体回发。如果自定义控件呈现不引起回发的 HTML 元素(如“文本框”(TextBox) 或“链接按钮”(LinkButton)),而您希望控件启动回发,则可以在 ASP.NET 中通过依靠客户端脚本(JScript、JavaScript)的事件结构进行编程来实现这一功能。

Protected Overrides Sub Render(output As HtmlTextWriter)
   output.Write("<a  id=""" & Me.UniqueID &   """ href=""javascript:" & Page.GetPostBackEventReference(Me) & """>")
   output.Write(" " & Me.UniqueID & "</a>")
End Sub

GetPostBackEventReference 方法发出启动回发的客户端脚本,并且还提供对启动回发事件的控件的引用。

在解释后的HTML源代码中是类似这样子滴
'****************************************************************************************************
<html>
<body>
<form name="ctrl2" method="POST" action="MyLinkButton.aspx" id="ctrl2">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="dDwtMjI1NTgwNDE2Ozs+eIZ+AfRvoCj1nWimbh+zPv/rKNg=" />                 
Here is the custom link button.<br>
<a  id ="Link" href="javascript:__doPostBack('Link','')"> Link</a>
<br><br>
<input name="TextBox" type="text" value="Click the link" id="TextBox" style="background-color:Cyan;width:200px;" />
<br>                                               
<script language="javascript">
<!--
      function __doPostBack(eventTarget, eventArgument) {
            var theform = document.ctrl2
            theform.__EVENTTARGET.value = eventTarget
            theform.__EVENTARGUMENT.value = eventArgument
            theform.submit()
      }
// -->
</script>
</form>                 
</body>                       
</html>

'*****************************************************************************************************

这里有一个很有意思的问题,那就是双引号的嵌套使用
刚开始看不懂”””和””的用法,经过资料查看,原来asp会将两个双引号转成一个双引号输出,就明白了。


在非窗体控件中保持客户端更改

客户端 ECMAScript(JScript、JavaScript)可用于跟踪不是窗体元素的控件中的客户端状态更改。此类控件不具有通过窗体提交将数据回发到服务器的方法。执行事件回发是可能的,但没有意义。通过将携带数据的隐藏输入域用于此类控件可以解决这一问题。控件必须在窗体提交之前的某一时刻发出隐藏输入域并发出将状态信息插入这些隐藏域的脚本。当加载控件时,控件可以检索并使用来自隐藏域的数据。为启用此机制,控件可以调用其 Page RegisterHiddenField 方法以发出隐藏域,并实现 IPostBackDataHandler 接口来恢复该隐藏域的值并更新其属性。

下面的示例说明此情形。示例 (DHtmlControl) 中的控件发出一个范围,当选择该范围时其颜色在客户端上发生更改。该控件发出一个隐藏变量,该变量的值由客户端脚本设置为一个布尔变量,以指示是否已选择该控件。此控件公开一个布尔属性 (Selected),该属性指示是否已在客户端上选择该控件。该控件实现 IPostBackDataHandler 接口,以便在将此页发送到服务器时,该控件可以读取隐藏域的值并更新其 Selected 属性。该控件还公开事件 (SelectedChanged),如果在客户端上选择该控件时将引发此事件。


Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Drawing
Imports System.Web
Imports System.Web.UI
Imports Microsoft.VisualBasic

Namespace CustomControls
   Public Class DHtmlControl
      Inherits Control
      Implements IPostBackDataHandler
      Public Event SelectedChanged As EventHandler
     
      Public Property Text() As String
         Get
            Dim obj As Object = ViewState("Text")
            If obj Is Nothing Then
               Return String.Empty
            Else
               Return CStr(obj)
            End If
         End Get
        
         Set
            ViewState("Text") = value
         End Set
      End Property
     
      Public Property Selected() As Boolean
         Get
            Dim obj As Object = ViewState("Selected")
            If obj Is Nothing Then
               Return False
            Else
               Return CBool(obj)
            End If
         End Get
        
         Set
            ViewState("Selected") = value
         End Set
      End Property
     
      Protected ReadOnly Property HelperID() As String
         Get
            Return "__" & ClientID & "_State"
         End Get
      End Property
     
      Protected Overrides Sub OnInit(e As EventArgs)
         MyBase.OnInit(e)
        
         If Not (Page Is Nothing) Then
            Page.RegisterRequiresPostBack(Me)
         End If
      End Sub
     
      Protected Overrides Sub OnPreRender(e As EventArgs)
         MyBase.OnPreRender(e)
        
         If Not (Page Is Nothing) Then
            Page.RegisterHiddenField(HelperID, Selected.ToString())
         End If
      End Sub
     
      Protected Overrides Sub Render(writer As HtmlTextWriter)
         Dim postback As String = ""
         If Not (Page Is Nothing) Then
            postback = Page.GetPostBackEventReference(Me) & ";"
         End If
        
         Dim click As String = "onclick=""var sel=getAttribute('selected'); sel = (sel.toLowerCase() == 'true'); sel=!sel; setAttribute('selected', sel.toString());this.style.backgroundColor=sel?'red':'white';" & HelperID & ".value=sel.toString();" & postback & """"
         Dim style As String = "style=""cursor:hand;background-color:" & IIf(Selected, "red", "white").ToString() & """"
         Dim selectedPiece As String = "selected=""" & Selected.ToString() & """"
         writer.Write(("<span " & style & " " & click & " " & selectedPiece & ">" & Text & "</span>"))
      End Sub
     
      Function LoadPostData(postDataKey As String, postCollection As NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
         Dim value As String = postCollection(HelperID)
        
         If Not (value Is Nothing) Then
            Dim newValue As Boolean = String.Compare(value, "true", True) = 0
            Dim oldValue As Boolean = Selected
           
            Selected = newValue
           
            ' If there is a change, raise a change event.
            Return newValue <> oldValue
         End If
        
         Return False
      End Function
     
      Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
         ' There was a change, so raise any events.
         RaiseEvent SelectedChanged(Me, EventArgs.Empty)
      End Sub
   End Class
End Namespace

 

 


 

阅读全文
0 0

相关文章推荐

img
取 消
img