zoukankan      html  css  js  c++  java
  • Asp.net--Ajax前后台数据交互

    转自:http://www.cnblogs.com/guolebin7/archive/2011/02/22/1961737.html

    代码由前后台两部分组成:

    前台:(新建一个Default.aspx)

    复制代码
    <head runat="server">
    <title>Ajax,I am coming</title>

    <style type = "text/css">
    *
    { font-family:Tahoma, Arial, Sans-Serif;}
    #helloTitle
    { color:#48f; font-size:1.5em;}
    </style>

    <script type = "text/javascript" src = "prototype.js">/*需加入prototype类库*/
    </script>

    <script type = "text/javascript">
    window.onload
    =function() {
    document.getElementById(
    'helloBtn')
    .onclick
    =function() {
    /*为helloBtn按钮编写一个方法*/
    var name = document.getElementById('helloTxt').value;
    new Ajax.Request(
    "Ajax.aspx?name="+ encodeURI(name), /*新建一个Ajax对象,数据传到Ajax.aspx进行处理*/
    {
    method:
    "get",
    onComplete:
    function(xhr) {
    document.getElementById(
    'helloTitle')
    .innerHTML
    = xhr.responseText;
    }
    }
    )
    }
    }
    </script>
    </head>

    <body>
    <form id="form1" runat="server">
    <div>
    <h1 id = 'helloTitle'>hello,stranger</h1>
    <p>Please introduce yourself by entering your name in the box below</p>
    <input type = "text" size = "24" id = "helloTxt"/>
    &nbsp;
    <input type = "button" id = "helloBtn" value = "Submit"/>
    </div>
    </form>
    </body>
    </html>
    复制代码

    后台:(新建一个Ajax.aspx页面,在Ajax.aspx.cs中加入下面的代码)

    复制代码
    protectedvoid Page_Load(object sender, EventArgs e)
    {
    string a = Request.QueryString["name"]; /*取出name变量的值,个人觉得与Asp.net取Session值类似*/
    SqlConnection conn
    =new SqlConnection("Data Source=X0O4MQIY5N0SXOY;Initial Catalog=kk;Integrated Security=True");
    string sql ="select t_id from tablename where t_name = '"+ a +"'";
    SqlCommand cmd
    =new SqlCommand(sql,conn);
    SqlDataAdapter myda
    =new SqlDataAdapter(cmd);
    DataSet myDs
    =new DataSet();
    myda.Fill(myDs);

    if (myDs.Tables[0].Rows.Count >0)
    {
    Response.Write(
    "true");
    }
    else
    {
    Response.Write(
    "false");
    }
    }
    复制代码

    在ASP.net中利用Ajax技术、表单进行一个前后台的数据交互。

    一个小小的实例,还得继续学习。

    请各位看到这篇博客的人如果有自己的想法,请留下的您宝贵的想法,我现在对在ASP.net中利用Ajax技术还不是很了解,我希望能与更多已经学习过的人交流,一起进步。。。

  • 相关阅读:
    http header Contenttype
    SharePoint 2010 文档库中直接打开文档
    玩转Google开源C++单元测试框架Google Test系列(gtest)(总)
    最近感兴趣的二三事
    最近遭遇的两个VS配置
    环游世界,走遍读过的每一个国家和城镇
    趣题一则:如何快速过桥?
    NASA庆祝地球日:50年地球最精美图片亮相(转载)
    Silverlight,Windows 8应用开发实例教程系列汇总
    Windows 8实例教程系列 数据绑定高级实例
  • 原文地址:https://www.cnblogs.com/cugwx/p/3566435.html
Copyright © 2011-2022 走看看