zoukankan      html  css  js  c++  java
  • .NET杂记 (自定义datatable 数值保留5位小数 将指定的datatable插入到数据库 GridViewRow gridview 文件另存 DataTable.Clone datatable.copy区别 检验是否为全角……)

    访问安全问题

    //如果发生安全问题财要在配制文件中加上在<configuration>之间加上 <system.web> <identity impersonate="true"/> </system.web>

    控制电子邮件Mail

    MailMessage mm = new MailMessage();

    mm.From = "ding@dcjet.com.cn";

    mm.To = "xbding@dcjet.com.cn";

    mm.Subject = "Microsoft Office Outlook 测试消息";

    mm.Body = "我你";

    SmtpMail.SmtpServer = "dcjet.com.cn";

    SmtpMail.Send(mm);

    Response.Write("<script>alert('发送成功!')</script>");

    ASP小巧门

             在写页面的时候,<input >里不用id,只用name。这样页面文本框输入值提交过后会被记录下来。

    下次再输入第一个字符相同时就会把以前输入首字符相同的数值自动以下拉列表形式显示出来。

    将日期转变指定格式:

    String(“yyMMdd”);

    DateTime.Now.dropdownList

    this.cmbCustomsNo.DataSource = dt;

    this.cmbCustomsNo.DataTextField = "CUSTOMS_NAME";

    this.cmbCustomsNo.DataValueField = "CUSTOMS_CODE";

    this.cmbCustomsNo.DataBind();

    自定义datatable

    DataTable dt = new DataTable();

    dt.Columns.Add("customs_code",typeof(string));    

    dt.Columns.Add("customs_name",typeof(string));

    DataRow dr = dt.NewRow();

    row["customs_code"] = "all";

    row["customs_name"] = "全部";

    dt.Rows.Add(dr);

     

     

    将数值转为字符串且保留5位小数:

    ToString("f5")

     

    /// <summary>

    /// 将指定的datatable插入到数据库中

    /// </summary>

    public void insertPardata(DataTable tmpdt,string sql)

    {

    try

    {

    Cn.Open();

    SqlDataAdapter da = new SqlDataAdapter(sql, cn);

    DataSet ds = new DataSet();

    da.Fill(ds,"tmp");                  

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

    {

    ds.Tables["tmp"].Rows.Add(tmpdt.Rows[i].ItemArray);

    }                 

    SqlCommandBuilder cmd = new SqlCommandBuilder(da);

    da.Update(ds,"tmp");

    cn.close();

    }

    catch(Exception ex)

    {

    throw ex;

    }

    }

     

    GridViewRow

    foreach (GridViewRow dr in GridView1.Rows)

    {

    dr.Cells[0].Attributes.Add("onclick", "javascript: return window.confirm('" + dr.Cells[5].Text + "?');");

    }

     

      <asp:TemplateField HeaderText="表头序号">

      <ItemTemplate>

     <asp:HyperLink ID="Hlink" Text='<%# Bind("head_id") %>' runat="server"></asp:HyperLink>

      </ItemTemplate>

      </asp:TemplateField>

    Link类型的 ButtonField控制

    获取gridview里的一个Link类型的 ButtonField 控件的值。,

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

    ((System.Web.UI.WebControls.LinkButton)(((GridView)e.CommandSource).Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls[0])).Text

    }

    //这个方法用来转来页

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    If(e.Row.RowType == DataControlRowType.DataRow)

    {

     string url = "EntryDisplay.aspx?EntryID =" + ((System.Web.UI.WebControls.LinkButton)(e.Row.Cells[0].Controls[0])).Text;

            string js = "javascript:window.open('" + url + "',null,'left = 0, top = 0,height=500,width=800,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no');return false;";

     ((System.Web.UI.WebControls.LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", js);

    }

    }

    protected  void  GridView1_RowDeleting(object  sender,  GridViewDeleteEventArgs  e)

    {

    string  jj  =  this.GridView1.Rows[e.RowIndex].Cells[1].Text;

    }

    在GridView中,设置某一列为隐藏列:

    在代码里想获取隐藏列里的控件的值的时候,会发现取出来的值一直是空的,

    不能直接在界面上把某个列visible。应该在GridView1_RowDataBound事情中设置

    e.Row.Cells[index].Visible = false;这样就可以了

    文件另存:

    file.copy("c:\\1.doc","c:\\2.doc");

    相对路径:

    相对路径指的是相对当前页的路径,如果要跳转的页面在当前页的上级目录中,则路径为"../",如果在相同目录下,则不用加路径,如果在在当前页的上级目录中的另一个目录下,则路径为"../foldername/"上上级就是http://www.cnblogs.com/

    最简单的客户端下载服务器端的功能:

    加一个超链接,给一个服务器端的相对路径

    DataTable.Clone 与datatable.copy方法区别

    返回值

    新的 DataTable,与当前的 DataTable 具有相同的架构。

    备注

    注意 如果这些类已经过派生,则副本也具有相同的派生类。

    DataTable.Copy 方法

    返回值

    新的 DataTable,它具有与该 DataTable 相同的结构(表架构和约束)和数据。

    注意 如果这些类已经过派生,则副本也具有相同的派生类。

    检验是否为全角:

    /// <summary>

    /// 检验是否为全角。

    /// </summary>

    private bool checkFormat(string checkString)

    {

    int i = checkString.Length;

    //返回字节数,一个全角有两个字节

    int j = Encoding.Default.GetByteCount(checkString);

    if (i <  j))

    {

    return true;

    }

    else

    {

    return false;

    }

    }

     

    /// <summary>

    /// 向本地插入远程机器的数据

    /// </summary>

    public void insertPardata(DataTable tmpdt,string sql)

    {

    try

    {

    DBOpen();

    SqlDataAdapter da = new SqlDataAdapter(sql, cn);

    DataSet ds = new DataSet();

    da.Fill(ds,"tmp");

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

    {

    ds.Tables["tmp"].Rows.Add(tmpdt.Rows[i].ItemArray);

    }

    SqlCommandBuilder cmd = new SqlCommandBuilder(da);

    da.Update(ds,"tmp");

    DBClose();

    }

    catch(Exception ex)

    {

    throw ex;

    }

    }

     

    页面转页时,传中文参数时会有问题。要将中文加密:

    将文字加密好再给string 变量 然后转值。

    加密:

    Server.UrlEncode()

    解密:

    Server.UrlDecode()

    获取IP地址和mac

    System.Net.IPAddress[] addressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;

    if ( addressList.Length>1)

    { label1.Text = addressList[0].ToString();label2.Text = addressList[1].ToString();}

    else{label1.Text = addressList[0].ToString();label2.Text = "没有可用的连接";}

    获取Mac(网卡物理地址)要引入System.Management;组件 

    string  strAddress = "";

    ManagementObjectSearcher   query   =new   ManagementObjectSearcher("SELECT   *   FROM   Win32_NetworkAdapterConfiguration")   ;  

    ManagementObjectCollection   queryCollection   =   query.Get();  

    foreach(   ManagementObject   mo   in   queryCollection   )    

    {    

    if(mo["IPEnabled"].ToString()   ==   "True")

    {

    strAddress = mo["MacAddress"].ToString();  

    }

    鼠标点击或移开后变颜色:

    onmouseover="if(this.bgColor!='#f2f2d2') this.bgColor='#f1f1f1'" onmouseout="if(this.bgColor!='#f2f2d2') this.bgColor='#ffffff'"

    样式会发生变化:

    如果页面用了样式,但有click事件的时候,会掉了样式。如果不想掉了样式就不超链接。

    解决window.showModalDialog()显示问题:

    例:window.showModalDialog("SetCustomsId.aspx","","dialogHeight:120px;dialogWidth:250px;center:yes;help:no;resizable:no;status:no;");

    当编辑过之后再对showModalDialog()出来的页面SetCustomsId.aspx做修改时,会发现页面没有变化。

    这时可以先window.open("SetCustomsId.aspx","","dialogHeight:120px;dialogWidth:250px;center:yes;help:no;resizable:no;status:no;")

    把页面全部改好后再window.showModalDialog()。就会得到最新的修改的SetCustomsId.aspx

     

    Redirect()方法转业,在该方法上面做的Response.Write(“<script></script>”)不被弹出。

    可用:

    Response.Write("<script>alert('该帐册已存在报关单或出入库申请单,不可删除。'); location.href='" + ActionReturnURL + "';</script>");

    Response.Write("<script>location.href='" + ActionReturnURL + "';</script>");

    三目运算符

    (判断语句)?满足条件返回的值:不满足条件返回的值。

    例:

    (3>2)?1:0

    不刷新页面跳转(类似ajax)只是多了一个页面

    把action.target设为一个iframe “hideFrame”

    这样在Action里做操作时页面不会出现白页

    frmAction.target="hideFrame";

    frmAction.action="Action.aspx";   

    frmAction.DataOperation.value="<%=CUSTOMS_ENTRY_VIEW.Operation.Affirm %>";

    frmAction.submit();

    <iframe name="hideFrame" style="display:none"></iframe>

  • 相关阅读:
    一、单一职责原则
    四、接口隔离原则
    彼得·林奇的25条黄金规则
    程序员的四个境界
    VS2008开发.NET 2.0的项目时,可用的C#3.0语言特性一览表
    Linq试用问题总结
    SQL Server 2000中修改数据库COLLATE一例
    SQL Tip:将SP生成的结果集Insert到另一Table中
    OOAD读书笔记(一):什么是好的软件?
    成功创业的8个关键点
  • 原文地址:https://www.cnblogs.com/xbding/p/net_sundry.html
Copyright © 2011-2022 走看看