none
pass Javascript output to C# variables RRS feed

  • Question

  • I have a SP2013 Visual WebPart with toolpart class.

    This is a method inside my C# toolpart class.

    How can i pass the value from "_spPageContextInfo.siteAbsoluteUrl" to the string "office".

    i need to do this kind of passing of data from other javascript to other variables as well not just _spPageContextInfo. Thanks in advance

            public string office;
            public void getCurrentOfficeMethod() {
                String csname1 = "PopupScript";
                Type cstype = this.GetType();
                ClientScriptManager cs = Page.ClientScript;
                StringBuilder cstext1 = new StringBuilder();
                cstext1.Append("<script type=text/javascript>office=_spPageContextInfo.siteAbsoluteUrl;</");
                cstext1.Append("script>");
                cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());}


    cal_bonjovi


    Monday, July 22, 2019 12:14 PM

All replies

  • Hi cal_bonjovi,

    You can add a hidden inputbox in ascx and then set JavaScript value to this inputbox, in the code behind, just read this hidden inputbox value for usage:

    <script type="text/javascript">
        
        setTimeout(function () { SetHiddenVariable(); }, 1000);
        function SetHiddenVariable()
        {
            var jsVar = _spPageContextInfo.webAbsoluteUrl;
            // Set the value of the hidden variable to
            // the value of the javascript variable
            var hiddenControl = '<%= inpHide.ClientID %>';
            document.getElementById(hiddenControl).value = jsVar;
        }
    </script>
    
    <input id="inpHide" type="hidden" runat="server" />

    Usage:

     string office = inpHide.Value;

    Reference:

    Access JavaScript variables on PostBack using ASP.NET Code

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 23, 2019 5:22 AM
  • When i add a script tag on my ascx, i got an error " Cannot create XmlSerializers for this Web Part.  ".

    Maybe because of the kind of template I added on my SharePoint Project, i choose Visual Web Part not Visual Web Part (Farm Solution Only)


    cal_bonjovi

    Tuesday, July 23, 2019 6:46 AM
  • Hi,

    Try to create a Visual Web Part solution and select "Deploy as a farm solution":

    In my side, never meet this exception, I can get the value from JavaScript in C# code as capture above shown.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, July 23, 2019 6:58 AM
  • i cant seem to make it work.

    It seems all server side code are executed first on my Visual WP, and the hidden field value is still empty at this point.

    Then client side code executes, to put value on the hidden field.

    But i need to use the value of the hidden field on my server side code.

    Please advise. Thank.s


    cal_bonjovi

    Wednesday, July 24, 2019 6:24 AM
  • Hello,

    I used a button click to trigger JavaScript function, then get the variable value:

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, July 24, 2019 7:01 AM
  • instead of adding javascript, i just did a purely C# code instead. Thanks for the help.

    All your answers were probably correct it just not applicable to me. Let me know if i still need to vote the above answers. Thanks again..


    cal_bonjovi

    Wednesday, July 24, 2019 2:39 PM
  • Hi cal_bonjovi,

    Thanks for sharing the information to us and very glad that you have find the suitable solution yourself.

    You can Mark both the reply above and your reply as answer if it helped, no need to vote the above reply.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, July 24, 2019 3:00 PM