【C#代码段】ajax从asp后台获取数据

前台获取

 1 $.ajax({

 2             //url: "demo/data/taskTree.txt",

 3             url: "GanttChart.aspx?taskList=1" ,

 4             cache: false,

 5             success: function (text) {

 6                 var data = mini.decode(text);

 7  

 8                 gantt.loadTasks(data);

 9  

10                 gantt.unmask();

11  

12                 //折叠全部

13                 //gantt.collapseAll();

14             }

15         });

 

 


获取标志       
  
 if (Request.QueryString["taskList" ] != null)

            {

                GetTaskList();

            }

 


准备数据 
 1 StringBuilder taskStr = new StringBuilder ( "[");

 2             using (SqlConnection conn = new SqlConnection(connString))

 3             {

 4                 conn.Open();

 5                 string strSQL = @"select * from Tasks order by OrderNumber" ;

 6                 //SqlCommand cmd = new SqlCommand(strSQL, conn);

 7                 //SqlDataReader rdr = cmd.ExecuteReader();

 8                 SqlDataAdapter myda = new SqlDataAdapter(strSQL, conn);

 9                 DataSet myds = new DataSet();

10                 myda.Fill(myds);

11                 DataTable tb = myds.Tables[0];

12                 for (int i = 0; i < tb.Rows.Count; i++)

13                 {

14                     DataRow row = tb.Rows[i];

15                     int UID = int .Parse(row[ "OrderNumber"].ToString());

16                     string taksName = row["Title" ].ToString();

17                     DateTime StartDate = DateTime .Parse(row[ "StartDate"].ToString());

18                     DateTime DueDate = DateTime .Parse(row[ "DueDate"].ToString());

19                     TimeSpan ts = DueDate.Subtract(StartDate);

20                     int FatherTask = int .Parse(row[ "FatherTask"].ToString());

21                     int datelenth = ts.Days;

22                     taskStr.Append( "{" );

23                     taskStr.Append( "'UID': '" + UID + "'," );

24                     taskStr.Append( "'Name': '" + taksName + "'," );

25                     taskStr.Append( "'Duration': " + datelenth + "," );

26                     taskStr.Append( "'Start': '" + StartDate.ToString() + ":T00:00:00" + "',");

27                     taskStr.Append( "'Finish': '" + DueDate.ToString() + ":T00:00:00" + "',");

28  

29                     taskStr.Append( "'PercentComplete': " + 0 + "," );

30                     if (FatherTask == -1)

31                         taskStr.Append( "'Summary': " + 1 + "," );

32                     else

33                         taskStr.Append( "'Summary': " + 0 + "," );

34                     taskStr.Append( "'Critical': " + 1 + "," );

35                     taskStr.Append( "'Milestone': " + 0 + "," );

36                     taskStr.Append( "'PredecessorLink': 0," );

37                     taskStr.Append( "'ParentTaskUID': " + FatherTask + "," );

38                     taskStr.Append( "}" );

39                     if (i != tb.Rows.Count - 1)

40                         taskStr.Append( "," );

41                 }

42                 taskStr.Append( "]" );

43             }

44             Response.Write(taskStr.ToString());

45             Response.End();

 

你可能感兴趣的:(Ajax)