Answered by:
calling javascript function from code behind

Question
-
I'm trying to execute a javascript function from code behind...
here is my sample code...
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim objBLLClient As New cls_BLL_Client
objBLLClient.FirstName = txtFName.Text
objBLLClient.LastName = txtLName.Text
objBLLClient.MI = txtMI.TextobjBLLClient.PrimDesp = txtPrimDesp.Text
objBLLClient.PrimAddr1 = txtPrimAddr1.Text
objBLLClient.PrimAddr2 = txtPrimAddr2.Text// few more lines of code..
If Not ClientScript.IsStartupScriptRegistered("testing") Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr()", True)
End If//and few more lines
End Sub
<script type="text/javascript" >
function getAddr() {
myMap.Clear();
var primAddr1 = document.getElementById('<%=txtPrimAddr1.ClientID%>').value;// few more lines...
address= primAddr1 +", " + primAddr2;
startGeocoding(address);
}
I'm not able to execute getAddr from code behind..any help would be appreciated...Friday, March 12, 2010 7:26 AM
Answers
-
It should be "getAddr();" with a semicolon here:
Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr()", True)
Otherwise, near the bottom of the page you'll get a missing ';' error:
<script type="text/javascript"> //<![CDATA[ getAddr()Sys.Application.initialize(); //]]> </script>
BTW, instead of IsStartupScriptRegistered("testing") you'll want the (Type, String) overload. (It's helpful, for example, if you were to move all this into a User Control and include more than one instance of the user control on a page).If Not ClientScript.IsStartupScriptRegistered(Me.GetType(), "testing") Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr();", True) End If
- Proposed as answer by Machine Elf Friday, March 12, 2010 10:25 AM
- Marked as answer by Harry Zhu Thursday, March 18, 2010 1:24 AM
Friday, March 12, 2010 10:24 AM -
You're welcome. Did you try setting a breakpoint in Protected Sub btnSave_Click to make sure it's getting called? If so, are there conditionals that might exit the sub early?
If it's too much grief over a minor mystery, you could "cheat" and detect whether it was clicked in Form_Load like so:<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
'In Form_Load If Not String.IsNullOrEmpty(Request(btnSave.UniqueID)) Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr();", True) End If
- Proposed as answer by Machine Elf Tuesday, March 16, 2010 8:49 PM
- Marked as answer by Harry Zhu Thursday, March 18, 2010 1:24 AM
Friday, March 12, 2010 11:19 PM
All replies
-
It should be "getAddr();" with a semicolon here:
Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr()", True)
Otherwise, near the bottom of the page you'll get a missing ';' error:
<script type="text/javascript"> //<![CDATA[ getAddr()Sys.Application.initialize(); //]]> </script>
BTW, instead of IsStartupScriptRegistered("testing") you'll want the (Type, String) overload. (It's helpful, for example, if you were to move all this into a User Control and include more than one instance of the user control on a page).If Not ClientScript.IsStartupScriptRegistered(Me.GetType(), "testing") Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr();", True) End If
- Proposed as answer by Machine Elf Friday, March 12, 2010 10:25 AM
- Marked as answer by Harry Zhu Thursday, March 18, 2010 1:24 AM
Friday, March 12, 2010 10:24 AM -
Thanks... Machine Elf
I tried but couldnt get it to work...
but when I place this in the Page_Load, getAddr() is executed...Friday, March 12, 2010 1:24 PM -
You're welcome. Did you try setting a breakpoint in Protected Sub btnSave_Click to make sure it's getting called? If so, are there conditionals that might exit the sub early?
If it's too much grief over a minor mystery, you could "cheat" and detect whether it was clicked in Form_Load like so:<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
'In Form_Load If Not String.IsNullOrEmpty(Request(btnSave.UniqueID)) Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr();", True) End If
- Proposed as answer by Machine Elf Tuesday, March 16, 2010 8:49 PM
- Marked as answer by Harry Zhu Thursday, March 18, 2010 1:24 AM
Friday, March 12, 2010 11:19 PM -
Hey bing!
Did that work out OK?Tuesday, March 16, 2010 8:48 PM -
Hi,
For the questions relating to asp.net ,please post to: http://forums.asp.net/.
Thanks,
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Thursday, March 18, 2010 1:25 AM -
It should be "getAddr();" with a semicolon here:
Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr()", True)
Otherwise, near the bottom of the page you'll get a missing ';' error:
<script type="text/javascript"> span style="color:Green;">// script>
BTW, instead of IsStartupScriptRegistered("testing") you'll want the (Type, String) overload. (It's helpful, for example, if you were to move all this into a User Control and include more than one instance of the user control on a page).If Not ClientScript.IsStartupScriptRegistered(Me.GetType(), "testing") Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "testing", "getAddr();", True) End If
This is what I'm looking for, The sample you shared is very valuable.Tuesday, August 24, 2010 12:23 AM -
This is what I'm looking for, The sample you shared is very valuable.
Thanks Archie, I'm glad it helped. Feel free to click the "vote as helpful" arrow, thank you!Thursday, August 26, 2010 5:15 AM