1.__doPostBack("id","")方法
2.GetPostBackEventReference方法作用
3.客户端如何触发服务器端控件的事件
右边提供程序用此方法实现在客户端单击按钮后,禁用此按钮,直到程序运行完毕再开启按钮。(单击右边下载)
下面再举个小例子.
前台页面
有个服务器控件 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
一个客户端控件用来触发服务器端
<a href="#" onclick="document.getElementById('Button1').click()">触发服务器端按钮事件</a>
(2)
利用GetPostBackEventReference给客户端生成__doPostBack()
前台
<a href="#" onclick="<%=PostBack()%>">触发服务器端按钮事件</a>
后台
protected string PostBack()
{
return this.Page.GetPostBackEventReference(this.Button1,"haha");
}
通过__EVENTARGUMENT="haha"可以判断是不是点了那个链接的PostBack
把Button1的按钮事件这么写:
if(Request["__EVENTARGUMENT" ]=="haha")
{
Response.Write("这个是链接的PostBack");
}
else
{
Response.Write("这个不是链接的PostBack");
}
以下这个是微软MSDN上的例子:
public class MyControl : Control, IPostBackEventHandler
{
// Create an integer property that is displayed when
// the page that contains this control is requested
// and save it to the control's ViewState property.
public int Number
{
get
{
if ( ViewState["Number"] !=null )
return (int) ViewState["Number"];
return 50;
}
set
{
ViewState["Number"] = value;
}
}
// Implement the RaisePostBackEvent method from the
// IPostBackEventHandler interface. If 'inc' is passed
// to this method, it increases the Number property by one.
// If 'dec' is passed to this method, it decreases the
// Number property by one.
public void RaisePostBackEvent(string eventArgument)
{
if ( eventArgument == "inc" )
Number = Number + 1;
if ( eventArgument == "dec" )
Number = Number - 1;
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void Render(HtmlTextWriter writer)
{
// Converts the Number property to a string and
// writes it to the containing page.
writer.Write("The Number is " + Number.ToString() + " (" );
// Uses the GetPostBackEventReference method to pass
// 'inc' to the RaisePostBackEvent method when the link
// this code creates is clicked.
writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"inc") + "\">Increase Number</a>");
writer.Write(" or ");
// Uses the GetPostBackEventReference method to pass
// 'dec' to the RaisePostBackEvent method when the link
// this code creates is clicked.
writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"dec") + "\">Decrease Number</a>");
}
}
回调中有两个方法
GetPostBackClientHyperlink() 与 GetPostBackEventReference()
他们之间的区别如下:
我们来看二个例子
<%=Page.ClientScript.GetPostBackClientHyperlink(Button1, "") %>;
<%= Page.ClientScript.GetPostBackEventReference(Button1,"") %>
第一句.结果类似于
javascript:__doPostBack('Button1','');
第二句,结果类似于
__doPostBack('Button1','');
我们可以看到,这二个作用基本上差不多,字面意思,
可以理解
第一个为连结生成回传语句用,
第二个为引发某控件事件.所写的一个回传方法
如果你使用
Page.ClientScript.GetPostBackClientHyperlink或
Page.ClientScript.GetPostBackEventReference
页面上还会生成
<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit ¦ ¦ (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>
这段js代码
通过字面意思,我们可以理解
eventTarget -> 表明引发回传的控件 ,asp.net通过判断此参数来选择所执行的事件
eventArgument -> 表明事件参数 (这个参数如果实现在IPostBackEvent..好像是这么拼的,这个接口,可以获取到.偷点懒用Request.Pamas["__EVENTARGUMENT"] 也行)