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