欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ajax.net对数据库的插入实例

程序员文章站 2024-03-01 22:34:34
default.aspx复制代码 代码如下:<%@ page language="c#" autoeventwireup="true"  codefile=...

default.aspx

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true"  codefile="default.aspx.cs" inherits="_default" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
       function data_insert()
       {
          var xmlhttp;
          if (window.activexobject) {
              xmlhttp = new activexobject("microsoft.xmlhttp");
          }
          else if (window.xmlhttprequest) {
              xmlhttp = new xmlhttprequest();
          }
          else {
              alert("您的浏览器不支持ajax,请使用其他!"); 
          }
          xmlhttp.onreadystatechange=function()
          {
               if (xmlhttp.readystate==4 && xmlhttp.status==200)
               {
                    alert("yeah!");
                }
          }
          var no, name;
          no = document.getelementbyid("textbox1").value;
          name = document.getelementbyid("textbox2").value;
          var url = "default2.aspx?no=" + no + "&name=" + name;
          xmlhttp.open("get", url, true);
          xmlhttp.send(null);
       }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        号码:<asp:textbox id="textbox1" runat="server"></asp:textbox><br />
        姓名:
        <asp:textbox id="textbox2" runat="server"></asp:textbox><br />
         <input type="button" value="button" onclick="data_insert()"/>
         </div>
    </form>
</body>
</html>


default.aspx.cs
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        //button1.attributes.add("onclick", "date_insert()");
    }

}

default2.aspx

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:sqldatasource id="sqldatasource1" runat="server"
            connectionstring="<%$ connectionstrings:connectionstring %>"
            deletecommand="delete from [tst] where [no] = @no"
            insertcommand="insert into [tst] ([no], [name]) values (@no, @name)"
            selectcommand="select * from [tst]"
            updatecommand="update [tst] set [name] = @name where [no] = @no">
            <deleteparameters>
                <asp:parameter name="no" type="int32" />
            </deleteparameters>
            <insertparameters>
                <asp:parameter name="no" type="int32" />
                <asp:parameter name="name" type="string" />
            </insertparameters>
            <updateparameters>
                <asp:parameter name="name" type="string" />
                <asp:parameter name="no" type="int32" />
            </updateparameters>
        </asp:sqldatasource>

        <br />

    </div>
    </form>
</body>
</html>

default2.aspx.cs

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class default2 : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        sqldatasource1.insertparameters["no"].defaultvalue = //www.jb51.net/wghost/archive/2011/12/01/request.querystring["no"].tostring();
        sqldatasource1.insertparameters["name"].defaultvalue = //www.jb51.net/wghost/archive/2011/12/01/request.querystring["name"].tostring();
        sqldatasource1.insert();
    }

}