我正在研究现有的网络表单应用程序。我有强类型数据列表。现在我在 ASP.NET 网页表单页面中有下拉菜单,所以当用户从下拉列表中选择项目时,ajax 调用 WebMethod 将相应地返回学生列表,现在因为我使用的是静态方法,我无法在代码隐藏中绑定数据源所以我需要做的是以 JSON 格式发送数据并与 ASP.NET Webform 网格“StudentFilterList_Grid”绑定。
代码隐藏网络方法
[WebMethod]
public static studentList FilterStudentListInRelationToStaff(string GivenStaffID, string SelectFilterOption)
{
StudentList customStudentFilterList = new StudentList();
customStudentFilterList= StaffRelay.GetStudentsForRelationship(Convert.ToInt32(GivenStaffID));
return customStudentFilterList;
}
ASP.NET 网格
<cc0:Grid ID="StudentFilterList_Grid" runat="server" FolderStyle="~/Styles/Grid" AutoGenerateColumns="false"
Width="100%" PageSizeOptions="5,10,20,50,100,-1" AllowFiltering="true" FilterType="ProgrammaticOnly"
AllowAddingRecords="false" PageSize="-1">
<Columns>
<cc0:Column DataField="ID" HeaderText="ID" Visible="false" />
<cc0:Column DataField="RelationshipID" HeaderText="RelationshipID" Visible="false" />
<cc0:Column DataField="StudentID" Width="10%" HeaderText="ID" SortExpression="StudentID" Visible="false" />
<cc0:Column DataField="RelationshipDateStart" Width="20%" HeaderText="From" DataFormatString="{0:dd MMMM yyyy}" DataFormatString_GroupHeader="{0:dd MMMM yyyy}" SortExpression="RelationshipDateStart" />
<cc0:Column DataField="RelationshipDateEnd" DataFormatString="{0:dd MMMM yyyy}" DataFormatString_GroupHeader="{0:dd MMMM yyyy}" Width="20%" HeaderText="To" SortExpression="RelationshipDateEnd" />
<cc0:Column DataField="RelationshipStaffStatus" Visible="false" HeaderText="Status" SortExpression="RelationshipStaffStatus" />
<cc0:Column DataField="RelationshipLocation" HeaderText="Locality" />
</Columns>
</cc0:Grid>
jQuery Ajax 函数
$("#ctl00_ContentArea_ddLStudentFilterList").change(function () {
var selectedValue = $("#<%=ddLStudentFilterList.ClientID%> option:selected").val();
if(selectedValue!=null)
{
var SelectStaffID = "37";
$.ajax({
url: 'TutorForm.aspx/FilterStudentListInRelationToStaff',
type: "POST",
data: JSON.stringify({ GivenStaffID: SelectStaffID, SelectFilterOption: selectedValue }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("success" + response.d[0].RelationshipID);
for (var index = 0; index <= response.d.length - 1; index++) {
var indexStudentFullName = response.d[index].Forenames + "-" + response.d[index].Surnames;
// alert(response.d[index].ID + " RelationshipID " + RelationshipID + " StudentID " + StudentID + " RelationshipDateStart " + RelationshipDateStart + " RelationshipDateEnd" + RelationshipDateEnd + " RelationshipStaffStatus " + RelationshipStaffStatus + " StudentFullName " + indexStudentFullName);
????????????????????????????????
}
},
failure: function (response) {
alert(response.d);
}
}).done(function (response) {
});
}
});
上面的代码确认我在 json 中获取数据,所以上面的警报确实给了我答案 response.d[0].RelationshipID