Ajax.net中与服务器控件的交互使用

前两天在网上下了个ajax组件体验了一下,感觉很不错。但后来开始想怎样能让它跟server控件交互呢,例如我上输出一个列表,就只有用js一条一条html的输出吗?不!!现在我说说怎样与 datagrid交互。

  注:ajax.net的组件可以到此网下载,我用的是for .net 1.1版本的。ajax.schwarz-interactive.de/">ajax.schwarz-interactive.de/" target=_blank>http://ajax.schwarz-interactive.de/

  1. 在引用中添加引用Ajax.dll。(这个很废话)

  2.在web.config中建立HttpHandler(这个当然是在system.web串里的)


<httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers> 

  3.在Global的Application_Start里加上个设置

        protected void Application_Start(Object sender, EventArgs e)
       {
            Ajax.Utility.HandlerPath = "ajax";
        }

  4.新建一个类DemoMethods,这个类里面提供了更新数据库和输出列表的方法。其实主要思想就是获得控件运行后生成的html,然后输出。

 1        [Ajax.AjaxMethod]
 2        public int AddAjaxTable(string name)
 3        {
 4            //输入一个字符串,然后更新
 5            SqlConnection conn = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["connectionString"] );
 6            SqlCommand cmd = new SqlCommand("insert into ajaxTable(name) values('"+name+"')", conn);
 7            cmd.Connection.Open();
 8            int result = cmd.ExecuteNonQuery();
 9            conn.Dispose();
10            cmd.Dispose();
11            return result;
12        }
13
14        [Ajax.AjaxMethod]
15        public string GetAjaxTable()
16        {
17            //这个方法就是拿到datagrid生成出来的html
18            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connectionString"]);
19            SqlCommand cmd = new SqlCommand("select * from ajaxTable order by id", conn);
20            SqlDataAdapter ap = new SqlDataAdapter( cmd );
21            DataSet ds = new DataSet();
22            ap.SelectCommand.Connection.Open();
23            ap.Fill( ds, "db" );
24
25            conn.Dispose();
26            cmd.Dispose();
27
28            //实例化一个datagird类并设置好数据源
29            DataGrid dg = new DataGrid();
30            dg.DataSource = ds.Tables["db"];
31            dg.DataBind();
32
33            //实例化一个HtmlTextWriter的类
34            System.Text.StringBuilder strb = new System.Text.StringBuilder();
35            System.IO.StringWriter sw = new System.IO.StringWriter( strb );
36            System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter( sw );
37
38            //执行控件的render并输出到HtmlTextWriter里
39            dg.RenderControl( htw );
40
41            string s = strb.ToString();
42
43            return s;//最后就是返回这个html啦
44        }

  5.然后再建一个default.js文件,用作存放 js方法 function AddAjax(name)

{
 DemoMethods.AddAjaxTable(name);
 LoadGrid();
}

function LoadGrid()
{
 var cc=document.getElementById("UCtd");
 cc.innerHTML=DemoMethods.GetAjaxTable().value;
}

  6.建一个default.aspx,在pageload事件里面加个注册的东西

         private void Page_Load(object sender, System.EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(AjaxTestPrjLib.DemoMethods));
        }

  7.最后就是default.aspx的html和js了,呵呵

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="AjaxTextPrjWeb._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>default</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="soft.com/intellisense/ie5">soft.com/intellisense/ie5" target=_blank>http://schemas.microsoft.com/intellisense/ie5">
        <script language="javascript" src="default.js"></script>
    </HEAD>
    <body onload="LoadGrid()">
        <form id="Form1" method="post" runat="server">
            <INPUT type="text" id="AddTextBox" maxlength="10"><INPUT type="button" value="添加" onclick="javascript:AddAjax(form.AddTextBox.value);">
            <table>
                <tr>
                    <td id="UCtd"></td>
                </tr>
            </table>
        </form>
    </body>
</HTML>

  这样,就能使用datagrid来输出表格了,呵呵。总体思路其实还是比较简单的,日后有兄弟做出更好的,请贴出来,而我也会继续努力的。谢谢....继续工作去....

你可能感兴趣的:(.net)