Run client javascript after servercode is executed right ?

Discussion in 'ASP .Net' started by jesper_lofgren@yahoo.se, Jan 16, 2007.

  1. Guest

    Hello,

    I have a problem, i have a event in asp.net codebehind file that
    updating a datasource. If that update went well i want to refresh my
    treeview thats on another page. The script works well, the problem
    is.... the client script runs before the updatemethod !

    I give u some ex code here...


    aspx page

    <script language="javascript" type="text/javascript">
    function UpdateFieldTree()
    {
    parent.UpdateMenu();
    }
    </script>

    <asp:Button ID="BtnAddCostCenter" Text="Lägg till" runat="server"
    OnClick="AddData_Click" />



    aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
    this.BtnAddCostCenter.Attributes.Add("onclick",
    "javascript:parent.UpdateMenu();");
    }

    protected void AddData_Click(object sender, EventArgs e)
    {

    // Save list
    jm.orderweb.formmanagement.ListManagement.SaveList(listObject);

    // Now i want to run the javascript !!

    }

    Hope you have some ideas that can help me, this should not be some odd
    case.

    Thanks
    Jeppe
     
    , Jan 16, 2007
    #1
    1. Advertising

  2. Hi,

    wrote:
    > Hello,
    >
    > I have a problem, i have a event in asp.net codebehind file that
    > updating a datasource. If that update went well i want to refresh my
    > treeview thats on another page. The script works well, the problem
    > is.... the client script runs before the updatemethod !
    >
    > I give u some ex code here...
    >
    >
    > aspx page
    >
    > <script language="javascript" type="text/javascript">
    > function UpdateFieldTree()
    > {
    > parent.UpdateMenu();
    > }
    > </script>
    >
    > <asp:Button ID="BtnAddCostCenter" Text="Lägg till" runat="server"
    > OnClick="AddData_Click" />
    >
    >
    >
    > aspx.cs
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > this.BtnAddCostCenter.Attributes.Add("onclick",
    > "javascript:parent.UpdateMenu();");
    > }
    >
    > protected void AddData_Click(object sender, EventArgs e)
    > {
    >
    > // Save list
    > jm.orderweb.formmanagement.ListManagement.SaveList(listObject);
    >
    > // Now i want to run the javascript !!
    >
    > }
    >
    > Hope you have some ideas that can help me, this should not be some odd
    > case.
    >
    > Thanks
    > Jeppe


    You got the timing wrong. What you do is add a click handler to the
    button which will then submit the form (when you have a button linked to
    a server-side Click handler, this button is rendered by a HTML button on
    the client and by a client-side code which will submit the form to the
    server. The server then reads some form fields and calls the
    corresponding event handler).

    In your case, if you add a client-side script to the button, as you
    found out, the client-side script will first be executed, then the form
    will be sent (thus triggering the server side action "Add_Data_Click").

    What you must do is:

    1) Client sends form to server
    2) Server executes the action
    3) Server generates a script which must be executed when the page is
    loaded on the client.

    Which means that you should remove the "onclick" attribute from your
    button. After the method AddDate_Click is executed, use the server-side
    object Page.ClientScript to register the client script which must be
    executed.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.aspx

    Example:

    protected void AddData_Click(object sender, EventArgs e)
    {

    // Save list
    jm.orderweb.formmanagement.ListManagement.SaveList(listObject);

    // Now i want to run the javascript !!
    if ( success )
    {
    this.RegisterStartupScript( this.GetType(),
    "uniqueID",
    "parent.UpdateMenu();",
    true );
    }
    }

    Note that your method "UpdateFieldTree" is not needed.

    Note also that JavaScript uses the Java naming and coding conventions,
    so normally methods are named "updateMenu" instead of "UpdateMenu".

    HTH,
    Laurent
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion [MVP], Jan 16, 2007
    #2
    1. Advertising

  3. marss Guest

    wrote:

    > Hope you have some ideas that can help me, this should not be some odd
    > case.


    If you use 2.0 framework you can also achive your goal by callback.
    http://msdn2.microsoft.com/en-us/library/ms153106.aspx

    Example:
    page:
    <script language="javascript" type="text/javascript">
    function UpdateFieldTree()
    {
    parent.UpdateMenu();
    }
    </script>
    <asp:Button ID="BtnAddCostCenter" Text="Lägg till" runat="server" />

    code:
    public partial class SomePage : System.Web.UI.Page,
    ICallbackEventHandler
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    BtnAddCostCenter.Attributes["onclick"] =
    Page.ClientScript.GetCallbackEventReference(this,"","UpdateFieldTree",null);
    }

    #region ICallbackEventHandler Members

    public string GetCallbackResult()
    {
    return "";
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
    //save list
    jm.orderweb.formmanagement.ListManagement.SaveList(listObject);
    }

    #endregion
    }
     
    marss, Jan 16, 2007
    #3
  4. Guest

    Thanks for the answers, i have updated my code so the executing should
    be in right time now.
    The only problem i have now is that i use a asp:UpdatePanel around all
    the content and then the javascript not fire.

    Any ideas ?

    // Jeppe


    marss skrev:

    > wrote:
    >
    > > Hope you have some ideas that can help me, this should not be some odd
    > > case.

    >
    > If you use 2.0 framework you can also achive your goal by callback.
    > http://msdn2.microsoft.com/en-us/library/ms153106.aspx
    >
    > Example:
    > page:
    > <script language="javascript" type="text/javascript">
    > function UpdateFieldTree()
    > {
    > parent.UpdateMenu();
    > }
    > </script>
    > <asp:Button ID="BtnAddCostCenter" Text="Lägg till" runat="server" />
    >
    > code:
    > public partial class SomePage : System.Web.UI.Page,
    > ICallbackEventHandler
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > BtnAddCostCenter.Attributes["onclick"] =
    > Page.ClientScript.GetCallbackEventReference(this,"","UpdateFieldTree",null);
    > }
    >
    > #region ICallbackEventHandler Members
    >
    > public string GetCallbackResult()
    > {
    > return "";
    > }
    >
    > public void RaiseCallbackEvent(string eventArgument)
    > {
    > //save list
    > jm.orderweb.formmanagement.ListManagement.SaveList(listObject);
    > }
    >
    > #endregion
    > }
     
    , Jan 17, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry
    Replies:
    4
    Views:
    1,521
    =?Utf-8?B?aGFyaSBwcmFzYWQ=?=
    Jun 15, 2004
  2. wyleu
    Replies:
    4
    Views:
    259
  3. Ryan George
    Replies:
    3
    Views:
    345
    Dennis Verdonschot
    Sep 7, 2010
  4. Raghu Raman
    Replies:
    0
    Views:
    245
    Raghu Raman
    Jan 24, 2005
  5. Oran
    Replies:
    2
    Views:
    550
Loading...

Share This Page