Asked by:
Calling Javascript From CodeBehind In VB.net WebForm

Question
-
User931778073 posted
On my vb.net Webform, I have a jQuery modal with two buttons which opens when the Save button is clicked. One of the buttons on the modal is labeled as "YES" and the other button is labeled as "NO". When the button labeled "YES" is clicked, I would like to return the string "YES". This will be used to determine if the Save() function will execute or not in Codebehind. I'm using ScriptManager.RegisterStartupScript but it is not working.
I get an error stating that ScriptManager.RegisterStartupScript does not produce a value. I don't think this is possible without using AJAX or a hiddenfield but maybe someone can do it without AJAX or a hiddenfield, so I thought I'd ask.
Below are my Javascript and code from from Codebehind.
<script> $(document).ready(function(){ var dialogDiv = $('#myDialog'); dialogDiv.dialog({ autoOpen : false, modal: true, buttons: { 'Yes': function () { confirmChange(); }, 'No': function () { dialogDiv.dialog('close'); } } }); }); function confirmChange() { return 'YES'; } function openDialog() { dialogDiv.dialog('open'); }; </script>
Public Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
If (Page.IsValid = True) Then
lblMsg.Text = String.Empty
Try
If myDropDownList.SelectedItem.Text <> "-Select-" And myTextBox.Text.Trim() <> String.Empty Then
If ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "openDialog();", True))=="YES" Then
Save()
LblMsg.Text = "Data Saved Sucessfully"
Exit Sub
End If
Else
lblMsg.Text = "Please enter data"
Exit Sub
End If
Catch ex As Exception
End Try
End If
End SubTuesday, January 22, 2019 1:25 AM
All replies
-
User475983607 posted
The way the code is designed, the modal dialog opens after the save on the server. You need to open the model before posting to the server not after.
In other works, JavasScript is an application that is loaded and initialized in the browser as a results of executing code behind. The order is code behind executes -> JavaScript executes. Your code assumes the code behind and JavaScript are running at the same time.
Tuesday, January 22, 2019 12:16 PM -
User-1174608757 posted
Hi ProgMaster,
According to your description, The string "YES" is a string in front end , it doesn't exist in code behind. So, if you want to get the string value, you have to send it to code behind.Since you don't want to use Ajax or hidden field to send the value , I suggest you to use ajax pagemethod to call the function in code behind. Here is the demo, I hope it could help you.
aspx:
<head id="Head1" runat="server"> <title></title> <script> function AccessServerSide1() { PageMethods.GetServerSideValue(document.getElementById("aa").value, onSuccess);// this will call function code behind } function AccessServerSide2() { PageMethods.GetServerSideValue(document.getElementById("bb").value, onSuccess); } function onSuccess(response) { alert(response) } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="smPageMethods" runat="server" EnablePageMethods="true"></asp:ScriptManager> <input id="aa" type="button" value="YES" onclick="AccessServerSide1()"/> <input id="bb" type="button" value="NO" onclick="AccessServerSide2()"/> </div> </form> </body> </html>
aspx.cs
public partial class pagemethod : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string GetServerSideValue(string name) { if (name == "YES") { return "YES"; } else if (name == "NO") { return "NO"; } else { return "NONE"; } } }
It shows as below:
Best Regards
Wei Zhang
Wednesday, January 23, 2019 8:18 AM