Is there a way to retrieve a constant from a Constants.cs file by using javascript? I can retrieve the my constant in my ascx page using Constants.Lists.DisplayName.MyList. I was wondering if I can reuse the same method of calling the list in javascript. I am currently using JSOM and I would need the list name as one of my parameters.


In .ascx, add Literal:

<asp:Literal runat="server" ID="MyListName" Mode="PassThrough" />

Then in .cs, add value for that control like this, e.g., in OnInit:

MyListName.Text = "<script type='text/javascript'>var myListName='" + Constants.Lists.DisplayName.MyList + "';</script>";

Finally in JS, use myListName variable like a boss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.