简体   繁体   中英

how to call javascript function from ascx code behind using C#

I want to call javascript function which is in login.ascx file when user click on log in button.

Here is a code for login.ascx code:

              <div class="dnnFormItem">
                <asp:label id="lblLogin" runat="server" AssociatedControlID="cmdLogin" CssClass="dnnFormLabel" />
               <asp:LinkButton id="cmdLogin" resourcekey="cmdLogin" cssclass="dnnPrimaryAction" text="Login" runat="server"/>
               <script type="text/javascript">
/*globals jQuery, window, Sys */
(function ($, Sys) {
    function setUpLogin() {
        var actionLinks = $("a[id$=cmdLogin]");
        actionLinks.click(function () {
            if ($(this).hasClass("dnnDisabledAction")) {
                return false;
            }

            actionLinks.addClass("dnnDisabledAction");
        });
    }

    $(document).ready(function () {
        setUpLogin();
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
            setUpLogin();
        });
    });
}(jQuery, window.Sys));

Below is the javascript code in login.ascx which i want to call when user click on login button.

function myTestFun() {





    //var  _currentUser =

    //           DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();

    var userName = '<%=username%>';

    var fname = '<%=firstnm%>';

    var userinfo= {

        "Usr_Username": userName,
         "Usr_Email": "",
         "Usr_FirstName": fname
    };

     localStorage.setItem('GoDashProUser', JSON.stringify(GoDashProUser));
   }
   </script>

Here is code behind file using C#:

         private void OnLoginClick(object sender, EventArgs e)
    {
        ClientScriptManager cs = Page.ClientScript;

            if (loginStatus == UserLoginStatus.LOGIN_USERNOTAPPROVED)
            {
                message = "UserNotAuthorized";
            }
            else
            {
                authenticated = (loginStatus !=UserLoginStatus.LOGIN_FAILURE);
            }

            //Raise UserAuthenticated Event
            var eventArgs = new UserAuthenticatedEventArgs(objUser, txtUsername.Text, loginStatus, "DNN")
                                {
                                    Authenticated = authenticated, 
                                    Message = message,

                                };
            //string script = "<script type=\"text/javascript\"> myTestFun(); </script>";
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", script);
           // cs.RegisterStartupScript(GetType(), "myTestFun", "myTestFun();", true);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "myTestFun()", true);
            ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), UniqueID, "myTestFun();", true);

            firstnm = objUser.FirstName;

            OnUserAuthenticated(eventArgs);
        }
    }

I have tried most possible options to call the javascript function but it will not call. I want to call javascript function to save data into local storage and also want to pass the values of userinfo.

Please suggest me some way to do so. Thank you.

 <asp:LinkButton OnClientClick="myTestFun()" id="cmdLogin" resourcekey="cmdLogin" cssclass="dnnPrimaryAction" text="Login" runat="server"/>

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