jQuery $ Ajax将数据从webform发布到ASP.NET中的代码隐藏方法

时间:2015-09-22 10:32:25

标签: webforms asp.net-3.5 jquery-ajaxq

我正在尝试将数据从webform传递到代码隐藏方法并在webform中获取值,然后打印它。我最初测试以下代码只是简单地发布请求到方法,获取字符串并在页面中打印并且它有效,但在尝试将数据发布回方法时出现问题

$(document).ready(function () {

$(".AddStaffToRoleLink").on("click", function () {

           var selectedStaffID = $(this).attr("id");

           alert("this is " + selectedStaffID);

           $.ajax({

               type: "POST",
               url: "AddUserInRole.aspx/AddRoleForSelectStaff",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               data: { selectedStaffID: selectedStaffID },
               success: function (response) {
                   $("#Content").text(response.d);
               },
               failure: function (response) {
                   alert(response.d);
               }
           });
       });

});

背后的代码

   [WebMethod]
    public static string AddRoleForSelectStaff(string selectedStaffID)
    {
        return "This string is from Code behind  " + selectedStaffID;
    }

3 个答案:

答案 0 :(得分:6)

这里是将sigle数据发布到方法背后的webform代码的方法......

  $(document).ready(function () {

 $(".AddStaffToRoleLink").on("click", function () {

           var selectedStaffID = $(this).attr("id");

           alert("this is " + selectedStaffID);

           $.ajax({
               url: 'AddUserInRole.aspx/AddRoleForSelectStaff',
               type: "POST",
               data: "{'GivenStaffID':'" + selectedStaffID +"'}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (response) {
                   $("#Content").text(response.d);
               },
               failure: function (response) {
                   alert(response.d);
               }
           }).done(function (response) {
               alert("done "+response );
           });
       });
   });

Code Behind方法

 [WebMethod]
    public static string AddRoleForSelectStaff(string GivenStaffID)
    {
        var staffID = Convert.ToInt32(GivenStaffID);

        return "This string is from Code behind  " + GivenStaffID;
    }

答案 1 :(得分:1)

internal static string BooleanTemplate(HtmlHelper html)
{
  bool? nullable1 = new bool?();
  if (html.ViewContext.ViewData.Model != null)
    nullable1 = new bool?(Convert.ToBoolean(html.ViewContext.ViewData.Model, (IFormatProvider) CultureInfo.InvariantCulture));
  if (html.ViewContext.ViewData.ModelMetadata.IsNullableValueType)
    return DefaultEditorTemplates.BooleanTemplateDropDownList(html, nullable1);
  HtmlHelper html1 = html;
  bool? nullable2 = nullable1;
  int num = nullable2.HasValue ? (nullable2.GetValueOrDefault() ? 1 : 0) : 0;
  return DefaultEditorTemplates.BooleanTemplateCheckbox(html1, num != 0);
}

private static string BooleanTemplateCheckbox(HtmlHelper html, bool value)
{
  return InputExtensions.CheckBox(html, string.Empty, value, DefaultEditorTemplates.CreateHtmlAttributes(html, "check-box", (string) null)).ToHtmlString();
}

private static string BooleanTemplateDropDownList(HtmlHelper html, bool? value)
{
  return SelectExtensions.DropDownList(html, string.Empty, (IEnumerable<SelectListItem>) DefaultEditorTemplates.TriStateValues(value), DefaultEditorTemplates.CreateHtmlAttributes(html, "list-box tri-state", (string) null)).ToHtmlString();
}

答案 2 :(得分:0)

代码:

protected void Page_Load(object sender, EventArgs e)
    { 
HttpContext.Current.Request.InputStream.Position = 0;
        string jsonString = "";
        using (StreamReader inputStream = new     StreamReader(this.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
        JSONRequest masterDetails =   Newtonsoft.Json.JsonConvert.DeserializeObject<JSONRequest>(jsonString);
 }

public class JSONRequest
{
    public long Org { get; set; }
    public long Dept { get; set; }
    public long Desg{ get; set; }
    public long Grp { get; set; }
    public long SubGrp { get; set; }
    public long Loc { get; set; }
    public long Cat { get; set; }
    public string Prv { get; set; }
    public long CustAttrId { get; set; }
    public string CustAttrVal { get; set; }
    public string Status { get; set; }
}

function GetEmployee()
{
    var jsonObj = {"Org":0,"Dept":0,"Desg":0,"Grp":0,"SubGrp":0,"Loc":0,"Prv":'CanViewEmployee',"CustAttrI    d":0,CustAttrVal:"","Status":'1' };
    $.ajax({
        url: 'EmployeeSearchControl.aspx/populateJsonResult',
        datatype: 'json',
        data:JSON.stringify(jsonObj),
        method: 'Post',
        success: function (data) {
            if(data != null || data != "")
            {
                for(var i=0;i<data.length;i++)
                {
                    addEmployeeToGrid(data[i]);
                }
            }
        }
    })
}