Aaron Li's Blog

Write it down before I forget

  Home  |   Contact  |   Syndication    |   Login
  30 Posts | 0 Stories | 21 Comments | 1 Trackbacks

News

Google

Archives

Other's Idea

Senario 1: there is a databound control on the page, for example, a Repeater, and for every row, there is a Delete button/link.

Senario 2: on the same page, there is a Delete All button/link.

In Senario 1, we want to know which row to delete after the postback.

In Senario 2, if we use asp button and related event handler, generally the process is Page_Load() where databound happens; then OnClick() event handler where to delete; after that we have to redo the databound which cause an extra round trip to database; as a result, we want to process the deletion right away when Page_Load() is called, then bind data, so only bind data to the databound control for once.

ClientScript.GetPostBackClientHyperlink() or __doPostBack() will help us with these kinds of problems.

ClientScript.GetPostBackClientHyperlink() and __doPostBack() do the same thing, the difference is ClientScript.GetPostBackClientHyperlink() speaks server side language, which means it can recognize server side controls; however, __doPostBack() is javascript.

Let's see how they perform the same function. Suppose there is a Literal control on the page. Later we'll explain what this control is for.

<asp:Literal ID="ltrDeleteSingle" runat="server" EnableViewState="false"/>       

in the code behind, set
string s = ClientScript.GetPostBackClientHyperlink(ltrDeleteSingle, "150");

guess what's the value of s? Here it is,
s="javascript:__doPostBack('ctl00$ctl00$ltrDeleteSingle','150')"

Perhaps you have a diffferent string rather than 'ctl00$ctl00$ltrDeleteSingle' on your page, anyway, it is the ltrDeleteSingle's UniqueID on client side.

Let's go back to our page,

In the HTML portion,

<asp:Repeater ID="rptUserList" runat="server" EnableViewState="false">
    <ItemTemplate>
...
<a href="javascript:__doPostBack('<%=ltrDeleteSingle.UniqueID%>','<%#Eval("UserID")%>')">Delete</a>

                </ItemTemplate>
        </asp:Repeater>
<a href="javascript:__doPostBack('<%=ltrDeleteAll.UniqueID%>','')">Delete All</a>

<asp:Literal ID="ltrDeleteSingle" runat="server" EnableViewState="false"/>       
<asp:Literal ID="ltrDeleteAll" runat="server" EnableViewState="false"/>       

Of course, we can replace javascript:__doPostBack() with

<a href="<%#ClientScript.GetPostBackClientHyperlink(ltrDeleteSingle, Eval("UserID"))%>">Delete</a>               
<a href="<%#ClientScript.GetPostBackClientHyperlink(ltrDeleteSingle, ""))%>">Delete All</a>               

In the c# portion,

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (!string.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
{
if (Request.Form["__EVENTTARGET"].Equals(ltrDeleteSingle.UniqueID))
{
if (!int.TryParse(Request.Form["__EVENTARGUMENT"], out UserID))
<Display error>
else
ToDelete(UserID);
}
else if (Request.Form["__EVENTTARGET"].Equals(ltrDeleteAll.UniqueID))
{
ToDelete(0);
}
}
}
ToBindData();
}

 

Shortly, ClientScript.GetPostBackClientHyperlink() and __doPostBack() give HTML controls the ability to fire postback. By anlyzing Request.Form["__EVENTTARGET"], different actions can be taken and performance could be improved.

We find, ltrDeleteSingle and ltrDeleteAll play the role of indicator so that the code knows who causes the postback then decides what to do. For those controls with AutoPostBack property, for example, dropdownlist or button, if set AutoPostBack =true, the control's UniqueID will be automatically passed in the form hidden field __EVENTTARGET.
posted on Friday, August 10, 2007 12:51 AM

Feedback

# re: ClientScript.GetPostBackClientHyperlink() and __doPostBack() 9/21/2010 6:10 AM dennish
good piece of work..... :-)

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: