Ajax在.NET中與Server控制項的互動

來源:互聯網
上載者:User
ajax|server|互動|控制項

利用ajax組件怎樣能讓它跟server控制項互動呢,例如輸出一個列表,就只有用js一條一條html的輸出嗎?當然不是,現在可以與datagrid互動。

註:ajax的.net組件可以到此網下載,本文用的是for .net 1.1版本的。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  DataSetds = 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="http://schemas.microsoft.com/intellisense/ie5"> 

<script language="javascript" src="default.js">
</script> 

</HEAD>

<body >

<form id="Form1" method="post" runat="server">

<INPUT type="text" id="AddTextBox" maxlength="10">
<INPUT type="button" value="添加" 


<table>

<tr>

 <td id="UCtd"></td>

</tr>

 </table>

</form>

</body>

</HTML>

這樣,就能使用datagrid來輸出表格了。總體思路其實還是比較簡單的。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.