This is a migrated thread and some comments may be shown as answers.

Firing Javascript function After Postback

6 Answers 2339 Views
Button
This is a migrated thread and some comments may be shown as answers.
pedrotti
Top achievements
Rank 1
pedrotti asked on 20 Oct 2011, 03:41 PM
Hello

I want to execute a javascript function, after a button click

It's easy to execute javascript and after postback  on the same button, but not postback and after javascript !

Please help me !

Anne

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Oct 2011, 06:51 AM
Hello Levefaude,

You can try the following approaches to execute javascript after a button click.

1. C#:
protected void btn_Click(object sender, EventArgs e)
{
  string radconfirmscript = "<script language='javascript'>function f(){test(); Sys.Application.remove_load(f) ;}; Sys.Application.add_load (f);</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "radconfirm", radconfirmscript);
}

JS:
<script type="text/javascript">
function test()
{
 alert("Hi");
}
</script>

2. C#:
protected void Button1_Click(object sender, EventArgs e)
{
   Response.Write("<script language=JavaScript> alert('Hello'); </script>");
}

3. C#:
protected void Button1_Click(object sender, EventArgs e)
{
   string s = "<script language=JavaScript>alert('Hello');</script>";
   Page.ClientScript.RegisterStartupScript(this.GetType(), "test", s);
}

Thanks,
Shinu.
0
pedrotti
Top achievements
Rank 1
answered on 21 Oct 2011, 10:00 AM
Thank you, but it does not work with an a little bit complex procedure

I am surprised that telerik is not planned an afterpostback feature for the controls!!!!
Anthem, free library, had planned everything!

Thank you and have a good day

Anne
0
Slav
Telerik team
answered on 25 Oct 2011, 02:30 PM
Hi Levefaude,

I would also recommend using the RegisterStartupScript method of the ScriptManager control in order to register a client script that will be executed after the postback. This method can be used as shown below:

Page Markup:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="Scriptmanager1" runat="server" />
    <div>
        <telerik:RadButton runat="server" ID="RadButton" Text="Click"
            onclick="RadButton_Click"></telerik:RadButton>
    </div>
    </form>
</body>
</html>

Code-Behind:
protected void RadButton_Click(object sender, EventArgs e)
{
    string script = "alert('Hello');";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "testScript", script, true);
}

The afterpostback feature that you are suggesting is indeed interesting. It will be brought to the attention of our developers and it will be considered for implementation, depending on its popularity. I have logged this request as a PITS Issue. You can check its status by following this link: Public URL.

Feel free to contact us again if you encounter more difficulties.

All the best,
Slav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
pedrotti
Top achievements
Rank 1
answered on 27 Oct 2011, 08:56 AM
Yesss ! Thank you, that works very well

Very good day, rainy in France

Anne
0
Jay
Top achievements
Rank 1
answered on 24 Aug 2015, 07:16 PM

I've been struggling with this for a few days now and can't seem to get it to work. I'm guessing my problem might be that I've got a more complicated situation with 2 master pages and a couple ascx pages? .ascx page #1 has a folder tree on right and loads .ascx page #2 based on what folder is clicked.

Here's my issues:
1. If I put the script in the .ascx page it doesn't get added to the page. I have to put it in the .aspx page that calls the 2 ascx controls.
2. It will show up in my debugging but it never gets hit?

Here's what I'm using code wise (asp.net)
JS call inside of a Telerik:RadCodeBlock

function viewFolderDocuments() {
            var strUrl;
            strUrl = '<%=GetViewStateString(PDMPlanroom.QUERY_STR_VIEW_URL)%>';
            window.open(strUrl, "_blank");

Then my codebehind button click event has this code:

Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "viewFolderDocuments", "viewFolderDocuments();", True)

The script NEVER gets hit?

 

0
pedrotti
Top achievements
Rank 1
answered on 25 Aug 2015, 07:42 AM

If it can help you


1 / We are also working with a treeview on a main page PP


Our page is divided into two radpane by a splitter

Click on treeview (client side) , we modified the right radPane adress


function onClientNodeClick(sender, args) {
var treeNode = args.get_node();
treeNode.set_selected(true);
var ZoneDetail = $find("<%=PaneDetail.ClientID%>");


var Categorie = treeNode._attributes.getAttribute("Categorie");
var ID = treeNode.get_value();
NoeudEnCours = treeNode;



switch (Categorie) {
case"F-Personnes":
{
ZoneDetail.set_contentUrl(repPersonnes + "Personne.aspx?Action=M&PerID=" + ID);
FicheEnCours.Action = "M";
}
break;





case"F-Forets":
{
ZoneDetail.set_contentUrl(repForets + "Foret.aspx?Action=M&ForID=" + ID);
}
break;


......
2 / To update the treeview from the detail page PD ....
Server side
PrivateSub MajTreeviewPrinc(CPCommune AsString)
Dim Libelle AsString


…...


Try
Fnscript = "<script>Maj('" & Libelle & "')</script>"
ScriptManager.RegisterStartupScript(Page, Me.GetType(), "Maj", Fnscript, False)
EndIf
Catch Ex AsException


EndTry


EndSub









Client Side


function Maj(LibelleNom) {
var tv = window.parent;
tv.MajLibelle(LibelleNom);
}




And on PP


Client Side


function MajLibelle(libelle) {
treeView = $find("<%= tvPersonnes.ClientID %>");
var node = treeView.get_selectedNodes();
node[0].set_text(libelle);
}

Have a good day

 

Anne

Tags
Button
Asked by
pedrotti
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
pedrotti
Top achievements
Rank 1
Slav
Telerik team
Jay
Top achievements
Rank 1
Share this question
or