Run client javascript after servercode is executed right ?

J

jesper_lofgren

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
 
L

Laurent Bugnion [MVP]

Hi,

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
 
M

marss

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
}
 
J

jesper_lofgren

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:
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
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top