1

I am building a multilingual web application in which i have a .aspx.cs file

public partial class example:System.Web.UI.Page
{
    private string message="message to be displayed depending on user language";
        public string Message
        {
            get{return message;}


            set{} 
         }    
}

and a .aspx file in which i have linked a javascript file for validating user input

<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
     <script type="text/javascript" src="Scripts/DataValidation.js"></script>
    </head>
    <body>
<form id="form1" runat="server" onsubmit="return validation()">
    <asp:TextBox ID="tbSupplierName" CssClass="marginspace" runat="server" Width="150px" MaxLength="30"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" 
                        onclick="btnSearch_Click"   />
</form>
    </body>
    </html>

my DataValidation.js file loks like this

 function validation()
    {
    var SupplierName = document.getElementById('tbSupplierName');
    if (SupplierName.value.length == 0)
    {
    alert('please enter supplier name');//here i want to display server side variable 'message'
    return false;
    }
    }

The problem is i want to pass my server side variable 'message' to the linked external javascript file DataValidation.js and display it in alert

I have tried below code but it is not working

function validation()
    {
  var message='<%=Message%>';
    var SupplierName = document.getElementById('tbSupplierName');
    if (SupplierName.value.length == 0)
    {
    alert(message);
    return false;
    }
    }

Help me to solve the problem.Thanks in advance.

1 Answer 1

4

You can declare global JavaScript variable in your ASP.NET page

 <script>
    message='<%=Message%>';
 </script>

Then you can use it directly in JS file

function validation() {
    var SupplierName = document.getElementById('tbSupplierName');
    if (SupplierName.value.length == 0) {
        alert(message);
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Satpal for helping.will there be any problem declaring global variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.