Calling __doPostBack in javascript

Update: There is an existing .Net Framework method Page.GetPostBackEventReference that emits client-side script that initiates postback and also provides a reference to the control that initiated the postback event. It is well described in MSDN article Generating Client-Side Script for Postback“. So my function should call and  in most cases GetPostBackEventReference can be used directly.

Original Post:

I've used a function to submit postback from my javascript by passing Id of the link server control as it was suggested in article “How postback works in ASP.NET” 

   //call the postback function with the right ID
 __doPostBack('ControlId','');
and it worked fine for a while.

However when I moved the link to the User control, it stopped to work.

I recognized that I need to provide fully qualified Control.ClientId but it didn't work.
In debugger I found that when calling the __doPostBack(), ASP.NET generates ID with dollar - $ -separator, not underscore that is used in ClientID, e.g. 'userContol1$linkContol1'.

I found confirmation to this in ASP.NET Server Control - Design Time Support  and .NET Development: PressButton Design and Server Control . Microsoft provides  UniqueIDWithDollars  internal method, that can be accessed through reflection.

However I found that it is simpler just to duplicate code in static function.

The working version of javascript function to __doPostBack :

            public static bool RegisterFunctionToPostBack(string sFunctionName,Control ctrl)

            { //from http://www.xefteri.com/articles/show.cfm?id=18 How postback works in ASP.NET

                  if (HttpContext.Current.Request.Browser.JavaScript)

                  {

 

                        string sJS ="    function " + sFunctionName + @"()

                        {

                            //call the postback function with the right ID

                             __doPostBack('" + UniqueIDWithDollars(ctrl) + @"','');

                        }";

                        sJS=JScriptHelper.JScript(sJS);

                        ctrl.Page.RegisterStartupScript(sFunctionName, sJS);

                        return true;

                  }

                  return false;

            }

            //from http://www.codeproject.com/aspnet/DesignTimeSupport.asp

            public static string UniqueIDWithDollars(Control ctrl)

            {

                        string sId = ctrl.UniqueID;

                        if (sId == null)

                        {

                              return null;

                        }

                        if (sId.IndexOf(':') >= 0)

                        {

                              return sId.Replace(':', '$');         

                        }

                        return sId;

            }

If I would start the function from the scratch, I probably would consider ScriptCallback/AJAX approach.

ctrl.Page.GetPostBackEventReference(ctrl)  instead of 
__doPostBack('" + UniqueIDWithDollars(ctrl) + @"','');
 

 

posted @ Friday, November 04, 2005 2:25 PM

Print

Comments on this entry:

# Good article!

Left by Evagoras Charalambous at 11/4/2005 4:01 PM
Gravatar
I wasn't aware of this difference. Good catch and great of you to post your solution here for us!

# re: Calling __doPostBack in javascript

Left by Jørn Schou-Rode at 5/29/2006 9:57 PM
Gravatar
Great tip! Very handy when you need to implement a page with an automatically starting file download. Emit the output of the following method call using Page.ClientScript.RegisterStartupScript() and you are rolling: string.Format("setTimeout(\"{0}\",100);", Page.GetPostBackClientEvent(lbtStartDownload, null))

# re: Calling __doPostBack in javascript

Left by Welcome at 2/24/2007 5:56 PM
Gravatar

not working

("setTimeout(\"{0} Very handy when you need to implement a page with an automatically starting file download. Emit the output of the following method call using Page.ClientScript.RegisterStartupScript() and you are rolling: string.Format("setTimeout(\"{0}

# Great tip! Very handy when you need to implement a page with an automatically starting file download. Emit the output of the fol

Left by Great tip! Very handy when you n at 10/11/2007 1:08 AM
Gravatar
Great tip! Very handy when you need to implement a page with an automatically starting file download. Emit the output of the following method call using Page.ClientScript.RegisterStartupScript() and you are rolling: string.Format("setTimeout(\"{0}\",100);", Page.GetPostBackClientEvent(lbtStartDownload, null))

# re: Calling __doPostBack in javascript

Left by Bhupinder at 10/21/2007 11:48 PM
Gravatar
have a nice day

# re: Calling __doPostBack in javascript

Left by Oleh at 11/16/2007 6:51 PM
Gravatar
Instead of __doPostBack('" + UniqueIDWithDollars(ctrl) + @"','');
use
__doPostBack('" + ctrl.ClientID + @"',''); //for IE
but for other browsers you should use
__doPostBack('" + ctrl.UniqueID + @"','');

# re: Calling __doPostBack in javascript

Left by Michael Freidgeim at 11/16/2007 11:04 PM
Gravatar
Oleg,
I beleive, that Page.GetPostBackEventReference will handle different browsers for you.
Also see AJAX client library. It should have similar methods.

# re: Calling __doPostBack in javascript

Left by Rajni Padhiyar at 11/29/2007 4:17 AM
Gravatar
You can do post back using hidden input's click event by javascript.

Thanks
Regards
Rajni Padhiyar
Project Leader
Rajni Padhiyar

# re: Calling __doPostBack in javascript

Left by praveen at 1/27/2008 9:46 PM
Gravatar
Using __doPostBack with ASCX page

Ascx page

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Test.ascx.cs" Inherits="Test" %>
<script language=javascript>
function call_me()
{
__doPostBack('frmTest$btnHdn',""); (need to find the Unique ID of the hidden button first and assign that id here)
}
function __doPostBack(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1)
{
theform = document.forms["frmDef"];
}
else
{
theform = document.frmDef; (frmDef is the form id of aspx page to this control is registered – check below the ascx form id)
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
</script>
<input type=button runat=server onclick="call_me();" id="btnClick" />
<input type=button runat=server id="btnHdn" visible=false onserverclick="Call_Server"/>

Code behind

protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterHiddenField("__EVENTTARGET", this.btnHdn.ClientID); (need to register these two events on pageload)
Page.RegisterHiddenField("__EVENTARGUMENT","");
}

protected void Call_Server(Object sender, EventArgs e)
{
Response.Write("Called from JS");
}

# re: Calling __doPostBack in javascript

Left by kiquenet at 2/13/2008 2:40 AM
Gravatar
Hi mister,
where is the code about JsScriptHelper ?

thanks.

# re: Calling __doPostBack in javascript

Left by Michael Freidgeim at 2/13/2008 8:54 AM
Gravatar
In My JScriptHelper class http://geekswithblogs.net/mnf/articles/102574.aspx

# re: Calling __doPostBack in javascript

Left by rajesh at 6/16/2008 8:08 PM
Gravatar
hi
i have used button click for hidding datagrid
but when we click refresh it is again showin
how to make autopostback false in javascript

# re: Calling __doPostBack in javascript

Left by Michael Freidgeim at 6/29/2008 10:46 PM
Gravatar
rajesh,
click refresh -is the same as open the page again.
If you want to keep state (e.g is datagrid visible)after Refresh, you probably need to use Session.

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 5 and type the answer here:
 

Live Comment Preview:

 
«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678