SHOWcode

2018年11月8日 星期四

C# ajax 範例


C#.net ajax 範例


  <asp:ScriptManager ID="ScriptManager" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel2"  UpdateMode="Conditional" runat="server" >
    <ContentTemplate>
       <fieldset>
       <legend>UpdatePanel content</legend>
        <!-- Other content in the panel. -->
           <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
           <table style="width: 1024px" align="center" >
                    <tr>
                        <td>
                 <nobr>
                <table class="table table-bordered" id="maintable" rules="ALL" class="table" style="width: 100%; max-width: 1024px; border-color: black;" align="center">
                      <tr style=" display: none;">
                       <td style="width: 10%;"></td>
                        <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                        <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>
                         <td style="width: 10%;"></td>

                    </tr>
<asp:Label ID="Label2" runat="server" ></asp:Label>
                     </table>
                        </td>
                        </tr>

                    </table>

           <input type="checkbox" checked="checked" />
           <input id="ch" type="checkbox"  />
           <input type="checkbox" checked="checked" />
           <asp:TextBox ID="TextBox23" runat="server"></asp:TextBox>
           <asp:Button ID="Button2" runat="server" Text="Button"/>
           <iframe id="iframesun" runat="server" style="width:100%;height:944px"></iframe>
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>

2018年10月30日 星期二

ASP.NET TextBox控制項設置ReadOnly="true"後臺取不到值


原文連結

ASP.NET TextBox控制項設置ReadOnly="true"後臺取不到值

當TextBox設置了ReadOnly="true" 後,要是在前臺為控制項添加了值,後臺是取不到的,值為「空」
原理沒想通,說不清楚微軟是出於什麼考慮的,
不過有時是要我們能通過前臺腳本來填充值,並不希望使用者修改其控制項內容,這時就比較尷尬了。
剛開始是換成Html 裡的 <input type="text" readonly="readonly" > ,不過後來發現這個工作量是很大的,
所以網上搜了下,沒找出TextBox ReadOnly="true" 頁面填充值取不到的原因,不過問題還算解決了。
正文:
出於vs2005對頁面代碼的規範與安全,個人意見是使用第二種方法
知道為什麼TextBox設置了ReadOnly屬性後,取不到值原理的,給下說明,謝謝啦!
在.NET 2.0 下,當頁面上的某個TextBox 設置了屬性ReadOnly="True"時,通過用戶端腳本給其賦值後,在後臺代碼中訪問其Text屬性卻無法獲得該值。經過嘗試,發現可以通過如下的方式解決這個問題:

方法一:
不設置ReadOnly屬性,通過onfocus=this.blur()來類比,如下:
<asp:TextBox ID="TextBox1" runat="server" onfocus=this.blur()></asp:TextBox>
在此情況下,當文字方塊獲得焦點時便立刻失去,所以也無法手動修改其內容,能夠類比ReadOnly,在後臺代碼中也能通過Text屬性,正常獲取通過腳本在用戶端設置的值;

方法二:
設置了ReadOnly屬性後,通過Request來取值,如下:
前臺代碼:
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" ></asp:TextBox>
後臺代碼:
string Text = Request.Form["TextBox1"].Trim();

方法三:
在Page_Load()正設置文字方塊的唯讀屬性,在前臺不設置。
就能正常讀取,如下:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox1.Attributes.Add("readonly","true");
}
}

2018年10月2日 星期二

C# 取得檔案最後修改時間



  foreach (string fname in System.IO.Directory.GetFileSystemEntries(@"C:\Users\joshs"))
            {
                //if (fname.IndexOf(".gif") > 0) {
                //MessageBox.Show("檔案建立日期" + fname.ToString(), "FileInfo");
                //     MessageBox.Show("檔案建立日期" + fname.ToString(), "FileInfo");
                //}

                MessageBox.Show("檔案最後修改時間:" + File.GetLastWriteTime(fname).ToString());

            }

2018年10月1日 星期一

字串 取值方法(第二階段) Class 取得 C#.NET 控制項


字串 取值方法(第二階段) 先在在 其他NET 建置laceHolder


#region BUTTON
    protected void lbtn_Click(object sender, EventArgs e)
    {
        Page page = (Page)System.Web.HttpContext.Current.Handler; //Page 給 class  這是重點

        Button lbtn = (Button)sender;
        String buttonid = lbtn.ID.ToString();//button 按鈕ID
       
        Label labtn = (Label)page.FindControl("lb_func2");  //取得控項
        TextBox txttn = (TextBox)page.FindControl("txt_func2");
       

       String mail_check = fun_sg_mail();  //

        if (mail_check == "success") {
            page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('信件寄出');", true);
        }
        else {
            page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('通報信件尚未寄出,麻煩幫忙管理員,謝謝');", true);
        }


    }
#endregion


字串 調用方法(第一階段)


  public static object callFunc(string funcName)
    {
        //反射獲取 命名空間+類名稱
//using System.Reflection;
        String TXSR = funcName;
        string className = "activefuction";
        string methodName = funcName;            //傳遞参数           
                                                 // Object[] paras = new Object[] { "string" };
        var t = Type.GetType(className);
        object obj = Activator.CreateInstance(t);
        try
        {
            #region 方法一                //直接调用               
            MethodInfo method = t.GetMethod(methodName);
            //method.Invoke(obj, paras);

            return method.Invoke(obj, null);

            // return method.Invoke(obj, null);
            #endregion

            #region 方法二               
            //MethodInfo[] info = t.GetMethods();
            //for (int i = 0; i < info.Length; i++)
            //{
            //    var md = info[i];                    //方法名                   
            //    string mothodName = md.Name;                    //参数集合                 
            //    ParameterInfo[] paramInfos = md.GetParameters();                    //方法名相同且參數個數一樣                 
            //    if (mothodName == methodName && paramInfos.Length == paras.Length)
            //    {
            //        md.Invoke(obj, paras);
            //    }
            //}
            #endregion

        }
        catch (Exception ex)
        {
            throw ex;
        }


    }


    #endregion

2018年9月18日 星期二

ASP.NET引用共用類別(aspx.cs檔引用cs檔)

ASP.NET引用共用類別(aspx.cs檔引用cs檔)

劃TABLE C#

  #region 劃TABLE


        Table newTable = Table1;
        //準備資料列及資料儲存格
        System.Web.UI.WebControls.TableRow testTableRow;
        System.Web.UI.WebControls.TableCell testTableCell;

        //newTable.ID = "newTable_1";// + tbcount.ToString();



        newTable.Attributes["runat"] = "server"; //


        //2列
        for (int TR_i = 0; TR_i < str_date_rpt.Rows.Count + 2; TR_i++)
        {
            testTableRow = new TableRow();



            //2格 >按照站別動態給格數 
            for (int Tc_i = 0; Tc_i < str_date_rpt.Columns.Count + 1; Tc_i++)
            {
                testTableCell = new TableCell();


                testTableRow.Cells.Add(testTableCell);
            }

            newTable.Rows.Add(testTableRow);
        }
        //this.Table1.Controls.Add(newTable);
        #endregion

在原有的DataTable 中动态怎么样添加一行(C#)

假定你的datatable名为tblDatas,有Product、Version、Description三个字段

DataTable tblDatas =  nowtable;


DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = "水果刀";
newRow["Version"] = "2.0";
newRow["Description"] = "打架专用";
tblDatas.Rows.Add(newRow);

來源

2018年6月14日 星期四

C# PlaceHolder



 protected void Page_Load(object sender, EventArgs e)
    {
        Label myLabel = new Label();
        TextBox mytxt1 = new TextBox();
        DropDownList dp01 = new DropDownList();

        // Set the label's Text and ID properties.
        myLabel.Text = "Label123";
        myLabel.ID = "Label123";
        PlaceHolder1.Controls.Add(myLabel);
        // Add a spacer in the form of an HTML <br /> element.
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        mytxt1.ID = "mytxt1";
        mytxt1.Text = "";
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        //dp01.DataBind();
        dp01.Items.Insert(0, new ListItem("Select a store", ""));
        dp01.Items.Insert(1, new ListItem("AAA", ""));
        dp01.Items.Insert(2, new ListItem("BBB", ""));
        PlaceHolder1.Controls.Add(mytxt1);

        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        PlaceHolder1.Controls.Add(dp01);
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

        //CheckBox
        CheckBoxList ckL01 = new CheckBoxList();
        ckL01.Items.Insert(0, new ListItem("please check point:", ""));
        ckL01.Items.Insert(1, new ListItem("point 001", "point001"));
        ckL01.Items.Insert(2, new ListItem("point 002", "point002"));
        ckL01.Items.Insert(3, new ListItem("point 003", "point003"));
        PlaceHolder1.Controls.Add(ckL01);

        //botton
        for (int i = 1; i <= 10; i++)
        {
            LinkButton olbtn = new LinkButton();
            olbtn.ID = "lbtn" + i;
            olbtn.Text = "測試按鈕" + i;
            olbtn.Click += new EventHandler(lbtn_Click);
            PlaceHolder1.Controls.Add(olbtn);

            Label olab = new Label();
            olab.Text = " | ";
            PlaceHolder1.Controls.Add(olab);

        }




    }

    protected void lbtn_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;

        Response.Write("按鈕事件觸發:" + lbtn.ID);
       // Label123.Text = "成功";

    }

2018年6月6日 星期三

使用LoadControl()動態加入UserControl

protected void Page_Load(object sender, EventArgs e)
{
    string UserControlPath = “MyControl.ascx"
    Control c = LoadControl(UserControlPath, TextBox1);
    PlaceHolder1.Controls.Add(c);
    ViewState[“UserControlPath"] = UserControlPath;
}

網址:原文

2018年5月29日 星期二

MS SQL 連線 操作 OR 抓資料


conn = conn.String()

String insert_data = into_data.String()
String show_data = data.String()

====SQL 操做
             

               
                SqlCommand cmd = new SqlCommand(insert_data, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                conn.Close();



=====SQL 抓資料table

            SqlCommand cmdERP = new SqlCommand(show_data, conn);
            conn.Open();
            SqlDataReader reader_farm = cmdERP.ExecuteReader();
            DataTable dt_eruncard = new DataTable();
            dt_eruncard.Load(reader_farm);
            conn.Close();




Jenkins-mail

參考:http://www.linuxea.com/1767.html 前置作業略過~有空再補 Mailer Plugin post { success { emailext ( subject: ...