分享3段平时很实用的微代码
一。发送电子邮件
用的dll是微软自带的,觉得挺好用的!!
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;//邮件服务器 比如 网易的是 smtp.163.COM
client.Port = 25;//端口号,也可不写
client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式
client.Credentials = new NetworkCredential(_UserID, _UserPsw);//用户名和密码
MailMessage myMessage = new MailMessage();
myMessage.Priority = MailPriority.Normal;//优先级
myMessage.From = new MailAddress(from, fromDisplay, Encoding.GetEncoding("gb2312"));
myMessage.To.Add(sendTo);
if (cc != "")
{
myMessage.CC.Add(cc);
}
myMessage.Subject = subject;//邮件主题
myMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
myMessage.IsBodyHtml = true;
myMessage.BodyEncoding = Encoding.GetEncoding("gb2312");
myMessage.Body = message;//正文
myMessage.Attachments.Add(new Attachment(@"C:/Users/lando/Desktop/Flex问题集结号.txt"));//加入附件。。。
client.Send(myMessage);//开始发送。
return ret;
}
}
页面调用:
SQ.FrameWork.SimpleEmailHelper emailHelper = new SQ.FrameWork.SimpleEmailHelper(stmpServerIpAddress, userId, psw);
emailHelper.Send(from, distEmailAddress, TextBoxTopic.Text.Trim(),TextBoxContent.Text.Trim(),txtCCCleint.Text);
ShowMessage("邮件发送成功。");
需要注意一下的是:
stmpServerIpAddress:是收邮件的服务器地址,比如我用网易的,那么就是 smtp.163.com 等等
userId:你发电子邮件的用户名
psw:你发电子邮件的密码
from:发送人姓名
distEmailAddress:收件人列表,可以有多个,用逗号分隔开来。。都很好理解!~。
二。下载word文档
这个很常用吧,这是我刚刚在给一个实习生改毕业论文的时候,遇到的,所以就记下来了!~以飨园友们哦!~。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string courseName = ((Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("Label1")).Text.ToString();//在GridView中文件名字
string time = ((Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("Label2")).Text.ToString();//在GridView中找时间
string tempPath = BusyworkManage.Path + tm.ReturnTeacherID(Request.Cookies["StudentID"].Value.ToString()) +BusyworkManage.TopicPath +
courseName + "/" + courseName + "_" + time + ".doc";//这样做是为了不使下载后的文件的名字重复!~~。
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();
首先需要说明的是,在gridview控件中放一个button 按钮,如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="139px"
Width="100%" OnRowDeleting="GridView1_RowDeleting"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
。。。。。。省略代码。。。。。
<asp:BoundField DataField="成绩" HeaderText="成绩">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:CommandField ButtonType="Button" DeleteText="下作业载" ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:HyperLinkField Text="提交作业" DataNavigateUrlFormatString="SubmitBusywork.aspx?course={0}&time={1}" DataNavigateUrlFields="课程名称,作业次数">
<ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
</Columns>
</asp:GridView>
然后,就可以了啦!~
三。DIV的绝对居中
很多时候,我们需要在浏览器中让一个div居中进行显示,而不会受到滚动条的影响,那怎么才能得到效果?其实,很简单,你需要理解下面这段就可以了。
最前端开人郁闷的就是浏览器的兼容性问题,所以在下面的代码中通过各浏览器的特有属性,来进行判断浏览器的类型。
比如说,self.pageYOffset 如果它为true的话,那么它说明在IE9中起作用,也说明了这个属性在IE9中是独一无二的。
直接看代码:
<script type='text/javascript'>
function myPopupRelocate() {
var scrolledX, scrolledY;
if (self.pageYOffset) {//IE9 起作用
scrolledX = self.pageXOffset;
scrolledY = self.pageYOffset;
alert("self.pageYOffset");
} else if (document.documentElement && document.documentElement.scrollTop) {// IE 6 ,360浏览器等起作用
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
alert("document.documentElement && document.documentElement.scrollTop");
} else if (document.body) {//Chrome... IE9 Firfox....IE 5.5起作用 www.2cto.com
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
alert("document.body");
}
//以上是浏览器滚动的距离
// alert("scrolledX:" + scrolledX);
// alert("scrolledY:" + scrolledY);
var centerX, centerY;
if (self.innerHeight) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.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>
我现在的位置是居中状态<br>
</p>
<input type='submit' value=' 关闭窗口! (2) ' onclick='document.getElementById("mypopup").style.display="none"'>
</div>
<input type='submit' value=' Fire! (2) ' onclick='fireMyPopup()'>
这就能得到在各个浏览器中绝对居中了。当然还有其他的方法,比如说 用css,也OK。
特殊情况:
如果,你不需要居中肿么办呢?很简单啊,你需要改的仅仅是下面这句话:
var leftOffset = scrolledX + (centerX - 250) / 2;
var topOffset = scrolledY + (centerY - 200) / 2;
比如说,你现在把当前的div放到top 100px,left 100px,就需要下面操作:
var leftOffset = scrolledX + 100;
var topOffset = scrolledY + 100;
好了,就先说这么这三个把!。祝大家 五一快乐哦!!~~
摘自 浪迹天涯