首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

jquery ajax 异步刷新gridview的有关问题

2014-04-24 
jquery ajax异步刷新gridview的问题这是前台代码

jquery ajax 异步刷新gridview的问题
这是前台代码

<%@ 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" src="js/jquery.js"></script>
    <script type="text/javascript">
    function UpdateGrid(args) 
    {
        args = args + "$" + window.document.getElementById('ddl').value;
        <%= ClientScript.GetCallbackEventReference(this,"args", "ShowResult", null) %>
    } 
    function ShowResult(eventArgument,context)
    {
        window.document.getElementById('Gridview').innerHTML = eventArgument;
    }
    $(function(){
        $("#button1").click(
            function(){
            $.ajax({
                 type:'POST',
                 url:'Default.aspx',
                 data:{action:'action'},
                 success: savesuccesscallbace
            })
            }
            )
            });
     //保存成功后的回调函数
    function savesuccesscallbace(r)
    {
        
    }
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>Test Async Gridview</h3>
        <hr />
    </div>
    <input id="input1" type="text"/>
    <input type="button" id="button1" value="提交"/>
    <div id="Gridview">
        <asp:GridView EnableViewState="false" runat="server" id="_grid" OnRowDataBound="_grid_RowDataBound" AllowPaging="True" ></asp:GridView>
        <br/>
    </div> Change page length to — 
    <asp:DropDownList ID="ddl" runat="server">
    </asp:DropDownList> 
    </form>
</body>
</html>

这是后台代码
using System;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
    private string result;
    string StrAction = "";
    protected void Page_Load(object sender, EventArgs e)
    {
         StrAction = Request["action"];
         if (StrAction == "action")
         {
             try
             {
                 this._grid.DataSource = _sampleData;
                 this._grid.DataBind();
                 this.ddl.Items.Add("10");
                 this.ddl.Items.Add("20");
                 this.ddl.Items.Add("30");
                 this.ddl.Attributes.Add("onchange", "javascript:UpdateGrid('changePageLength$' + this.value);");


                 Response.Clear();
                 Response.ContentType = "application/text";
                 Response.Write("yes");
                 Response.End();
                 int n=this._grid.Rows.Count;
             }
             catch(Exception )
             {
                 Response.Clear();
                 Response.ContentType = "application/text";
                 Response.Write("no");
                 Response.End();
             }
         }
    }public DataTable _sampleData
    {
        get 
        {
            DataTable dt = (DataTable)ViewState["DataTable"];
            if(dt == null)
            {
                ...//绑定的数据
            }
            return dt;
        } 
    }private void sortGrid(string Argument , string pageLength) 
    { 
        DataView dv = _sampleData.DefaultView;
        result = "";
        dv.Sort = Argument;
        _grid.DataSource = dv;
        _grid.PageSize = Convert.ToInt16(pageLength);
        _grid.DataBind();
        renderGrid(_grid);
    } 
    private void changePage(string Argument , string pageLength) 
    {
        result = "";
        _grid.DataSource = _sampleData;
        _grid.PageSize = Convert.ToInt16(pageLength);
        _grid.PageIndex = Convert.ToInt16(Argument);
        _grid.DataBind();
        renderGrid(_grid);
    } 
    private void changePageLength(string Argument, string pageLength) 
    {
        result = "";
        _grid.DataSource = _sampleData;
        _grid.PageSize = Convert.ToInt16(Argument);
        _grid.DataBind();
        renderGrid(_grid);
        //pageLength is not used
    }
    private void renderGrid(GridView _grid)
    {
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            _grid.RenderControl(htw);
            htw.Flush();
            result = sw.ToString();
        }
    }
    public string GetCallbackResult()
    {
        return result;
    }
    public void RaiseCallbackEvent(string eventArgument) 
    {
        string[] args = eventArgument.Split('$');
        if (args[0] == "sort") { sortGrid(args[1], args[2]); }


        else if (args[0] == "changePage") { changePage(args[1], args[2]); }
        else if (args[0] == "changePageLength") { changePageLength(args[1], args[2]); }
    } 
    protected void _grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].Text = string.Format("{0}<img alt="Ascending"" src="Images/up.jpg" onclick="UpdateGrid('sort${0} Asc')"; /><img alt="Descending" src="Images/down.jpg" onclick="UpdateGrid('sort${0} desc')"; />", e.Row.Cells[i].Text);
            }
        }
        else if (e.Row.RowType == DataControlRowType.Pager)
        {
            GridView gdv = (GridView)sender;
            int _pageCount = gdv.PageCount;
            e.Row.Cells[0].Text = "";
            for (int i = 0; i < _pageCount; i++)
            {
                HyperLink hyp = new HyperLink();
                hyp.Text = i.ToString() + "&nbsp;";
                hyp.Attributes.Add("href", "javascript:UpdateGrid('changePage$" + i + "');");
                e.Row.Cells[0].Controls.Add(hyp);
                Label l = new Label();
                l.Text = "&nbsp;";
                e.Row.Cells[0].Controls.Add(l);
                hyp = null;
            }
        } 
    }
}


我把后台代码if (StrAction == "action")去掉打开网页就会显示这个gridview,可是加了这个之后点击按钮什么都不显示,我怀疑就是jquery的ajax的关系,请问要怎么修改啊! GridView jQuery?Ajax
[解决办法]
get GET GET!

热点排行