通过ajax使用ado.net插入数据

时间:2015-04-24 11:50:01

标签: asp.net ajax

我在找到问题时遇到了麻烦。我通过ado.net插入产品记录,工作正常。现在我尝试了ajax以更好的方式做到这一点。但我遇到了麻烦,因为当我通过ajax点击按钮时,代码永远不会运行打算运行的功能。所以需要你的帮助。谢谢。这是代码:

//这是我的home.aspx页面      

   $(document).ready(function () {

       $("#submit").click(function () {


           alert("you clicked");

           $.ajax({

               type: "POST",
               contentType: "application/json", 
               url: "home.aspx/insert",
               dataType: "json",
              // data: "{'Name':'" + document.getElementById('name').value + "'}",
               data:"{}",
                //async: false,
                success: function (response) {
                   alert("User has been added successfully.");
                   window.location.reload();

               }

           });




       })


   });

</script>

//////////////////////////////////

 <form id="form1" runat="server">

<div>
      <table>

          <tr>

              <td><label>Product Name</label></td>
              <td> <asp:TextBox  runat="server" id="name"></asp:TextBox></td>
          </tr>

          <tr>
              <td><label>Product Code</label></td>
              <td><asp:TextBox  runat="server" id="code"></asp:TextBox></td>
          </tr>

           <tr>

              <td><label>Product Category</label></td>
              <td><asp:DropDownList ID="DropDownList1"  runat="server">
                  <asp:ListItem>Gadgets</asp:ListItem>
                  <asp:ListItem>Vehicles</asp:ListItem>
                  <asp:ListItem>Garments</asp:ListItem>
                  <asp:ListItem>Eatables</asp:ListItem>
                  <asp:ListItem>Furniture</asp:ListItem>
                  </asp:DropDownList></td>


           </tr>

            <tr>

              <td><label>Expiry date</label></td>
              <td><asp:TextBox  runat="server" TextMode="Date" id="dt"></asp:TextBox></td>
          </tr>


           <tr>
              <td><label>Product price</label></td>
              <td><asp:TextBox  runat="server" id="price"></asp:TextBox></td>
          </tr>

          <tr>
              <td><label>Product Image</label></td>
              <td> <asp:FileUpload ID="file_image"   runat="server" /></td>
              <td> </td>
              <td><asp:Label runat="server" id="showtext"></asp:Label></td>
          </tr>

         <tr></tr>
         <tr></tr>
          <tr>
             <td></td>
              <%--<asp:Button runat="server" OnClick="submit" Text="Submit" BackColor="White" Width="100px" />--%>

              <td> <input type="button"  style=" width:100px;" value="Submit" id="submit" /> </td>

          </tr>
      </table>
</div>
    <asp:Label ID="res" runat="server"></asp:Label>
</form>

///////////////////////////////////////////////

//这是我的home.aspx.cs页面:

 public void insert()
    {
        if (file_image.HasFile)
        {
            string ext = System.IO.Path.GetExtension(file_image.FileName);
            if (!ext.Equals(".jpg"))
            {

                showtext.Text = "Please select correct  file format i.e jpg or jpeg";
                showtext.ForeColor = System.Drawing.Color.Purple;

            }
            else
            {
                file_image.SaveAs(Server.MapPath("~/Uploads/" + file_image.FileName));
                showtext.Text = "File uploaded";
                showtext.ForeColor = System.Drawing.Color.Green;






                productclass pc = new productclass();
                pc.Pr_name = name.Text;
                pc.Code = code.Text;
                pc.Category = DropDownList1.SelectedValue;
                pc.Expiry = Convert.ToDateTime(dt.Text);
                pc.Price = Convert.ToInt32(price.Text);
                pc.Pr_image = file_image.FileName;

                dalinsert di = new dalinsert();
                bool flag = di.insert(pc);

                if (flag.Equals(true))
                {

                    res.Text = "data inserted";
                    //Thread.Sleep(10000);
                   // Response.Redirect("home.aspx");

                }

                else
                {

                    res.Text = "issue in inserting data";

                }        






            }



        }


        else
        {

            showtext.Text = "Please select file";
            showtext.ForeColor = System.Drawing.Color.Red;
        }

    }

0 个答案:

没有答案