简体   繁体   中英

Javascript not Alerting String Variable Value From Database in Code behind C#, but alerting integer variable, why?

I am facing issue with alerting Database value fetching in code behind C# and alerting them at client side. Issue is for string value. Eg - Suppose any user has set their password as integer, then the code is easily alerting the value, but if the password is string then it's not alerting any thing. Even it not alerting as 'undefined', while value is there with variable.

See the below example

Code Behind C#

public string strPassword = string.Empty;
public string strEmailPassword = string.Empty;
var objData = UserBusiness.GetUserByID(strUserID);
if (objData != null)
{
    strPassword = objData.Password;
    strEmailPassword = objData.EmailPwd;
}

At JavaScript

$(document).ready(function () {
    var strPwd = <%= strPassword %> ;
    $('#<%= txtLoginPassword.ClientID%>').val(strPwd);
    var strEmailPwd = <%= strEmailPassword %> ;
    $('#<%= txtEmailPwd.ClientID%>').val(strEmailPwd);
});

Change your JavaScript code like this.

$(document).ready(function () {
    var strPwd = '<%= strPassword %>' ;
    $('#<%= txtLoginPassword.ClientID%>').val(strPwd);
    var strEmailPwd = "<%= strEmailPassword %>" ;
    $('#<%= txtEmailPwd.ClientID%>').val(strEmailPwd);
});

Reason: Strings must be place inside quotes ' or " and integers doesn't need quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM