Share 3 useful microcodes

Source: Internet
Author: User

I. Send email
The dll is provided by Microsoft. It is quite useful !!
 
Public class SimpleEmailHelper
{
Private string _ SmtpAdd;
Private string _ UserID;
Private string _ UserPsw;

Public SimpleEmailHelper (string smtpAddress, string userID, string userPsw)
{
_ SmtpAdd = smtpAddress;
_ UserID = userID;
_ UserPsw = userPsw;
}

Public bool Send (string from, string to, string subject, string message, string cc)
{
Return Send (from, from, to, to, subject, message, cc );
}

Public bool Send (string from, string fromDisplay, string sendTo, string sendToDisplay, string subject, string message, string cc)
{

Bool ret = true;

SmtpClient client = new SmtpClient ();
Client. Host = _ SmtpAdd; // the mail server, for example, Netease, is smtp.163.COM.
Client. Port = 25; // Port number. Do not write
Client. DeliveryMethod = SmtpDeliveryMethod. Network; // sending Method
Client. Credentials = new NetworkCredential (_ UserID, _ UserPsw); // user name and password

MailMessage myMessage = new MailMessage ();
MyMessage. Priority = MailPriority. Normal; // Priority
MyMessage. From = new MailAddress (from, fromDisplay, Encoding. GetEncoding ("gb2312 "));
MyMessage. To. Add (sendTo );
If (cc! = "")
{
MyMessage. CC. Add (cc );
}
MyMessage. Subject = subject; // mail Subject
MyMessage. SubjectEncoding = Encoding. GetEncoding ("gb2312 ");
MyMessage. IsBodyHtml = true;
MyMessage. BodyEncoding = Encoding. GetEncoding ("gb2312 ");
MyMessage. Body = message; // Body
MyMessage. Attachments. Add (new Attachment (@ "C: \ Users \ Desktop \ flexquestion set node. txt"); // Add the Attachment...
Client. Send (myMessage); // start sending.
Return ret;
}
}
 
Page call:
SQ. FrameWork. SimpleEmailHelper emailHelper = new SQ. FrameWork. SimpleEmailHelper (stmpServerIpAddress, userId, psw );
EmailHelper. Send (from, distEmailAddress, TextBoxTopic. Text. Trim (), TextBoxContent. Text. Trim (), txtCCCleint. Text );
ShowMessage ("the email is sent successfully. ");
Note that:
StmpServerIpAddress: the address of the receiving server. For example, if I use Netease, It is smtp.163.com.
UserId: Your Email user name
Psw: Your Email Password
From: sender name
DistEmailAddress: list of recipients, which can be separated by commas .. It's easy to understand !~.
II. Download Word documents
This is very common. This is what I met when I was modifying my graduation thesis for an intern, so I wrote it down !~ Thank you !~.
 
Protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
String courseName = (Label) GridView1.Rows [e. RowIndex]. Cells [1]. FindControl ("Label1"). Text. ToString (); // name of the file in the GridView
String time = (Label) GridView1.Rows [e. RowIndex]. Cells [2]. FindControl ("Label2"). Text. ToString (); // locate the time in the GridView
String tempPath = BusyworkManage. Path + tm. ReturnTeacherID (Request. Cookies ["StudentID"]. Value. ToString () + BusyworkManage. TopicPath +
CourseName + "/" + courseName + "_" + time + ". doc"; // This is done so that the names of the downloaded files are not repeated !~~.
String path = Server. MapPath (tempPath );
FileInfo fInfo = new FileInfo (path );
String fname = fInfo. Name;
Response. Clear ();
Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fname ));
Response. AddHeader ("Content-Length", fInfo. Length. ToString ());
Response. ContentType = "application/octet-stream ";
Response. WriteFile (fInfo. FullName );
Response. Flush ();
 
The first thing to note is to put a button in the gridview control, as shown below:
 
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" Height = "139px"
Width = "100%" OnRowDeleting = "GridView1_RowDeleting"
Onselectedindexchanged = "GridView1_SelectedIndexChanged">
<Columns>
...... Omitting code .....
<Asp: BoundField DataField = "score" HeaderText = "score">
<ItemStyle HorizontalAlign = "Center"/>
</Asp: BoundField>
<Asp: CommandField ButtonType = "Button" DeleteText = "etext =" "ShowDeleteButton =" True ">
<ItemStyle HorizontalAlign = "Center"/>
</Asp: CommandField>
<Asp: HyperLinkField Text = "submit job" DataNavigateUrlFormatString = "SubmitBusywork. aspx? Course = {0} & amp; time = {1} "DataNavigateUrlFields =" course name, number of assignments ">
<ItemStyle HorizontalAlign = "Center"/>
</Asp: HyperLinkField>
</Columns>
</Asp: GridView>
 
Then, you can !~
3. Absolute center of DIV
Most of the time, we need to center a div in the browser for display without being affected by the scroll bar. How can we achieve the effect? In fact, it is very simple. You need to understand the following section.
The most depressing front-end is the compatibility of browsers. Therefore, the following code uses the special attributes of each browser to determine the browser type.
For example, if self. pageYOffset is true, it indicates that it plays a role in IE9 and that this attribute is unique in IE9.
View the Code directly:
 
<Script type = 'text/javascript '>
Function myPopupRelocate (){
Var scrolledX, scrolledY;
If (self. pageYOffset) {// IE9 takes effect
ScrolledX = self. pageXOffset;
ScrolledY = self. pageYOffset;
Alert ("self. pageYOffset ");
} Else if (document.doc umentElement & document.doc umentElement. scrollTop) {// IE 6,360 browser, etc.
ScrolledX = document.doc umentElement. scrollLeft;
ScrolledY = document.doc umentElement. scrollTop;
Alert ("document.doc umentElement & document.doc umentElement. scrollTop ");
} Else if (document. body) {// Chrome... IE9 Firfox... IE 5.5 works at www.2cto.com
ScrolledX = document. body. scrollLeft;
ScrolledY = document. body. scrollTop;
Alert ("document. body ");
}
// The above is the browser's rolling distance
// Alert ("scrolledX:" + scrolledX );
// Alert ("scrolledY:" + scrolledY );

Var centerX, centerY;
If (self. innerHeight ){
CenterX = self. innerWidth;
CenterY = self. innerHeight;
} Else if (document.doc umentElement & document.doc umentElement. clientHeight ){
CenterX = document.doc umentElement. clientWidth;
CenterY = document.doc umentElement. clientHeight;

} Else if (document. body ){
CenterX = document. body. clientWidth;
CenterY = document. body. clientHeight;
}

Alert ("centerX:" + centerX );
Alert ("centerY:" + centerY );

Var leftOffset = scrolledX + (centerX-250)/2;
Var topOffset = scrolledY + (centerY-200)/2;
Document. getElementById ("mypopup"). style. top = topOffset + "px ";
Document. getElementById ("mypopup"). style. left = leftOffset + "px ";
}
Function fireMyPopup (){
MyPopupRelocate ();
Document. getElementById ("mypopup"). style. display = "block ";
// Document. body. onscroll = myPopupRelocate;
// Window. onscroll = myPopupRelocate;
}
</Script>
 
HTML Code:
 
<Div id = 'mypopup' name = 'mypopup' style = 'position: absolute; width: 250px; height: 200px;
Display: none; background: # ddd; border: 1px solid #000; z-index: 100 '>
<P>
My current position is center <br>
</P>
<Input type = 'submit 'value = 'close the window! (2) 'onclick = 'document. getElementById ("mypopup"). style. display = "none" '>
</Div>
<Input type = 'submit 'value = 'Fire! (2) 'onclick = 'firemypopup () '>
 
In this way, the browser is absolutely centered. Of course there are other methods, such as using css and OK.
Special cases:
What if you don't need to center and swollen? It's easy. You only need to modify the following sentence:
Var leftOffset = scrolledX + (centerX-250)/2;
Var topOffset = scrolledY + (centerY-200)/2;
For example, if you put the current div in top 100px and left 100px, You need to perform the following operations:
Var leftOffset = scrolledX + 100;
Var topOffset = scrolledY + 100;

Well, let's talk about these three !. Happy May 1 !!~~

 

Excerpt from Longji Tianya

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.