demo1

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Test.WebForm1" %>



   
    测试
   
   



   


       

           
               

                   
               
           
           
               
                   


               
               
                   

                       
                       
                       
                   

               
               

<%# Eval("Id") %><%#  Eval("ProName") %><%# Eval("ProPrice") %>

           
       

       

           
       

       
       

       

       
        angularJS
       


             
           
                        
                       
                       
                       
                       
                   
                 
{{ $index + 1 }}{{ x.Name }}{{ x.Country }}

           

在输入框中尝试输入:


           

姓名:
               


           


           
               
                   
                        
                       
                       
                       
                       
                   
               
{{ $index + 1 }}{{ x.Name }}{{ x.Country }}

           

       

        

          
            

   




using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GriddBind();
            }

        }
        protected void GriddBind()
        {
            DataTable newdtb = new DataTable();
            newdtb.Columns.Add("Id", typeof(int));
            newdtb.Columns.Add("ProName", typeof(string));
            newdtb.Columns.Add("ProPrice", typeof(decimal));
            newdtb.Columns["Id"].AutoIncrement = true;
            for (int i = 1; i < 11; i++)
            {
                DataRow newRow = newdtb.NewRow();
                newRow["ProName"] = "测试pro" + i.ToString();
                newRow["ProPrice"] = 12.3m;
                newdtb.Rows.Add(newRow);
            }
            Repeater1.DataSource = newdtb;
            Repeater1.DataBind();
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable newdtb = new DataTable();
            newdtb.Columns.Add("Id", typeof(int));
            newdtb.Columns.Add("ProName", typeof(string));
            newdtb.Columns.Add("ProPrice", typeof(decimal));
            newdtb.Columns["Id"].AutoIncrement = true;
            for (int i = 1; i < 11; i++)
            {
                DataRow newRow = newdtb.NewRow();
                newRow["ProName"] = "测试pro" + i.ToString();
                newRow["ProPrice"] = 12.3m;
                newdtb.Rows.Add(newRow);
            }
            String strSql = DataTableConvertJson.DataTable2Json(newdtb);
            Literal1.Text = strSql;
        }
    }
    public class DataTableConvertJson
    {

        #region dataTable转换成Json格式
        ///

 
        /// dataTable转换成Json格式 
        ///
 
        ///  
        ///  
        public static string DataTable2Json(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"");
            jsonBuilder.Append(dt.TableName);
            jsonBuilder.Append("\":[");
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }

        #endregion dataTable转换成Json格式
        #region DataSet转换成Json格式
        ///

 
        /// DataSet转换成Json格式 
        ///
 
        /// DataSet
        ///  
        public static string Dataset2Json(DataSet ds)
        {
            StringBuilder json = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {
                json.Append("{\"");
                json.Append(dt.TableName);
                json.Append("\":");
                json.Append(DataTable2Json(dt));
                json.Append("}");
            }
            return json.ToString();
        }
        #endregion

        ///


        /// Msdn
        ///

        ///
        ///
        ///
        public static string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }
    }
}

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