获得服务器控件生成的HTML的方法,NET中与Server控件的交互C
分类:微服架构

注:ajax的.net组件能够到此网下载,笔者用的是for .net 1.1本子的。
      
  1. 在援引中丰裕援用Ajax.dll。(这一个很废话)

上边以DataSet为例子,代码摘自jasmineou的ajax在.net中与server控件的相互影响

 

AJAXLoadProgressForm.aspx: 复制代码 代码如下:

  2.在web.config中创建HttpHandler(那一个本来是在system.web串里的)

图片 1DataGrid dg = new DataGrid();
图片 2dg.DataSource = ds.Tables["db"];
图片 3dg.DataBind();
图片 4
图片 5//实例化三个HtmlTextWriter的类
图片 6System.Text.StringBuilder strb = new System.Text.StringBuilder();
图片 7System.IO.StringWriter sw = new System.IO.StringWriter( strb );
图片 8System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter( sw );
图片 9
图片 10//实施控件的render并出口到HtmlTextWriter里
图片 11dg.RenderControl( htw );
图片 12
图片 13string s = strb.ToString();
图片 14

图片 15图片 16顾客端代码

GetGridViewByConditionForm.aspx: 复制代码 代码如下: protected void Page_Load(object sender, EventArgs e) { if (Request["id"] != null) { SqlConnection conn=null; SqlCommand cmd = null; SqlDataAdapter adapter = null; try { conn = new SqlConnection(); conn.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; conn.Open(); cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; String cmdStr = "select * from dbo.Customers"; if (Request["id"].ToString { cmdStr += " where CustomerID= '" + Request["id"].ToString() + "'"; } cmd.CommandText = cmdStr; adapter = new SqlDataAdapter; DataSet ds = new DataSet; this.gvData.DataSource = ds; this.gvData.DataBind(); } catch { Response.Write; Response.Flush; } finally { if { adapter.Dispose { cmd.Dispose(); } if && (conn.State == ConnectionState.Open)) { conn.Close(); } } } }

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

 1 <html xmlns="" >
 2 <head runat="server">
 3   <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
 4     <title>AjaxPro两级联DataTable</title>
 5       <meta name="Author" content="Geovin Du 塗聚文"/>
 6  <meta name="Keywords" content="Geovin Du 塗聚文"/>
 7   <meta name="Description" content="Geovin Du 塗聚文"/>
 8 
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12        <script language="javascript" type="text/javascript">
13     function GetCity()
14     {
15         var Provinceid = document.getElementById("ProvinceList");
16         geovindu.CityBind(Provinceid.value,CityCallBack);
17     }
18     
19     
20     function CityCallBack(response)
21     {
22         if(response.value !=null)
23         {
24         var city = document.getElementById("CityList");
25         var dt = response.value;
26         city.length = 0;
27         city.options.add(new Option("请选择","-1"));
28             for(var i=0;i<dt.Rows.length;i++)
29             {
30                 var city_text = dt.Rows[i]["city"];
31                 var city_value = dt.Rows[i]["cityID"];
32                 city.options.add(new Option(city_text,city_value));     
33             }   
34         }
35     }
36     </script>
37     <div>
38        <asp:DropDownList ID="ProvinceList" runat="server" Width="120px">  
39         </asp:DropDownList>
40          <asp:DropDownList ID="CityList" runat="server" Width="120px">
41             </asp:DropDownList>
42 
43     </div>
44     </form>
45 </body>
46 </html>

  3.在Global的Application_Start里丰盛个设置

 

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

 

  4.新建二个类德姆oMethods,那个类里面提供了翻新数据库和输出列表的点子。其实根本思量正是赢得控件运营后生成的html,然后输出。

图片 17图片 18CS代码

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 }

  1        /// <summary>
  2         /// 加载页面
  3         /// 涂聚文 2010-08-27
  4         /// </summary>
  5         /// <param name="sender"></param>
  6         /// <param name="e"></param>
  7         protected void Page_Load(object sender, EventArgs e)
  8         {
  9             AjaxPro.Utility.RegisterTypeForAjax(typeof(WebForm2),this.Page);
 10 
 11             if (!IsPostBack)
 12             {
 13                 ProvinceBind();
 14                 BindToCity();
 15                 ProvinceList.Attributes.Add("onchange", "GetCity()");
 16 
 17             }
 18 
 19         }
 20         /// <summary>
 21         /// 绑定市级数据
 22         /// </summary>
 23         public void ProvinceBind()
 24         {
 25 
 26             string SqlConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 27             SqlConnection con = new SqlConnection(SqlConnectionString);
 28             con.Open();
 29             DataSet dsCourtesy = new DataSet();
 30             string sSQL = "select * from lc_province";
 31             SqlDataAdapter daCourtesy = new SqlDataAdapter(sSQL, con);  
 32             daCourtesy.Fill(dsCourtesy, "lc_province");
 33             ProvinceList.DataSource = dsCourtesy.Tables["lc_province"].DefaultView;
 34             ProvinceList.DataTextField = "province";
 35             ProvinceList.DataValueField = "provinceID";
 36             ProvinceList.DataBind();
 37             daCourtesy.Dispose();
 38             dsCourtesy.Tables.Clear();
 39             con.Close();
 40 
 41         }
 42         /// <summary>
 43         /// 绑定市级数据
 44         /// </summary>
 45         private void BindToCity()
 46         {
 47 
 48             string SqlConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 49             SqlConnection con = new SqlConnection(SqlConnectionString);
 50             con.Open();
 51             DataSet dsCourtesy = new DataSet();
 52             string sSQL = "select * from lc_city where provinceid=" + this.ProvinceList.SelectedValue;
 53             SqlDataAdapter daCourtesy = new SqlDataAdapter(sSQL, con);
 54             daCourtesy.Fill(dsCourtesy, "lc_city");
 55             CityList.DataSource = dsCourtesy.Tables["lc_city"].DefaultView;
 56             CityList.DataTextField = "city";
 57             CityList.DataValueField = "cityID";
 58             CityList.DataBind();
 59             daCourtesy.Dispose();
 60             dsCourtesy.Tables.Clear();
 61             con.Close();
 62         }
 63         [AjaxPro.AjaxMethod]//ajaxPro注明是办法绑定省级数据
 64         public DataTable CityBind(string provinceid)
 65         {
 66             Hashtable ht = new Hashtable();
 67             string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 68             SqlConnection conn = new SqlConnection(ConnectionString);
 69             conn.Open();
 70 
 71             string strsql = "select * from lc_city where provinceid=" + provinceid + "";//city", "cityID
 72             SqlCommand cmd = new SqlCommand(strsql, conn);
 73             cmd.CommandText = strsql;
 74             SqlDataAdapter da = new SqlDataAdapter();
 75             da.SelectCommand = cmd;
 76             DataTable dt = new DataTable();
 77             da.Fill(dt);
 78 
 79             return dt;
 80 
 81 
 82         }
 83         //[AjaxPro.AjaxMethod]//申明是DataSet ajaxPro方法
 84         //public DataSet CityBind(string provinceid)
 85         //{
 86         //    Hashtable ht = new Hashtable();
 87         //    string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
 88         //    SqlConnection conn = new SqlConnection(ConnectionString);
 89         //    conn.Open();
 90 
 91         //    string strsql = "select * from lc_city where provinceid=" + provinceid;//city", "cityID
 92         //    SqlCommand cmd = new SqlCommand(strsql, conn);
 93         //    cmd.CommandText = strsql;
 94         //    SqlDataAdapter da = new SqlDataAdapter();
 95         //    da.SelectCommand = cmd;
 96         //    DataSet dt = new DataSet();
 97         //    da.Fill(dt);
 98 
 99         //    return dt;
100         //}
101         #region DataSet客户端代码
102 
103         //<script type="text/javascript">
104             //function GetCitys()  
105             //{  
106             //  var pro =document.getElementById("ddlProvince");  
107             //  //命名空间.类名.
108             //  AjaxProDemo.WebForm1.getCityListDataSet(pro.value, getCityListDataSet_CallBack);  
109                
110             //}  
111             //function getCityListDataSet_CallBack(response)  
112             //{  
113             //      if (response.value != null)  
114             //      {   
115             //          var ds = response.value;
116             //          var ddlc=document.getElementById("ddlCity");
117             //          ddlc.length=0;
118             //          ddlc.options.add(new Option("请选择","-1")); 
119             //              for(var i=0; i <ds.Tables[0].Rows.length; i++)  
120             //              {  
121             //              var text=ds.Tables[0].Rows[i].city;  
122             //              var val=ds.Tables[0].Rows[i].cityID;  
123             //              ddlc.options.add(new Option(text,val));  
124             //              }  
125              
126             //       }  
127             //}
128              //</script>
129         #endregion 

  5.然后再建三个default.js文件,用作寄存 js方法

 

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

Ajax.NET Professional  最新学习和下载:

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_defaultClients cript" content="Javas cript">
 <meta name="vs_targetSchema" content="
<s cript language="javas cript" src="default.js"></s _fcksavedurl=""default.js"></s" cript>
</HEAD>
<body onload="LoadGrid()">
<form id="Form1" method="post" runat="server">
 <INPUT type="text" id="AddTextBox" maxlength="10"><INPUT type="button" value="添加" onclick="javas cript:AddAjax(form.AddTextBox.value);">
 <table>
  <tr>
   <td id="UCtd"></td>
  </tr>
 </table>
</form>
</body>
</HTML>

<> <>

本文由10bet手机官网发布于微服架构,转载请注明出处:获得服务器控件生成的HTML的方法,NET中与Server控件的交互C

上一篇:学习笔记之七 下一篇:socket通信较完善方案,UMD手机电子书的读取和制作源码
猜你喜欢
热门排行
精彩图文