Home >Backend Development >C#.Net Tutorial >Summary of commonly used codes in ASP.NET programs
///631fb227578dfffda61e1fa4d04b7d25
///Name: IsNumberic
///Function: Determine whether the input is a number
///Parameter: stringoText: source text
///Return value: booltrue: yes false: no
///039f3e95db2a684c7b74365531eb6044
publicboolIsNumberic(stringoText)
{
try
{
intvar1=Convert.ToInt32(oText);
returntrue;
}
catch
{
returnfalse;
}
}
get The actual length of the string (including Chinese characters)
//Get the actual length of the string oString
publicintStringLength(stringoString)
{
byte[]strArray=System.Text.Encoding.Default.GetBytes(oString);
intres=strArray .Length;
returnres;
}
42. Convert the carriage return to TAB
//当在有keydown事件的控件上敲回车时,变为tab publicvoidTab(System.Web.UI.WebControls.WebControlwebcontrol) { webcontrol.Attributes.Add("onkeydown","if(event.keyCode==13)event.keyCode=9"); }
43. If the index exceeds the deletion in datagrid paging
publicvoidjumppage(System.Web.UI.WebControls.DataGriddg) { intint_PageLess;//定义页面跳转的页数 //如果当前页是最后一页 if(dg.CurrentPageIndex==dg.PageCount-1) { //如果就只有一页 if(dg.CurrentPageIndex==0) { //删除后页面停在当前页 dg.CurrentPageIndex=dg.PageCount-1; } else { //如果最后一页只有一条记录 if((dg.Items.Count%dg.PageSize==1)||dg.PageSize==1) { //把最后一页最后一条记录删除后,页面应跳转到前一页 int_PageLess=2; } else//如果最后一页的记录数大于1,那么在最后一页删除记录后仍然停在当前页 { int_PageLess=1; } dg.CurrentPageIndex=dg.PageCount-int_PageLess; } } }
31. When the files are in different directories, you need to obtain the database connection character String (if the connection string is placed in Web.config, then initialized in Global.asax)
在Application_Start中添加以下代码: Application["ConnStr"]=this.Context.Request.PhysicalApplicationPath+ConfigurationSettings. AppSettings["ConnStr"].ToString();
32. Variable.ToString()
Convert character type to string
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. Variable.Substring (parameter 1, parameter 2);
Intercept a part of the string, parameter 1 is the starting digit on the left, parameter 2 is the number of digits to intercept. For example: string s1 = str.Substring(0,2);
34. Log in to other websites on your own website: (If your page is nested, because a page can only have one FORM, you can Direct to another page and submit login information)
<SCRIPTlanguage="javascript"> <!-- functiongook(pws) { frm.submit(); } //--> </SCRIPT><bodyleftMargin="0"topMargin="0"onload="javascript:gook()"marginwidth="0"marginheight="0"> <formname="frm"action="http://220.194.55.68:6080/login.php?retid=7259"method="post"> <tr> <td> <inputid="f_user"type="hidden"size="1"name="f_user"runat="server"> <inputid="f_domain"type="hidden"size="1"name="f_domain"runat="server"> <inputclass="box"id="f_pass"type="hidden"size="1"name="pwshow"runat="server"> <INPUTid="lng"type="hidden"maxLength="20"size="1"value="5"name="lng"> <INPUTid="tem"type="hidden"size="1"value="2"name="tem"> </td> </tr> </form>
The name of the text box must be the name on the web page you want to log in. If the source code does not work, you can use vsniffer to take a look.
The following is the code to obtain the login information entered by the user:
stringname; name=Request.QueryString["EmailName"]; try { inta=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. Warning window
///<summary> ///服务器端弹出alert对话框 ///</summary> ///<paramname="str_Message">提示信息,例子:"不能为空!"</param> ///<paramname="page">Page类</param> publicvoidAlert(stringstr_Message,Pagepage) { page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>"); }
36. Reload this warning window so that a certain control gets focus
///<summary> ///服务器端弹出alert对话框,并使控件获得焦点 ///</summary> ///<paramname="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param> ///<paramname="str_Message">提示信息,例子:"请输入您姓名!"</param> ///<paramname="page">Page类</param> publicvoidAlert(stringstr_Ctl_Name,stringstr_Message,Pagepage) { page.RegisterStartupScript("","<script>alert('"+str_Message+"');document.forms(0)."+str_Ctl_Name+".focus();document.forms(0)."+str_Ctl_Name+".select();</script>"); }
37. Confirmation dialog box
///<summary> ///服务器端弹出confirm对话框 ///</summary> ///<paramname="str_Message">提示信息,例子:"您是否确认删除!"</param> ///<paramname="btn">隐藏Botton按钮Id值,比如:btn_Flow</param> ///<paramname="page">Page类</param> publicvoidConfirm(stringstr_Message,stringbtn,Pagepage) { page.RegisterStartupScript("","<script>if(confirm('"+str_Message+"')==true){document.forms(0)."+btn+".click();}</script>"); }
38 .Reload the confirmation dialog box, click OK to trigger a hidden button event, click Cancel to trigger a hidden button event
///<summary> ///服务器端弹出confirm对话框,询问用户准备转向那些操作,包括“确定”和“取消”时的操作 ///</summary> ///<paramname="str_Message">提示信息,比如:"成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param> ///<paramname="btn_Redirect_Flow">"确定"按钮id值</param> ///<paramname="btn_Redirect_Self">"取消"按钮id值</param> ///<paramname="page">Page类</param> publicvoidConfirm(stringstr_Message,stringbtn_Redirect_Flow,stringbtn_Redirect_Self,Pagepage) { 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. Get focus
///<summary> ///使控件获得焦点 ///</summary> ///<paramname="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param> ///<paramname="page">Page类</param> publicvoidGetFocus(stringstr_Ctl_Name,Pagepage) { page.RegisterStartupScript("","<script>document.forms(0)."+str_Ctl_Name+".focus();document.forms(0)."+str_Ctl_Name+".select();</script>"); }
40. The subform returns to the main form
///<summary> ///名称:redirect ///功能:子窗体返回主窗体 ///参数:url ///返回值:空 ///</summary> publicvoidredirect(stringurl,Pagepage) { if(Session["IfDefault"]!=(object)"Default") { page.RegisterStartupScript("","<script>window.top.document.location.href='"+url+"';</script>"); } }
21. How to set global variables in
Global.asax
In the Application_Start() event
Add Application [attribute name] = xxx;
It is your global variable
22. How to make the connection generated by HyperLinkColumn, Click to connect and open a new window?
HyperLinkColumn has an attribute Target, just set the value to "_blank". (Target="_blank")
【ASPNETMENU】Click on the menu item to pop up a new window0c6dc11e160d3b678d68754cc175188a