1

I have an aspx.cs page which has a method that needs to registers the Javascript file & a method. Could someone guide me as to how to register the JS file & a method in it.

aspx.cs

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
        "TaskControlJSON", "<script language='javascript' 
        type='text/javascript'>" + objLoginJson + "</script>"
        , false);
}

Javascript file that needs to be registered to the cs file.

function GetLoginJson(strLoginJson) {
    if (strLoginJson != '' && strLoginJson != undefined) {
        var objLoginJson = eval('"+strLoginJson+"');
    }
    if (objLoginJson.LoginSuccess == "1") {

    }
    else if (objLoginJson.LoginSuccess == "0") {

        if (objLoginJson.txtUserName != '' && objLoginJson.txtUserName != '') 
        {
            $('#txtUserName').attr("class", objLoginJson.txtUserName);
        }
        if (objLoginJson.txtPassword != '' && objLoginJson.txtPassword != '') 
        {
            $('#txtPassword').attr("class", objLoginJson.txtPassword);
        }
        if (objLoginJson.txtTestTokenNumber1 != '' 
             && objLoginJson.txtTestTokenNumber1 != '') 
        {
            $('#txtTestTokenNumber1').attr("class"
               , objLoginJson.txtTestTokenNumber1);
        }
        if (objLoginJson.txtTestTokenNumber2 != '' 
             && objLoginJson.txtTestTokenNumber2 != '') 
        {
            $('#txtTestTokenNumber2').attr("class",
               objLoginJson.txtTestTokenNumber2);
        }
        if (objLoginJson.txtTestTokenNumber3 != '' 
            && objLoginJson.txtTestTokenNumber3 != '') 
        {
            $('#txtTestTokenNumber3').attr("class", 
               objLoginJson.txtTestTokenNumber3);
        }
        if (objLoginJson.txtTestTokenNumber4 != '' 
             && objLoginJson.txtTestTokenNumber4 != '') 
        {
            $('#txtTestTokenNumber4').attr("class",
                objLoginJson.txtTestTokenNumber4);
        }
    }
    return objLoginJson;
}

This method returns a Json string, if an user provides incorrect Login credentials. Thanks in advance.

2 Answers 2

2

Since you a pretty big block of javascript code, this is best kept in a separate js file.

You can use the ScriptManager.RegisterClientScriptInclude method to achieve this.

ScriptManager.RegisterClientScriptInclude(
            this,
            typeof(Page),
            "LoginScript",
            ResolveClientUrl("~/scripts/login.js"));

You have not specified how you will use your js function GetLoginJson, so I will just give a reference. Your js file could call it by something like this:

$(document).ready(function(){
    $("#loginButton").click(function(){
       var strLoginJson = "";//form the string as you need
       GetLoginJson(strLoginJson);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Your code will generate another script tag, I was trying to keep all the js in one file. Also, you can avoid the <script></script> tags if you set the last parameter to true.
0

Ok, I did a little tweaking around & made it to work.

protected void Page_PreRender(object sender, EventArgs e)
{
    if (strLoginJson != string.Empty)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "LoginControlJSON", "<script language='javascript' type='text/javascript'>GetLoginJson(" + strLoginJson + ");</script>", false);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.