如果您从aspx.cs 文件中调用特定方法
在aspx页面中使用以下脚本
$('#<%= ddlItemCatogory.ClientID%>').change(function () {
var catId = this.value;
$('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("loading ... ", "0");
$("#phMainContent_ctl00_ddlItem").resetSS();
$.ajax({
type: "POST",
url: "handler/PopulateAjaxData.aspx/GetItem",
data: "{'catId':'" + catId.toString() + "'}",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
if (data.d.length > 0) {
$('#<%= ddlItem.ClientID%>').get(0).options.length = 0;
$('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("Select Item", "0");
$.each(data.d, function (index, item) {
$('#<%= ddlItem.ClientID%>').get(0).options[$('#<%= ddlItem.ClientID%>').get(0).options.length] = new Option(item.Display, item.Value);
});
}
else {
$('#<%= ddlItem.ClientID%>').get(0).options.length = 0;
$('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("No Item found", "0");
}
$("#phMainContent_ctl00_ddlItem").resetSS();
},
error: function () {
alert("Failed to load Item");
}
});
});
PopulateAjaxData.aspx.cs中的代码是
[WebMethod]
public static ArrayList GetItem(string catId)
{
ArrayList list = new ArrayList();
PopulateAjaxData item = new PopulateAjaxData();
List<DataModels.Items> items = item.loadItem(int.Parse(catId));
foreach (DataModels.Items i in items)
{
list.Add(new { Value = i.Id.ToString(), Display = i.Name });
}
return list;
}
如果您调用aspx.cs文件,则使用以下代码...
在 .aspx 页面中使用以下代码
function addToCart() {
var vehicleId = $('#<%= VehicleId.ClientID%>').val();
var parameter = "vehicleId=" + vehicleId;
$.ajax({
type: "POST",
url: "handler/AddToCart.aspx?" + parameter,
success: function (msg) {
if (msg == "1") {
$('#message').html('Vehicle has been added to Cart.');
}
else {
$('#message').html('Vehicle can not be added at this moment.');
}
}
});
}
aspx.cs加载事件使用下面的代码
protected void Page_Load(object sender, EventArgs e)
{
string vehicleId = Request.Params["vehicleId"] ?? "";
string productId = Request.Params["productId"] ?? "";
string message = string.Empty;
int CartId = 0;
if (Request.Cookies["CartId"] == null)
{
if (CartId == 0)
{
DataModels.Cart cart = new DataModels.Cart();
cart.CreatedAt = DateTime.Now;
try
{
cart = Service.Create(cart);
if (cart.Id > 0)
{
HttpCookie objCookie = new HttpCookie("CartId");
objCookie.Value = cart.Id.ToString();
objCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(objCookie);
CartId = cart.Id;
}
}
catch (Exception ex)
{
message = ex.Message;
Response.Write("-1");
}
}
}
else
{
CartId = int.Parse(Request.Cookies["CartId"].Value);
}
DataModels.CartItem cv_new = new DataModels.CartItem();
if (CartId > 0 && !(string.IsNullOrEmpty(vehicleId)))
{
DataModels.CartItem cv = Service.ReadCartByVehicleId(CartId, int.Parse(vehicleId));
if (cv == null)
{
cv_new.Cart = Service.ReadCart(CartId);
cv_new.Vehicle = Service.ReadVehicle(int.Parse(vehicleId));
cv_new.Product = null;
cv_new = Service.Create(cv_new);
}
}
else if (CartId > 0 && !(string.IsNullOrEmpty(productId)))
{
DataModels.CartItem cv = Service.ReadCartByProductId(CartId, int.Parse(productId));
if (cv == null)
{
cv_new.Cart = Service.ReadCart(CartId);
cv_new.Vehicle = null;
cv_new.Product = Service.ReadProduct(int.Parse(productId));
cv_new = Service.Create(cv_new);
}
}
Response.Write("1");
}