2017年3月30日 星期四

利用PageMethod,從JS執行CodeBehind函式

1.必要條件:
        a.加入ScriptManager控制項,EnablePageMethods屬性設為True
        b.此函式必須加上[System.Web.Services.WebMethod]
        c.必須宣告為靜態 Static
        d.JS使用PageMethods物件呼叫伺服器端函式。

2.範例
  2.1 ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
2.2 CodeBehind:
      [System.Web.Services.WebMethod]
        public static string GetServerTime(string format)
        {
            return DateTime.Now.ToString(format);
        }
2.3 JS:
     //呼叫伺服端函式
     PageMethods.GetServerTime("yyyy/MM/dd HH:mm:ss", onSuccess, onError);
   
     //成功時取出內容  
     function onSuccess(Data) {
        var data = Data;
    }
 
     / /失敗時彈出失敗訊息
    function onError(error) {
        if (error != null)
            alert(error);
    }

依經緯度取得地址

1.在CodeBehind建立命名空間,for Google map api轉出的JSON資料之用途

namespace GoogleGeocodingAPI
{
    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { get; set; }
    }
    public class Location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }
    public class Northeast
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Southwest
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    public class Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }
    public class Geometry
    {
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }
    public class Result
    {
        public List<AddressComponent> address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public bool partial_match { get; set; }
        public List<string> types { get; set; }
    }
    public class RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
}

2. 建立函式
        //=======================================================
        //<aummary>緯度經度轉中文地址</summary>
        //<param name="latLng"></param>
        //=======================================================
        private static string latLngToChineseAddress(params string[] latLng)
        {
            string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + string.Join(",", latLng) + "&language=zh-TW&sensor=true";
            string json = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    json = sr.ReadToEnd();
                }
            }
            GoogleGeocodingAPI.RootObject rootObj = JsonConvert.DeserializeObject<GoogleGeocodingAPI.RootObject>(json);
            return rootObj.results[0].formatted_address;
        }

3.呼叫轉換函示
private static string GetAddress(string lat,string lng)
        {
            string result = string.Empty;
            result = latLngToChineseAddress(lat, lng);
            return result;
           
        }

2016年7月25日 星期一

字串與數字轉換,currency and string

1.字串型態的數字轉為整數,currency string to int
ex:
string sNN = 100,200,300
int iNN = int.Parse(Regex.Replace(sNN, @",", "")); (iNN=100200300)

2.從sql 取出來的數字(Numeric)轉為有千分位的字串

2016年7月20日 星期三

ReportViewer相關應用-換頁保留標題


一、換頁後,表頭顯示標題列

1.於RDLC設計畫面,開啟進階模式
2.點選 '靜態'(最上層那ㄧ個)
3.屬性 KeepWithGroup -> After
            RepeatOnNewPage -> True


二、不換頁,全部資料在同一頁

1.ReportViewer屬性 ShowPageNavigatiorControls -> False
2.打開RDLC檔,屬性下拉選擇'報表'(Report),或是報表設計範圍外部灰色區域點一下,
找到InteractiveSize項目,高度設為 0cm。

2016年7月1日 星期五

字串應用

1.有小數點的數字字串,四捨五入取整數且加千分位
ex:string gg1 = 123456.78

decimal dgg = Convert.ToDecimal(gg1);
sting gg2 = dgg.ToString("#,#");
結果: 123,457

2016年5月19日 星期四

DataGridView塞入CheckBox、Button,Insert CheckBox、Button into DataGridView

1.塞入CheckBox:
  1.1 在Form_Load裡面:
            int iColumnCount = dataGridView1.ColumnCount;
            if (iColumnCount == 0)
            {
                DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn()
                {
                    Name = "選擇",
                    FalseValue = 0,
                    TrueValue = 1,
                    Visible = true
                };
                dataGridView1.Columns.Add(col);
            }
 
 1.2 檢視有被勾選的項目:
 foreach (DataGridViewRow row in dataGridView1.Rows)
 {
      DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
      if(Convert.ToBoolean(chk.Value))
      {
          string data= row.Cells[1].Value.ToString() ;//取出第1欄位資料
      }
 }

2.塞入Button:
  2.1  在Form_Load裡面:
            int iColumnCount = dataGridView1.ColumnCount;
            if (iColumnCount == 0)
            {
                DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                dataGridView1.Columns.Add(btn);
                btn.Name = "btn";
                btn.HeaderText = "標題";
                btn.Text = "執行";
                btn.UseColumnTextForButtonValue = true;
            }
  2.2 執行被按下的Button:
        在dataGridView1_CellClick裡面,
         if (e.ColumnIndex == 0)
         {
              ... do something...
              .
           }

2016年1月8日 星期五

開啟新的Form且定時自動關閉

1.Create new Form, name Form2
2.At Form1:
private void btn_Buttin1_Click(object sender, EventArgs e)
{
    Form2 FM2 = new Form2();
    FM2.ShowDialog();
}
3.At Form2:
private void Form2_Shown(object sender, EventArgs e)
{
    this.timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.timer1.Interval = 1000; //1秒
    this.Close();
}