演示改变客户端控件事件响应脚本及无刷新调用服务端(ASP.net2.0)

页面




ChangeAtt.aspx
<% @ Page Language="C#" CompileWith="ChangeAtt.aspx.cs" ClassName="ChangeAtt_aspx"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  >
< title > 客户端脚本 </ title >
< script  language  =javascript>

        
        
function f1()
        {
            alert("函数[1]");
        }
        
        function f2()
        {
            alert("函数[2]");
        }

        function RefSwitch(message, context)
        {
            switch (context)
            {
                case "FirstCallServer":
                        DoServerCallBack(message, context);
                break;
                
                case "GetServerTime":
                     ShowServerTime(message, context);
                break;
               
            }
        }
        
        function FirstCallServer()//第一次调用服务器
        {
        var message 
= 'FirstCallServer';
        
var context  = 'FirstCallServer';
        
<% =sCallBackFunctionInvocation%>
        
}
        
        function DoServerCallBack(Message, context) //服务器提示是否交互确认
        {
        if (Message 
==  "GetTime?")
        {
            var b 
= confirm("是否返回服务器时间?");//根据用户确认调用服务器操作
            
if (b)
            {
                var message 
= 'GetServerTime';
                
var context  = 'GetServerTime';
                
                
<% =sCallBackFunctionInvocation%>
            
}
        }
        }
        
        function ShowServerTime(timeMessage, context) 
        {
        alert('服务器当前时间为:\n' + timeMessage);
        }

        function OnError(message, context) 
        {
        alert('An unhandled exception has occurred:\n' + message);
        } 
        
    </script
>

<link href="Common/Css/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form1" runat="server">
  
<div>
      
<span style="font-family: 宋体"><span style="font-family: 宋体"></span>
      
<table width="529" style="width: 224px; height: 71px">
          
<tr>
              
<td style="width: 766px; height: 16px">
                  
<p>
                      
<span style="color: #ff0000; font-family: 宋体"><b>演示内容:</b></span></p>
                  
<ul>
                      
<li><span style="font-family: 宋体">改变客户端控件事件响应函数</span></li><li>改变客户端控件响应函数脚本</li>
                      
<li>无刷新调用服务端函数,并使用客户端对话框交互,将结果返回服务端</li>
                  
</ul>
              
</td>
          
</tr>
          
<tr>
              
<td style="width: 766px; height: 51px;">
                  演示改变客户端控件的响应函数
<input type="button" class="Button" id="Button1" style="width: 111px; " onclick = "f1();" value="客户端" runat="server" />        
                
&nbsp;<asp:Button ID="Button2" Runat="server" Text="服务端" Width="114px" Height="20px"
            OnClick
="Button2_Click" CssClass="Button" /></td>
          
</tr>
          
<tr>
            
<td style="width: 766px; height: 150px">
                  
<p>
                      
&nbsp;</p>
                
<p>
                    演示改变客户端控件的响应脚本
</p>
                  
<asp:TextBox ID="TextBox1" Runat="server" Height="77px" Width="231px"
                        TextMode
="MultiLine">alert(&quot;服务端指定函数1&quot;);</asp:TextBox>
                    
<input type="button" class="Button" id="Button3" style="width: 231px; " value="客户端激发函数" runat="server" />
              
<asp:Button ID="Button4" Runat="server" Width="111px" Text="服务端指定函数" OnClick="Button4_Click" CssClass="Button" />&nbsp;
            
<asp:Button ID="Button5" runat="server" Height="20px" Width="114px" Text="服务端指定并执行" OnClick="Button5_Click" CssClass="Button" /></td>
          
</tr>
          
<tr>
              
<td style="width: 766px; height: 57px; vertical-align: top;">
                  
&nbsp;<br />
                  演示无刷新B
/S联动<input type="button" class="Button" id="Button6" style="width: 231px; " onclick="FirstCallServer();" value="服务端无刷新调用,并用对话框交互" />
              
</td>
          
</tr>
      
</table>
  
</span></div>
    
</form>
</body>
</html>


ChangeAtt.aspx.cs
using  System;
using  System.Web.UI;

public  partial  class  ChangeAtt_aspx : ICallbackEventHandler
{
    
public string sCallBackFunctionInvocation;

    
void Page_Load(object sender, System.EventArgs e)
    
{
        sCallBackFunctionInvocation 
= this.GetCallbackEventReference(this"message""RefSwitch""context""OnError");

    }

     
public string RaiseCallbackEvent(string eventArgument)
    
{
        
string[] aStr = eventArgument.Split(("\n").ToCharArray());
        
string commstr, param1, param2, param3;
        commstr 
= "";
        param1 
= "";
        param2 
= "";
        param3 
= "";
        
switch (aStr.Length)
        
{
            
case 0:
                commstr 
= "";
                
break;
            
case 1:
                commstr 
= aStr[0];
                
break;
            
case 2:
                commstr 
= aStr[0];
                param1 
= aStr[1];
                
break;
            
case 3:
                commstr 
= aStr[0];
                param1 
= aStr[1];
                param2 
= aStr[2];
                
break;
            
case 4:
                commstr 
= aStr[0];
                param1 
= aStr[1];
                param2 
= aStr[2];
                param3 
= aStr[3];
                
break;
            
default:
                commstr 
= aStr[0];
                param1 
= aStr[1];
                param2 
= aStr[2];
                param3 
= aStr[3];
                
break;
        }

        
string s = "";
        
switch (commstr)
        
{
            
case "FirstCallServer":
                s 
= "GetTime?";
                
break;

            
case "GetServerTime":
                s 
= DateTime.Now.ToString();
                
break;

        }


        
return s;
    }


    
void Button2_Click(object sender, EventArgs e)//指定客户端响应事件变更
    {
        
if (Button2.ToolTip == "函数f2")
        
{
            Button1.Attributes.Add(
"onclick""f1();");
            Button2.ToolTip 
= "函数f1";
        }

        
else
        
{
            Button1.Attributes.Add(
"onclick""f2();");
            Button2.ToolTip 
= "函数f2";

        }

        
        
string scriptKey = "intoPopupMessage:" + this.UniqueID;

        
if (!Page.IsStartupScriptRegistered(scriptKey) && Page.IsPostBack)
        
{
            
string scriptBlock =
               
@"<script language=""JavaScript"">
                  alert(""%%POPUP_MESSAGE%%"");
               
</script>";
            scriptBlock = scriptBlock.Replace("%%POPUP_MESSAGE%%""函数变更为:"+Button2.ToolTip);

            Page.RegisterStartupScript(scriptKey, scriptBlock);
        }


    }

    
void Button4_Click(object sender, EventArgs e)//指定客户端脚本
    {
        
string scriptKey = "intoServerScript:" + this.UniqueID;

        
if (!Page.IsStartupScriptRegistered(scriptKey) && Page.IsPostBack)
        
{
            
string scriptBlock =
        
@"
        <script language=""JavaScript"">
        function sf1()
        
{
        
";
            scriptBlock += TextBox1.Text;

            scriptBlock 
+=
        
@"        
        }

        
</script>
        
";
            Page.RegisterStartupScript(scriptKey, scriptBlock);
        }


        Button3.Attributes.Add(
"onclick""sf1();");

    }

    
void Button5_Click(object sender, EventArgs e)//指定客户端脚本并执行
    {
        Button4_Click(
nullnull);

        
string scriptKey = "doIntoServerScript:" + this.UniqueID;

        
if (!Page.IsStartupScriptRegistered(scriptKey) && Page.IsPostBack)
        
{
            
string scriptBlock =
               
@"<script language=""JavaScript"">
                  sf1();
               
</script>";

            Page.RegisterStartupScript(scriptKey, scriptBlock);
        }

    }



}


你可能感兴趣的:(asp.net)