ClientScriptManager.GetPostBackClientHyperlink 方法

获取一个引用,并在其开头附加 javascript:,可以在客户端事件中使用该引用,并将该引用与指定的事件参数一起使用,以便回发到指定控件的服务器。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

public string GetPostBackClientHyperlink (Control control,string argument)

参数

control : 处理回发的服务器控件。

argument : 传递给服务器控件的参数。

返回值

一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用。

备注

使用 GetPostBackEventReference 方法要求处理回发的控件实现 IPostBackEventHandler 接口。若要为 Page 实现 IPostBackEventHandler 接口,请使用 @ Implements 指令。

下面的代码示例演示 GetPostBackClientHyperlink 方法的用法。自定义控件 MyControl 实现了 IPostBackEventHandler 接口。当单击页面中的 HTML 定位点元素时,会调用该自定义控件的 RaisePostBackEvent 方法。

<%@ Page Language="C#"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  
public class MyControl : Label, IPostBackEventHandler
  
{

    
// Use the constructor to defined default label text.
    public MyControl()
    
{
      
base.Text = "No postback raised.";
    }

    
    
// Implement the RaisePostBackEvent method from the
    
// IPostBackEventHandler interface. 
    public void RaisePostBackEvent(string eventArgument)
    
{
      
base.Text = "Postback handled by " + this.ID.ToString() + ". <br/>" +
                  
"Postback caused by " + eventArgument.ToString() + ".";
      
    }

  }


  
protected void Page_Load(object sender, EventArgs e)
  
{
    
// Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    
// Create an instance of the custom label control and 
    
// add it to the page.
    MyControl mycontrol = new MyControl();
    mycontrol.ID 
= "mycontrol1";
    PlaceHolder1.Controls.Add(mycontrol);
    PlaceHolder1.Controls.Add(
new LiteralControl("<br/>"));
    
    
// Create a button element with its onClick attribute defined
    
// to create a postback event reference to the custom label control.
    HtmlInputButton b = new HtmlInputButton();
    b.ID 
= "mybutton1";
    b.Value 
= "Click";
    b.Attributes.Add(
"onclick", cs.GetPostBackEventReference(mycontrol, b.ID.ToString()));
    PlaceHolder1.Controls.Add(b);
    PlaceHolder1.Controls.Add(
new LiteralControl("<br/>"));
    
    
// Create a link element with its href attribute defined
    
// to create a postback event reference to the custom label control.
    HtmlAnchor a = new HtmlAnchor();
    a.ID 
= "myanchor1";
    a.InnerText 
= "link";
    a.HRef 
= cs.GetPostBackClientHyperlink(mycontrol, a.ID.ToString());
    PlaceHolder1.Controls.Add(a);
  }

</script>

<html  >
<head runat="server">
    
<title>ClientScriptManager Example</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
      
<asp:PlaceHolder id="PlaceHolder1" runat="server">
      
</asp:PlaceHolder>
    
</div>
    
</form>
</body>
</html>

ClientScriptManager.GetPostBackEventReference 方法和ClientScriptManager.GetPostBackClientHyperlink方法的功能是一样的,都是在客户端生成脚本,当控件的特定事件触发时,可以回发至服务器。

他们在客户端的表现形式有点不同,GetPostBackClientHyperlink表现为:javascript:__doPostBack('mycontrol1','myanchor1'),GetPostBackEventReference 表现为::__doPostBack('mycontrol1','myanchor1')。

posted on 2008-07-10 00:19  王丹小筑  阅读(747)  评论(0)    收藏  举报

导航