One of my challenges is transferring a string of data from C# to JavaScript in ASP web forms. My plan involves setting the data as a text for an ASP label in C#, then extracting the label's text by ID in JS.
This is the C# code (ascx.cs file):
List<Event> eventList;
protected void Page_Load(object sender, EventArgs e)
{
string message = string.Empty;
SPSite franasabank = new SPSite("http://lbshrptweb/sites/fransabank/");
SPWeb calendar = franasabank.OpenWeb();
SPList list = calendar.Lists["Fransabank Calendar"];
eventList = new List<Event>();
foreach (SPListItem oItem in list.Items)
{
// Access each item in the list...
DateTime startTime = (DateTime)oItem["Start Time"];
DateTime endTime = (DateTime)oItem["End Time"];
string status = (String)oItem["Status"];
string title = oItem.Title;
string description = (String)oItem["Description"];
Event calendar_event = new Event(startTime, endTime, status, title, description);
eventList.Add(calendar_event);
}
foreach (Event item in eventList)
{
message += item.Title + " " + item.Description + item.StartDate + "-" + item.EndDate + "-" + item.Status + "\n";
}
Label1.Text = message;
}
This snippet shows the HTML with the Label (ascx file):
<div data-ng-app="Calendar">
<div data-ng-controller="CalendarController" id="mycontroller">
<div class="row " data-ng-init="Initialize()">
<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>
And here is the JavaScript code:
<script>
var a = document.getElementById('<%= Label1.ClientID %>');
console.log(a);
</script>
Despite this setup, I'm finding that the data comes through as null in variable 'a'. I've attempted:
var a = document.getElementById('<%= Label1.ClientID %>').innerHTML;
But unfortunately, it also results in null values.