Get data from aspx.cs page to aspx page.
- by Brad8118
So I am using a jquery plug in that allows me to change the order of things in a list by dragging and dropping them.
So my goal is to be able to grab a list of my objects (AlertInfo) and using it in a javascript function.
I was able to use a json webservice call in a test project to pass the data to the page.
But we don't have a webservice page now so I tried to grab it from a aspx.cs page and it hasn't worked.
///Aspx page:
$.ajax({
type: "POST",
url: "~/Alerts/GetAlerts",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var data = eval("(" + msg.d + ")");
jQuery.each(data, function (rec) {
AlertList[AlertList.length] = new objAlert(this.id, this.title, this.details, JSONDateSerializationFix(this.startdate), JSONDateSerializationFix(this.enddate));
UpdateDisplayList();
})
},
error: function (msg) {
alert("BRAD" + msg);
}
The issue is that the Alerts page in "URL /Alerts/GetAlerts" is Alerts.aspx.cs. I can't figure out if I can use this ajax command to call a method in a aspx.cs page.
//Code behind page aspx.cs
[WebMethod]
//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetAlerts()
{
List list = AlertInfo.GetTestAlerts();
return new JavaScriptSerializer().Serialize(list);
}
public List GetAlertsList()
{
List list = AlertInfo.GetTestAlerts();
return list; ;
}
So I was hoping that I could load data into an asp control (dataList) and then grab the data
//code behind page
protected void Page_Load(object sender, EventArgs e)
{
dataListAlertList.DataSource = GetAlertsList();
dataListAlertList.DataBind();
}
public static List<AlertInfo> GetTestAlerts()
{
List<AlertInfo> list = new List<AlertInfo>();
list.Add(new AlertInfo("0", "Alert 1 Title", "Alert 1 Detail", "10/10/2010", "10/10/2011"));
list.Add(new AlertInfo("1", "Alert 2 Title", "Alert 2 Detail", "10/10/2010", "10/10/2011"));
return list;
}
//.aspx page
$(document).ready(function () {
var a1 = $("#dataListAlertList").val();
// do fun stuff now.
}
But I keep getting undefined....