Encapsulate applications of basic classes in projects

Source: Internet
Author: User

I believe you are using it. net will encapsulate its own basic classes during development, including a lot of basic classes such as saving data, verifying various controls and sending emails. The following is what is used in the development process, I have sorted it out for a long time, so I feel it is necessary to paste it here. Hope to be useful to everyone!

Public class webbasesetdata: webbasepage

{

 

Public webbasesetdata (){
//
// Todo: Add constructor logic here
//
}

Protected virtual void initform (){

}

Protected virtual bool savedatadetail (){
Return true;
}

Protected virtual void savedatadetailafter (){

}

Protected bool savedata (){
Try {
If (! Checkbeforesave ()){
Return false;
}

Bool rtnvalue = savedatadetail ();
If (rtnvalue ){

}< br> savedatadetailafter ();
return rtnvalue;
}< br> catch (exception e) {
string errmsg = E. message;
errmsg = errmsg. replace ("\ r", "");
errmsg = errmsg. replace ("\ n", "");
errmsg = errmsgctr (errmsg);
errmsgstr = errmsg;

Return false;
}
}

 

# Region validation of input

Protected virtual bool checkbeforesave (){
Return true;
}

Protected bool checknull (system. Web. UI. webcontrols. webcontrol control, string hintmsg ){
Bool rtnvalue = true;
If (control is textbox ){
If (control as textbox). Text. Trim () = ""){
Rtnvalue = false;
}
}
Else if (control is dropdownlist ){
If (control as dropdownlist). selectedvalue. tostring () = ""){
Rtnvalue = false;
}
}

If (! Rtnvalue ){
Showerrmsg (hintmsg );
}

Return rtnvalue;
}

Protected void checkisemial (textbox [] itxtarray ){
For (INT I = 0; I <itxtarray. length; I ++ ){
Itxtarray [I]. attributes ["onchange"] = "Return isemail (this. value ,'.');";
}
}

Protected void checkisdatetime (textbox [] itxtarray ){
For (INT I = 0; I <itxtarray. length; I ++ ){
Itxtarray [I]. attributes ["onchange"] = "Return isdatetime (this. value ,'.');";
}
}

Protected void checkisdate (textbox [] itxtarray ){
For (INT I = 0; I <itxtarray. length; I ++ ){
Itxtarray [I]. attributes ["onchange"] = "Return isdate (this. value ,'.');";
}
}

Protected void checkisnum (textbox [] itxtarray ){
For (INT I = 0; I <itxtarray. length; I ++ ){
Itxtarray [I]. attributes ["onchange"] = "Return isnumberstr (this. Value );";
}
}

Protected void checkisregistername (textbox [] itxtarray ){
For (INT I = 0; I <itxtarray. length; I ++ ){
Itxtarray [I]. attributes ["onchange"] = "Return isregisterusername (this. Value );";
}
}

Protected void setdeletebtn (Button BTN ){
BTN. attributes ["onclick"] = "Return confirm ('whether to delete ');";
}

Protected void setdeleteimgbtn (imagebutton imgbtn ){
Imgbtn. attributes ["onclick"] = "Return confirm ('whether to delete ');";

}

# Endregion

 

# Region about upload

Protected void delefile (string filepath ){
File. Delete (filepath );
}

Private void creatdir (string filetmppath ){

If (! Directory. exists (filetmppath )){
Directory. createdirectory (filetmppath );
}

}

Protected virtual void uploadend (string filepath ){

}

Protected bool uploaddata (fileupload myfile, string logicpath ){
String filename = "", fileextension = "", fullname = "";
Logicpath = server. mappath (".") + @ "\" + logicpath;

Creatdir (logicpath );

If (myfile. postedfile! = NULL ){
Filename = path. getfilename (myfile. postedfile. filename );
Fileextension = path. getextension (myfile. postedfile. filename );
Fullname = logicpath + @ "\" + guid. newguid (). tostring () + "_" + filename;

Try {
Myfile. postedfile. saveas (fullname );
Uploadend (fullname );
Return true;

}
Catch (exception s ){
Throw S;
}
}
Return false;
}

Protected string readfilebytype (string filepath ){
String retstr = "";
Int I = 0;

File. setattributes (filepath, fileattributes. Normal );
Filestream tmpstr = new filestream (filepath, filemode. Open );

If (tmpstr. Canread ){
For (; (I = tmpstr. readbyte ())! =-1 ;){
Retstr + = (char) I;
}
}

Tmpstr. Close ();

File. setattributes (filepath, fileattributes. readonly );

Return retstr;
}

# Endregion

 

# region
protected void vwobjtoentity (T vwobj, t entity) {
foreach (propertyinfo pro in typeof (t ). getproperties () {
If (! Pro. Canread) {
continue;
}< br> If (! Pro. canwrite) {
continue;
}

If (PRO. getvalue (vwobj, null )! = NULL) {
pro. setvalue (entity, pro. getvalue (vwobj, null), null);
}< br> else {
pro. setvalue (entity, null, null);
}< BR >}

Protected void setobjtoentity <t, m> (T sobj, M eobj, string comment t ){
Foreach (propertyinfo prom in typeof (M). getproperties ()){
If (partition T. indexof (prom. Name)> = 0 ){
Continue;
}

Foreach (propertyinfo prot in typeof (T). getproperties ()){
If (prom. Name = Prot. Name ){
If (Prot. getvalue (sobj, null )! = NULL ){
If (prom. propertytype = typeof (system. nullable <double>) | prom. propertytype = typeof (system. Double )){
Prom. setvalue (eobj, convert. todouble (Prot. getvalue (sobj, null), null );
}
Else if (prom. propertytype = typeof (system. nullable <int16> )){
Prom. setvalue (eobj, convert. toint16 (Prot. getvalue (sobj, null), null );
}
Else if (prom. propertytype = typeof (system. byte )){
Prom. setvalue (eobj, convert. tobyte (Prot. getvalue (sobj, null), null );
}
Else if (prom. propertytype = typeof (system. decimal )){
Prom. setvalue (eobj, convert. todecimal (Prot. getvalue (sobj, null), null );
}
Else if (Prot. propertytype = typeof (system. Char )){
Prom. setvalue (eobj, convert. tostring (Prot. getvalue (sobj, null), null );
}
Else if (prom. propertytype = typeof (system. Char )){
Prom. setvalue (eobj, convert. tochar (Prot. getvalue (sobj, null), null );
}
Else {
Prom. setvalue (eobj, Prot. getvalue (sobj, null), null );
}
}
}
}
}
}

# Endregion

 

 

# Region -- sendemail --

Protected string getemailbody (string emailtemplatename ){
String emailbody = "";
Emailbody = readfilebytype (httpcontext. Current. Request. physicalapplicationpath + @ "emailtemplate \" + emailtemplatename );
Return emailbody;
}

Protected void sendmailbodytolist (string subject, string emailbody, list <string> tolist ){

String MailServer = configurationsettings. receivettings ["MailServer"];
String fromname = configurationsettings. receivettings ["mailfromname"];
String fromaddr = configurationsettings. receivettings ["mailfromaddress"];
List <string> to = tolist;

Sendemail (fromname, fromaddr, to, new list <string >{}, new list <string >{}, MailServer, "", "", new list <string> {}, subject, emailbody, true, null, 60, false );

}

Protected void sendmailtolist (string emailtemplatename, list <string> tolist ){
String emailbody = "";
Emailbody = readfilebytype (httpcontext. Current. Request. physicalapplicationpath + @ "emailtemplate \" + emailtemplatename );
String MailServer = configurationsettings. receivettings ["MailServer"];
String fromname = configurationsettings. receivettings ["mailfromname"];
String fromaddr = configurationsettings. receivettings ["mailfromaddress"];
List <string> to = tolist;
String subject = "register mail ";
Sendemail (fromname, fromaddr, to, new list <string >{}, new list <string >{}, MailServer, "", "", new list <string> {}, subject, emailbody, true, null, 60, false );
}

Public event sendcompletedeventhandler asyncsendcompleted;

Private void sendemail (string fromname, string fromemail, list <string> recipients, list <string> BCCs, list <string> CCS, string server, string username, string password, list <string> attachments, string subject, string body, Boolean isbodyhtml, object OBJ, int timeout, Boolean needauthenticate ){
Mailaddress from = new mailaddress (fromemail, fromname );
Mailmessage mail = new mailmessage ();
Foreach (VAR to in recipients ){
Mail. to. Add ();
}
Mail. From = from;
Mail. headers. Add ("X-priority", "3 ");
Mail. headers. Add ("X-msmail-priority", "normal ");
Mail. headers. Add ("X-mailer", "Microsoft Outlook Express 6.00.2900.2869 ");
Mail. headers. Add ("X-mimeole", "produced by Microsoft mimeole v6.00.2900.2869 ");
Foreach (var bcc in BCCs ){
Mail. bcc. Add (BCC );
}
Foreach (var cc in CCS ){
Mail. CC. Add (CC );
}
String A = convert. tobase64string (system. Text. encoding. utf8.getbytes (subject ));
Mail. Subject = subject;
Mail. bodyencoding = system. Text. encoding. utf8;
Mail. Body = body;
Mail. isbodyhtml = isbodyhtml;
Foreach (string attachment in attachments ){
Attachment DATA = new attachment (Attachment );
Mail. attachments. Add (data );
}

Smtpclient client = new smtpclient (server );

Client. sendcompleted + = new sendcompletedeventhandler (client_sendcompleted );

Client. Timeout = timeout * 1000;
Client. deliverymethod = smtpdeliverymethod. Network;
If (needauthenticate ){
Client. usedefacrecredentials = false;
Client. Credentials = new networkcredential (username, password );
}
Else {
Client. usedefacrecredentials = true;
}
Client. Send (Mail );

}

Void client_sendcompleted (Object sender, system. componentmodel. asynccompletedeventargs e ){
Asyncsendcompleted (sender, e );
}
# Endregion

 

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.