project code

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.Collections;

/// <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();
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值