DMGLE10 项目开发用到的一些代码总结

  1.  双语的实现:主要思想是通过获取XML 文件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections;

/// <summary>
/// Summary description for Resource
/// </summary>
public class Resource
{
    public Resource()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static string GetString(string strKey)
    {
        Hashtable htMessages = new Hashtable();
        htMessages = GetResource();
        if (htMessages[strKey] == null)
            htMessages[strKey] = string.Empty;
        return Convert.ToString(htMessages[strKey]);
    }

    private static Hashtable GetResource()
    {
        string strDefaultCulture = System.Configuration.ConfigurationManager.AppSettings.Get("culture");
        string strCacheKey = "Localization";
        if (HttpRuntime.Cache[strCacheKey] == null)
        {
            Hashtable htResource = new Hashtable();
            LoadResource(htResource, strDefaultCulture, strCacheKey);
        }
        //(DateTime)Convert.ChangeType(s, typeof(DateTime));

        return (Hashtable)Convert.ChangeType(HttpRuntime.Cache[strCacheKey], typeof(Hashtable));
    }

    private static void LoadResource(Hashtable htResource, string strCulture, string strCacheKey)
    {
        string strFile;
        if (strCulture == "English")
            strFile = System.Configuration.ConfigurationManager.AppSettings["English_Path"] + "Resource.xml";
        else
            strFile = System.Configuration.ConfigurationManager.AppSettings["French_Path"] + "Resource.xml";

        XmlDocument xml = new XmlDocument();
        xml.Load(strFile);

        //For Each n As XmlNode In xml.SelectSingleNode("Resource")
        foreach (XmlNode n in xml.SelectSingleNode("Resource"))
        {
            if (n.NodeType != XmlNodeType.Comment)
                htResource[n.Attributes["name"].Value] = n.InnerText;
        }
        HttpRuntime.Cache.Insert(strCacheKey, htResource);
    }
}
xml文件:

<item name="key">word</item>

2.一些公用的方法  print ,Export

/// <summary>
/// Summary description for UtilityFunction
/// </summary>
public class UtilityFunction
{
    public UtilityFunction()
    {

    }

    // used in .aspx.cs file
    // register client script to a field control which can't be null
    public void registerNotNullScript(System.Web.UI.Page page, string msgName, string objName)
    {
        page.RegisterStartupScript("Close", "<script language=javascript> alert('"
            + Resource.GetString(msgName)
            + "');window.document.form1." + objName + ".focus();</script>");
    }

    // used in .aspx.cs file
    // register client script to a field control which has a maximum length
    public void registerExceedMaxLengthScript(System.Web.UI.Page page, string msgName, string objName)
    {
        page.RegisterStartupScript("Close", "<script language=javascript> alert('"
            + Resource.GetString(msgName)
            + "');window.document.form1." + objName
            + ".value='';window.document.form1." + objName + ".focus();</script>");
    }

    // used in .aspx.cs file
    // register client script to a popup page for self closing
    public void registerPageCloseNoErrorScript(System.Web.UI.Page page)
    {
        page.RegisterStartupScript("Close",
            "<script language=javascript>self.close();"
            + "window.dialogArguments.location.href=window.dialogArguments.location.href</script>");
    }

    // used in .aspx.cs file
    // register client script to a popup page for self closing
    public void registerPageCloseUpdationFailScript(System.Web.UI.Page page)
    {
        page.RegisterStartupScript("Close",
            "<script language=javascript>window.alert('No updatation occurs');</script>");
    }

    // used in .aspx.cs file
    // register client script to a field control which has some validity checking
    public void registerClientScript(System.Web.UI.Page page, string msgName, string objName)
    {
        page.RegisterStartupScript("Close", "<script language=javascript> alert('"
            + Resource.GetString(msgName)
            + "');window.document.form1." + objName
            + ".value='';window.document.form1." + objName + ".focus();</script>");
    }

    // used in .aspx.cs file
    // register client script to a hide a image icon when viewing form
    public void registerHideImageIcon(System.Web.UI.Page page, string objName,
        string strScriptName)
    {
        page.RegisterStartupScript(strScriptName, "<script type='text/javascript'> "
            + " var img1 = document.getElementById('" + objName + "');"
            + " try {img1.style.visibility = 'hidden'; } catch (e) {}</script>");
    }

    // checking whether given string contain only numbers
    // return true if it fails
    public bool isNotValidNumber(string str)
    {
        int k = 0, x = 0;
        string numbers = "0123456789", tmp = "";
        while (k < str.Length)
        {
            tmp = str.Substring(k, 1);
            x = numbers.IndexOf(tmp);
            if (x < 0)
            {
                return true;
            }
            k++;
        }
        return false;
    }

    // used in aspx.cs
    // return the field value based on field name from given DataView
    public string getFieldValue(string strFieldName, DataView dsForm)
    {
        return dsForm.Table.Rows[0][strFieldName].ToString();
    }

       public void ExportGridViewExcel(System.Web.HttpResponse Response, GridView grdView, string filename, ArrayList excludedColumnList)
     {
         Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
        Response.Charset = string.Empty;
        Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        if (grdView.HeaderRow != null && grdView.HeaderRow.Cells != null)
        {
            for (int ct = 0; ct < grdView.HeaderRow.Cells.Count; ct++)
            {
                 string headerText = grdView.HeaderRow.Cells[ct].Text;

                if (grdView.HeaderRow.Cells[ct].HasControls())
                {
                    if (grdView.HeaderRow.Cells[ct].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                    {
                        headerText = ((LinkButton)grdView.HeaderRow.Cells[ct].Controls[0]).Text;
                    }
                    grdView.HeaderRow.Cells[ct].Controls.Clear();
                }
                // grdView.HeaderRow.Cells[ct].Text = headerText;
            }
        }
       
        if (grdView.FooterRow != null)
        {
            grdView.FooterRow.Visible = false;
        }
        foreach (DataControlField field in grdView.Columns)
        {
            if (excludedColumnList.Contains(field.HeaderText))
            {
                field.Visible = false;
            }
        }
      
        grdView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

   

    public void ExportGridViewWord(System.Web.HttpResponse Response, GridView grdView, string filename, ArrayList excludedColumnList)
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".doc");
        Response.Charset = string.Empty;
        Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
        Response.ContentType = "application/vnd.ms-word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        if (grdView.HeaderRow != null && grdView.HeaderRow.Cells != null)
        {
            for (int ct = 0; ct < grdView.HeaderRow.Cells.Count; ct++)
            {
                string headerText = grdView.HeaderRow.Cells[ct].Text;

                if (grdView.HeaderRow.Cells[ct].HasControls())
                {
                    if (grdView.HeaderRow.Cells[ct].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                    {
                        headerText = ((LinkButton)grdView.HeaderRow.Cells[ct].Controls[0]).Text;
                    }
                    grdView.HeaderRow.Cells[ct].Controls.Clear();
                }
                grdView.HeaderRow.Cells[ct].Text = headerText;
            }
        }
        if (grdView.FooterRow != null)
        {
            grdView.FooterRow.Visible = false;
        }
        foreach (DataControlField field in grdView.Columns)
        {
            if (excludedColumnList.Contains(field.HeaderText))
            {
                field.Visible = false;
            }
        }
        grdView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

}

调用的代码:

页面代码:

    <script type="text/javascript">
     function startPrint()
  {
            bdhtml=window.document.body.innerHTML;
            sprnstr="<!--startprint-->";
            eprnstr="<!--endprint-->";
            prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
            window.document.body.innerHTML=prnhtml;
            window.print();
       
  }

   #region print
    protected void btnPrint_ServerClick(object sender, EventArgs e)
    {
        Session["GridDV"] = null;
        BLManager objBLManager = new BLManager();
        DataView dvByDate = objBLManager.BLSelectRequestByDate(ViewState["SysID"].ToString());
        if (dvByDate.Count > 0)
        {
            Session["GridDV"] = dvByDate;
            Page.RegisterClientScriptBlock("abc", "<script type=text/javascript>window.showModalDialog('frmPrintPreviewByDate.aspx', this, 'dialogHeight:500px;dialogWidth:920px;Status:yes;Toolbars:no;Scroll:yes;Help:no;Center:yes;');</script>");
        }
    }
    #endregion

 #region "GridView to Excel"
    protected void btnExport_Click1(object sender, EventArgs e)
    {
        UtilityFunction uf = new UtilityFunction();
        grdByDate2.AllowPaging = false;
        grdByDate2.AllowSorting = false;
        BindByDate2();
        ArrayList array = new ArrayList();
        array.Add("");
        uf.ExportGridViewExcel(Response, grdByDate2, "byDate", array);
        //dgrByDate.AllowPaging = true;
        //dgrByDate.AllowSorting = true;
        //BindByDate();
    }

3 格式化GridView 数据的问题:对于相同的数据,只显示第一条

  if (!this.IsPostBack)
        {
                lastcolumn = string.Empty;
                ViewState["SysID"] = GetManagerCorrsponingSystem();
                BindByDate();
                BindByDate2();
                 
        }

  #region "Formate date"
    protected void dgrByDate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblsub = e.Row.FindControl("lblSub") as Label;
            string curColumn = lblsub.Text;
            if (curColumn == lastcolumn)
            {
                e.Row.Cells[1].Text = string.Empty;
            }
            lastcolumn = curColumn;
        }
    }
    #endregion

4.排序

 #region "Sorting"
    protected void dgrByDate_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataView dvByDate = new DataView();
        BLManager objManager = new BLManager();
        dvByDate = objManager.BLSelectRequestByDate(ViewState["SysID"].ToString());
        if (dvByDate.Count > 1)
        {
           dvByDate.Sort = e.SortExpression+ " "+ConvertSortDirectionToSql(e.SortDirection,e.SortExpression);
           dgrByDate.DataSource = dvByDate;
           dgrByDate.DataBind();
        }
    }

    protected string ConvertSortDirectionToSql(SortDirection sortDirection, string strExpression)
    {
        string newSortDirection;

        if (txtSortingOrder.Text == "ASC")
        {
            newSortDirection = "ASC";
            txtSortingOrder.Text = "DESC";
            string url = "<img src='../images/arrow_up.gif' border='0'>";
            dgrByDate.Columns[1].HeaderText = dgrByDate.Columns[1].HeaderText + url;

            //for (int i = 1; i < dgrByDate.Columns.Count; i++)
            //{
            //    if (dgrByDate.Columns[i].SortExpression.ToString() != strExpression)
            //    {
            //        dgrByDate.Columns[i].HeaderText = dgrByDate.Columns[i].HeaderText;
            //    }
            //    if (dgrByDate.Columns[i].SortExpression.ToString() == strExpression)
            //    {
            //        string url="<img src='../images/arrow_down.gif' border='0'>";
            //        dgrByDate.Columns[i].HeaderText = dgrByDate.Columns[i].HeaderText + url;
            //    }
            //}
        }
        else
        {
            newSortDirection = "DESC";
            txtSortingOrder.Text = "ASC";
            string Url = "<img src='../images/arrow_down.gif' border='0'>";
            dgrByDate.Columns[1].HeaderText = dgrByDate.Columns[1].HeaderText + Url;

            //for (int i = 1; i < dgrByDate.Columns.Count; i++)
            //{
            //    if (dgrByDate.Columns[i].SortExpression.ToString() != strExpression)
            //    {
            //        dgrByDate.Columns[i].HeaderText = dgrByDate.Columns[i].HeaderText;
            //    }
            //    if (dgrByDate.Columns[i].SortExpression.ToString() == strExpression)
            //    {
            //        string Url = "<img src='../images/arrow_up.gif' border='0'>";
            //        dgrByDate.Columns[i].HeaderText = dgrByDate.Columns[i].HeaderText + Url;
            //    }
            //}
        }
        return newSortDirection;
    }
    #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值