2017年3月30日 星期四

[javascript]觸發textbox的keydown使觸發button事件


javascript
<script type="text/javascript">
 
    function Function() {

        var button = document.getElementById("<%=Button1.ClientID %>");
        button.click();
</script>



asp.net
        <input type="text" onkeydown="Function()" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" Style="display: none" Visible="True" />


C#
 protected void Button1_Click(object sender, EventArgs e)
       {


            Response.Redirect("WebForm1.aspx");
       }


}

2017年3月27日 星期一

[C#]介面

   interface write
    {

        void testwrite(int word);

     
    }

    public class realwrite : write
    {
        public void testwrite(int word)
        {
            word += word;
        }
    }

2017年3月23日 星期四

[Linq] 練習

 protected void Page_Load(object sender, EventArgs e)
        {

            var people = GenerateListOfPeople();

            //There will be two Persons in this variable: the "Steve" Person and the "Jane" Person
            var peopleOverTheAgeOf30 = people.Where(x => x.Age > 30);//設定條件
            foreach (var person in peopleOverTheAgeOf30)
            {
              Label1.Text=  person.FirstName;
            }
        }
        public static void Main()
        {
            var people = GenerateListOfPeople();

            // Write your code here
        }

        public static List<Person> GenerateListOfPeople()//資料庫
        {
            var people = new List<Person>();

            people.Add(new Person { FirstName = "Eric", LastName = "Fleming", Occupation = "Dev", Age = 24 });
            people.Add(new Person { FirstName = "Steve", LastName = "Smith", Occupation = "Manager", Age = 40 });
            people.Add(new Person { FirstName = "Brendan", LastName = "Enrick", Occupation = "Dev", Age = 30 });
            people.Add(new Person { FirstName = "Jane", LastName = "Doe", Occupation = "Dev", Age = 35 });
            people.Add(new Person { FirstName = "Samantha", LastName = "Jones", Occupation = "Dev", Age = 24 });

            return people;
        }
 

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Occupation { get; set; }
        public int Age { get; set; }
    }


資料來源:https://www.microsoft.com/net/tutorials/csharp/getting-started/linq

2017年3月22日 星期三

[C#]try心得

try { //程式主執行區或可能發生錯誤的地方 } catch (Exception ex) { //例外的處理方法,如秀出警告 } finally { //不論是否發生例外事件都會執行的區塊 }

[javascript]列印的範圍用 DIV 區塊包起來

Step 1:將你要列印的範圍用 DIV 區塊包起來

<div id="print_parts">
   框住要列印的網頁範圍,記得不要破壞到 Tag 的巢狀架構
</div>




Step 2:撰寫 javascript 程式區塊



<script type="text/javascript">
function printScreen(printlist)
  {
     var value = printlist.innerHTML;
     var printPage = window.open("", "Printing...", "");
     printPage.document.open();
     printPage.document.write("<HTML><head></head><BODY onload='window.print();window.close()'>");
     printPage.document.write("<PRE>");
     printPage.document.write(value);
     printPage.document.write("</PRE>");
     printPage.document.close("</BODY></HTML>");
  }
</script>


Step 3:在網頁中插入列印的按鈕或文字

<a href="#" onclick="printScreen(print_parts);" >Printing ... </a>


資料來源:
http://blog.xuite.net/tolarku/blog/447081329

2017年3月21日 星期二

[C#]連接字串變成一個clss


連接字串變成一個clss,可以重複使用,只要帶參數進去


 protected void Page_Load(object sender, EventArgs e)
        {
         
            string text = "參數";

            connection1(text);




        }

        public void connection1  (string text)
            {
            SqlConnection conn = new SqlConnection("Data Source=IP位置;Initial Catalog=資料庫;Persist Security Info=True;User ID=使用者ID;Password=使用者密碼");
            SqlCommand cmd = new SqlCommand("sql字串 where id=@id", conn);
            cmd.Parameters.AddWithValue("@id", text);
            SqlDataReader Rd;
            conn.Open();
            //cmd.Connection = conn;
            Rd = cmd.ExecuteReader();
            while (Rd.Read())
            {
                Response.Write("ok");
            }
            conn.Close();


        }


P.S.自己想的,以前面試被問到。

[C#]grid view轉成excel

protected void btn_excel_Click(object sender, EventArgs e)按鈕觸發

 {
   //呼叫的片段、方式
   // export_excel(要匯出的 Gridview 名稱, 匯出的檔名,模式);  // 1=會加入日期時間
    export_excel("gridview1", "output",1);
 }

private void export_excel(string gvname, string filename, int t_mode)
 {
   //  呼叫方式 export_excel("gridview1", "output",1);
   // export_excel(要匯出的 Gridview 名稱, 匯出的檔名,模式);  // 1=會加入日期時間

   GridView xgv = (GridView) FindControl(gvname);//重點可增加數入excel



   string style = "<style> .text { mso-number-format:\\@; } </script> ";
   System.IO.StringWriter sw = new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
 
Response.Clear();


   if(t_mode==1)  // 加上時間日期
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "_" + DateTime.Now .ToString ("yyyyMMdd-HHmm") + ".xls");
   else
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename +".xls");


   Response.Cache.SetCacheability(HttpCacheability.NoCache);


   Response.ContentType = "application/vnd.ms-excel";


   Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");


   xgv.RenderControl(hw);//重點可增加數入excel


   Response.Write(style);
   Response.Write(sw.ToString().Replace("<div>", "").Replace("</div>", ""));
   Response.End();
 }

public override void VerifyRenderingInServerForm(Control control)
 {
     //處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
 }


參考網址:http://blog.xuite.net/tolarku/blog/26786484-ASP.NET+%E7%B0%A1%E5%96%AE%E4%BA%94%E6%AD%A5%E9%A9%9F%E5%B0%87+GridView+%E8%B3%87%E6%96%99%E8%BD%89%E6%AA%94%E5%88%B0+Excel