asp .net web服务插入

时间:2014-05-23 12:29:26

标签: jquery asp.net ajax web-services

您好我正在尝试使用带有ajax的webservice来获取数据并插入。 要获取数据,我使用它;

  [WebMethod]
    public string MenuInsert(int id)
    {
        try
        {
            using (Models.HaberScriptEntities db = new Models.HaberScriptEntities())
            {





                var res = (from i in db.MasterMenu
                           where
                               i.Id == id
                           select i.MenuName).FirstOrDefault();

                return res;

            }
        }
        catch (Exception)
        {
            return null;
        }
    }

我使用按钮点击ajax来获取它; 这是我的menuList.aspx;

中的脚本
<script type="text/javascript">
    function getUserName() {
        var id = $("#ContentPlaceHolder1_UserId").val();
        $("#ContentPlaceHolder1_Result").delay(200).hide("slow");
        $.ajax({
            dataType: "json",
            type: "POST",
            contentType: "application/json",
            url: "/WebService/MenuServices.asmx/GetUserName",
            data: "{id:'" + id + "'}",
            success: function (data) {
                if (data.d != null) {
                    $("#ContentPlaceHolder1_Result").text(data.d);
                    $("#ContentPlaceHolder1_Result").css("color", "#FF6600");
                }
                else {
                    $("#ContentPlaceHolder1_Result").text("not found");
                    $("ContentPlaceHolder1_Result").css("color", "red");

                }
                $("#ContentPlaceHolder1_Result").delay(200).slideDown("slow");

            },
            error: function () {

            }

        });

        return false;



    }

    $(document).ready(function(){
        $("#ContentPlaceHolder1_Search").click(getUserName);
        $("#ContentPlaceHolder1_Result").css("display", "none");
    });



</script>

<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="page-content-wrapper">
    <div class="page-content">





        <div class="row">
            <div class="col-md-12">
                <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                <h3 class="page-title">
                MFT Haber Scripti V1  <small>Admin Paneli</small>
                </h3>
                <ul class="page-breadcrumb breadcrumb">
                    <li>
                        <i class="fa fa-home"></i>
                        <a href="index.html">Home</a>
                        <i class="fa fa-angle-right"></i>
                    </li>
                    <li>
                        <a href="#">Dashboard</a>
                    </li>
                    <li class="pull-right">

                    </li>
                    <li>
                    </li>
                </ul>
                <!-- END PAGE TITLE & BREADCRUMB-->
            </div>
        </div>
        <!-- END PAGE HEADER-->

        <div class="clearfix">

<form runat="server">
<asp:Label ID="Result" runat="server" Text="Label" Font-Bold="True" Font-Size="X-Large" ForeColor="#FF6600"></asp:Label>
<br />
<asp:TextBox ID="UserId" runat="server"></asp:TextBox>
<br />
<asp:Button Id="Search" runat="server" Text="Button" />

现在我想通过textbox添加到数据库中。我无法将文本框ID发送到MenuService.asmx.cs我该怎么做?请帮忙

 protected void addmebaby_Click(object sender, EventArgs e)
    {
        using (HaberScriptEntities db = new HaberScriptEntities())
        {
            MasterMenu create = new MasterMenu();

            create.MenuName = name.Text.Trim();


            db.MasterMenu.Add(create);
            db.SaveChanges();

         //   Response.AppendHeader("Refresh", 1 + "; #MenuDiv");   

        }

    }

所以这给我添加了babyclick from codebehind我想在网络服务页面

中这样做

1 个答案:

答案 0 :(得分:0)

在var中,如果你获得文本框的值而不是文本框的id,请试试这个

var id = $("#ContentPlaceHolder1_UserId").id;

OR

var id = $("#ContentPlaceHolder1_UserId").attr('id');