首页 新闻 会员 周边 捐助

简单的AJAX局部刷新gridview

0
悬赏园豆:5 [已关闭问题] 关闭于 2015-01-20 15:36

基础较差,不太理解ajax的实现。目前情况如下:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ajaxSearch.aspx.cs" Inherits="ajax.ajaxSearch" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="/jquery.js"></script>
    <script type="text/javascript">
        function search() {
            var txtKey = $("#txtKey").val();
            $.ajax({
                type: "Post",
                url: "ajaxSearch.aspx/search",
                data: "{txtKey:'" + txtKey + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                },
                error: function (err) {
                }
            })
        }        
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtKey" />
        <input type="button" id ="btn" onclick="search()" value="查询"/>        
    </div>
        <span>--------------------------------------------</span>
    <div>
        <asp:GridView ID="gvSearch" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="PrizedName" HeaderText="奖品名称" />
                <asp:BoundField DataField="PrizedNumber"  HeaderText="奖品数量"/>
            </Columns>
        </asp:GridView>  
    </div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data.Common;
using log4net;
using System.Configuration;

namespace ajax
{
    public partial class ajaxSearch : System.Web.UI.Page
    {
        // log日志
        private static ILog LOG = LogManager.GetLogger(typeof(ajaxSearch));

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public DataTable CreateData(string SQLText)
        {
            string cs = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
            DataSet ds = new DataSet();
            SqlConnection connection = new SqlConnection(cs);
            try
            {
                // 读取配置文件
                SqlCommand command = new SqlCommand(SQLText, connection);
                SqlDataAdapter mDataAdapter = new SqlDataAdapter(SQLText, connection);
                connection.Open();
                mDataAdapter.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception e)
            {
                LOG.Error(e.Message);
                return null;
            }
            finally
            {
                connection.Close();
            }
        }

        public void bind(string txtKey)
        {
            DataTable dt = new DataTable();

            if (string.IsNullOrEmpty(txtKey))
            {
                dt = CreateData("select PrizedName ,PrizedNumber from HD_Prizes order by PrizedName");
            }
            else
            {
                dt = CreateData("select PrizedName ,PrizedNumber from HD_Prizes where PrizedName like '%" + txtKey + "%' order by PrizedName");
            }
            gvSearch.DataSource = dt;
            gvSearch.DataBind();
        }
        
        [WebMethod]
        public static int search(string txtKey)
        {
            ajaxSearch bll = new ajaxSearch();
            try
            {
                return 0;
            }
            catch (Exception ex)
            {
                return 1;
            }
        } 
    }
}

在写AJAX方法的时候静态调用动态方法需要引用

我就用了ajaxSearch bll = new ajaxSearch();把后台查询DB的CS给引过来了,

但是这样就找不到gvSearch这个前台控件了,大概原因是因为我把对应的后台跟重新实例化了, 但是我想实现这种动态查询刷新该如何解决。

问题补充:

HELP

15904915314的主页 15904915314 | 菜鸟二级 | 园豆:215
提问于:2015-01-20 10:37
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册