📄 utilities.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Drawing;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
public class Utilities
{
public static void SendMail(string from, string to, string subject, string body)
{
SmtpClient mailClient = new SmtpClient(/*BalloonShopConfiguration.MailServer*/"");
MailMessage mailMessage = new MailMessage(from, to, subject, body);
/*
// For SMTP servers that require authentication
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "SmtpHostUserName");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "SmtpHostPassword");
*/
// Send mail
mailClient.Send(mailMessage);
}
// Send error log mail
/*
public static void LogError(Exception ex)
{
// get the current date and time
string dateTime = DateTime.Now.ToLongDateString() + ", at "
+ DateTime.Now.ToShortTimeString();
// stores the error message
string errorMessage = "Exception generated on " + dateTime;
// obtain the page that generated the error
System.Web.HttpContext context = System.Web.HttpContext.Current;
errorMessage += "\n\n Page location: " + context.Request.RawUrl;
// build the error message
errorMessage += "\n\n Message: " + ex.Message;
errorMessage += "\n\n Source: " + ex.Source;
errorMessage += "\n\n Method: " + ex.TargetSite;
errorMessage += "\n\n Stack Trace: \n\n" + ex.StackTrace;
// send error email in case the option is activated in Web.Config
if (BalloonShopConfiguration.EnableErrorLogEmail)
{
string from = "noreply@cristiandarie.ro";
string to = BalloonShopConfiguration.ErrorLogEmail;
string subject = BalloonShopConfiguration.SiteName + " error report";
string body = errorMessage;
SendMail(from, to, subject, body);
}
}
*/
public static void TieButton(Page page, Control TextBoxToTie, Control ButtonToTie)
{
// Init jscript
string jsString = "";
// Check button type and get required jscript
if (ButtonToTie is LinkButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.ClientScript.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else if (ButtonToTie is ImageButton)
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
+ page.ClientScript.GetPostBackEventReference(ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
}
else
{
jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."
+ "forms[0].elements['" + ButtonToTie.UniqueID.Replace(":", "_") + "'].click();return false;} else return true; ";
}
// Attach jscript to the onkeydown attribute - we have to cater for HtmlControl or WebControl
if (TextBoxToTie is HtmlControl)
{
((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
else if (TextBoxToTie is WebControl)
{
((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
}
}
public static void BindingDataGridView(GridView dg, DataTable dt)
{
dg.DataSource = dt;
dg.DataBind();
}
public static void BindingDropDownList(DropDownList ddl, DataTable dt, string strTextField, string strValueField)
{
ddl.DataSource = dt;
ddl.DataTextField = strTextField;
if (strValueField != "" || strValueField != null)
ddl.DataValueField = strValueField;
ddl.DataBind();
}
public static void BindingListBox(ListBox lb, DataTable dt, string strDisplayMember, string strValueMember)
{
lb.DataSource = dt;
lb.DataTextField = strDisplayMember;
if (strValueMember != "" || strValueMember != null)
lb.DataValueField = strValueMember;
lb.DataBind();
}
public static string GetValueInDDLByText(DropDownList ddl,string strSelectDisplayMember)
{
string str = String.Empty;
foreach(ListItem li in ddl.Items)
{
if (li.Text == strSelectDisplayMember)
{
str = li.Value;
}
}
return str;
}
public static ArrayList GetFontsName()
{
ArrayList al = new ArrayList();
foreach(FontFamily oneFontFamily in FontFamily.Families)
{
al.Add(oneFontFamily.Name);
}
return al;
}
public static void BindingDataList(DataList dl,DataTable dt,string datakeyfield)
{
dl.DataSource = dt;
dl.DataKeyField = datakeyfield;
dl.DataBind();
}
public static void BindingDropDownList(DropDownList ddl, ArrayList al)
{
ddl.DataSource = al;
ddl.DataValueField = ddl.DataTextField;
ddl.DataMember = ddl.DataValueField;
ddl.DataBind();
}
public static void SetSelectDropDownListByDisplayMember(DropDownList ddl, string strDisplayMember)
{
int i=0;
foreach (ListItem li in ddl.Items)
{
if (li.Text == strDisplayMember)
{
ddl.SelectedIndex = i;
break;
}
i++;
}
}
public static void SetSelectDropDownListByValueMember(DropDownList ddl, string strValueMember)
{
int i = 0;
foreach (ListItem li in ddl.Items)
{
if (li.Value == strValueMember)
{
ddl.SelectedIndex = i;
break;
}
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -