ASP.NET GetPostBackEventReference
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET GetPostBackEventReference
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.__doPostBack("id","")方法
2.GetPostBackEventReference方法作用
3.客戶端如何觸發(fā)服務(wù)器端控件的事件
右邊提供程序用此方法實(shí)現(xiàn)在客戶端單擊按鈕后,禁用此按鈕,直到程序運(yùn)行完畢再開(kāi)啟按鈕。(單擊右邊下載)
下面再舉個(gè)小例子.
前臺(tái)頁(yè)面
有個(gè)服務(wù)器控件 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
一個(gè)客戶端控件用來(lái)觸發(fā)服務(wù)器端
<a href="#" οnclick="document.getElementById('Button1').click()">觸發(fā)服務(wù)器端按鈕事件</a>
(2)
利用GetPostBackEventReference給客戶端生成__doPostBack()
前臺(tái)
<a href="#" οnclick="<%=PostBack()%>">觸發(fā)服務(wù)器端按鈕事件</a>
后臺(tái)
protected string PostBack()
{
return this.Page.GetPostBackEventReference(this.Button1,"haha");
}
通過(guò)__EVENTARGUMENT="haha"可以判斷是不是點(diǎn)了那個(gè)鏈接的PostBack
把Button1的按鈕事件這么寫(xiě):
if(Request["__EVENTARGUMENT" ]=="haha")
{
Response.Write("這個(gè)是鏈接的PostBack");
}
else
{
Response.Write("這個(gè)不是鏈接的PostBack");
}
以下這個(gè)是微軟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>");
}
}
2.GetPostBackEventReference方法作用
3.客戶端如何觸發(fā)服務(wù)器端控件的事件
右邊提供程序用此方法實(shí)現(xiàn)在客戶端單擊按鈕后,禁用此按鈕,直到程序運(yùn)行完畢再開(kāi)啟按鈕。(單擊右邊下載)
下面再舉個(gè)小例子.
前臺(tái)頁(yè)面
有個(gè)服務(wù)器控件 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
一個(gè)客戶端控件用來(lái)觸發(fā)服務(wù)器端
<a href="#" οnclick="document.getElementById('Button1').click()">觸發(fā)服務(wù)器端按鈕事件</a>
(2)
利用GetPostBackEventReference給客戶端生成__doPostBack()
前臺(tái)
<a href="#" οnclick="<%=PostBack()%>">觸發(fā)服務(wù)器端按鈕事件</a>
后臺(tái)
protected string PostBack()
{
return this.Page.GetPostBackEventReference(this.Button1,"haha");
}
通過(guò)__EVENTARGUMENT="haha"可以判斷是不是點(diǎn)了那個(gè)鏈接的PostBack
把Button1的按鈕事件這么寫(xiě):
if(Request["__EVENTARGUMENT" ]=="haha")
{
Response.Write("這個(gè)是鏈接的PostBack");
}
else
{
Response.Write("這個(gè)不是鏈接的PostBack");
}
以下這個(gè)是微軟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>");
}
}
總結(jié)
以上是生活随笔為你收集整理的ASP.NET GetPostBackEventReference的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 深入学习__doPostBack函数
- 下一篇: 23种基本设计模式简介