ASP.NET程序中常用代码汇总(四)(转载)
31. 当文件在不同目录下,需要获取数据库连接字符串(如果连接字符串放在Web.config,然后在Global.asax中初始化)
在Application_Start中添加以下代码:
Application["ConnStr"]=this.Context.Request.PhysicalApplicationPath+ConfigurationSettings.
AppSettings["ConnStr"].ToString();
32.变量.ToString() AppSettings["ConnStr"].ToString();
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
33.变量.Substring(参数1,参数2); 12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
截取字串的一部分,参数1为左起始位数,参数2为截取几位。 如:string s1 = str.Substring(0,2);
34.在自己的网站上登陆其他网站:(如果你的页面是通过嵌套方式的话,因为一个页面只能有一个FORM,这时可以导向另外一个页面再提交登陆信息)
<SCRIPT language="javascript">
<!--
function gook(pws)
{
frm.submit();
}
//-->
</SCRIPT> <body leftMargin="0" topMargin="0" onload="javascript:gook()" marginwidth="0" marginheight="0">
<form name="frm" action=" http://220.194.55.68:6080/login.php?retid=7259 ;" method="post">
<tr>
<td>
<input id="f_user" type="hidden" size="1" name="f_user" runat="server">
<input id="f_domain" type="hidden" size="1" name="f_domain" runat="server">
<input class="box" id="f_pass" type="hidden" size="1" name="pwshow" runat="server">
<INPUT id="lng" type="hidden" maxLength="20" size="1" value="5" name="lng">
<INPUT id="tem" type="hidden" size="1" value="2" name="tem">
</td>
</tr>
</form>
文本框的名称必须是你要登陆的网页上的名称,如果源码不行可以用vsniffer 看看。 <!--
function gook(pws)
{
frm.submit();
}
//-->
</SCRIPT> <body leftMargin="0" topMargin="0" onload="javascript:gook()" marginwidth="0" marginheight="0">
<form name="frm" action=" http://220.194.55.68:6080/login.php?retid=7259 ;" method="post">
<tr>
<td>
<input id="f_user" type="hidden" size="1" name="f_user" runat="server">
<input id="f_domain" type="hidden" size="1" name="f_domain" runat="server">
<input class="box" id="f_pass" type="hidden" size="1" name="pwshow" runat="server">
<INPUT id="lng" type="hidden" maxLength="20" size="1" value="5" name="lng">
<INPUT id="tem" type="hidden" size="1" value="2" name="tem">
</td>
</tr>
</form>
下面是获取用户输入的登陆信息的代码:
string name;
name=Request.QueryString["EmailName"];
try
{
int a=name.IndexOf("@",0,name.Length);
f_user.Value=name.Substring(0,a);
f_domain.Value=name.Substring(a+1,name.Length-(a+1));
f_pass.Value=Request.QueryString["Psw"];
}
catch
{
Script.Alert("错误的邮箱!");
Server.Transfer("index.aspx");
}
35.警告窗口
name=Request.QueryString["EmailName"];
try
{
int a=name.IndexOf("@",0,name.Length);
f_user.Value=name.Substring(0,a);
f_domain.Value=name.Substring(a+1,name.Length-(a+1));
f_pass.Value=Request.QueryString["Psw"];
}
catch
{
Script.Alert("错误的邮箱!");
Server.Transfer("index.aspx");
}
/// <summary>
/// 服务器端弹出alert对话框
/// </summary>
/// <param name="str_Message">提示信息,例子:"不能为空!"</param>
/// <param name="page">Page类</param>
public void Alert(string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
}
36.重载此警告窗口,使某控件获得焦点/// 服务器端弹出alert对话框
/// </summary>
/// <param name="str_Message">提示信息,例子:"不能为空!"</param>
/// <param name="page">Page类</param>
public void Alert(string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
}
/// <summary>
/// 服务器端弹出alert对话框,并使控件获得焦点
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert(string str_Ctl_Name,string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
}
37.确认对话框 /// 服务器端弹出alert对话框,并使控件获得焦点
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public void Alert(string str_Ctl_Name,string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
}
/// <summary>
/// 服务器端弹出confirm对话框
/// </summary>
/// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
/// <param name="btn">隐藏Botton按钮Id值,比如:btn_Flow</param>
/// <param name="page">Page类</param>
public void Confirm(string str_Message,string btn,Page page)
{
page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn+".click();}</script>");
}
38.重载确认对话框,点击确定触发一个隐藏按钮事件,点击取消触发一个隐藏按钮事件/// 服务器端弹出confirm对话框
/// </summary>
/// <param name="str_Message">提示信息,例子:"您是否确认删除!"</param>
/// <param name="btn">隐藏Botton按钮Id值,比如:btn_Flow</param>
/// <param name="page">Page类</param>
public void Confirm(string str_Message,string btn,Page page)
{
page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn+".click();}</script>");
}
/// <summary>
/// 服务器端弹出confirm对话框,询问用户准备转向那些操作,包括“确定”和“取消”时的操作
/// </summary>
/// <param name="str_Message">提示信息,比如:"成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
/// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
/// <param name="btn_Redirect_Self">"取消"按钮id值</param>
/// <param name="page">Page类</param>
public void Confirm(string str_Message,string btn_Redirect_Flow,string btn_Redirect_Self,Page page)
{
page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn_Redirect_Flow+".click();}else{document.forms(0)."+btn_Redirect_Self+".click();}</script>");
}
39.获得焦点 /// 服务器端弹出confirm对话框,询问用户准备转向那些操作,包括“确定”和“取消”时的操作
/// </summary>
/// <param name="str_Message">提示信息,比如:"成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
/// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
/// <param name="btn_Redirect_Self">"取消"按钮id值</param>
/// <param name="page">Page类</param>
public void Confirm(string str_Message,string btn_Redirect_Flow,string btn_Redirect_Self,Page page)
{
page.RegisterStartupScript("","<script> if (confirm('"+str_Message+"')==true){document.forms(0)."+btn_Redirect_Flow+".click();}else{document.forms(0)."+btn_Redirect_Self+".click();}</script>");
}
/// <summary>
/// 使控件获得焦点
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="page">Page类</param>
public void GetFocus(string str_Ctl_Name,Page page)
{
page.RegisterStartupScript("","<script>document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
}
40.子窗体返回主窗体/// 使控件获得焦点
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="page">Page类</param>
public void GetFocus(string str_Ctl_Name,Page page)
{
page.RegisterStartupScript("","<script>document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
}
///<summary>
///名称:redirect
///功能:子窗体返回主窗体
///参数:url
///返回值:空
///</summary>
public void redirect(string url,Page page)
{
if ( Session["IfDefault"]!=(object)"Default")
{
page.RegisterStartupScript("","<script>window.top.document.location.href='"+url+"';</script>");
}
}
///名称:redirect
///功能:子窗体返回主窗体
///参数:url
///返回值:空
///</summary>
public void redirect(string url,Page page)
{
if ( Session["IfDefault"]!=(object)"Default")
{
page.RegisterStartupScript("","<script>window.top.document.location.href='"+url+"';</script>");
}
}