<转载总结>总结一些常用的页面小技巧

  闲暇下来在网上看一些前辈们总结的一些常用技巧,看多了觉得自己有用的记录一些。

  1.点击按钮弹出确认提示  

View Code
   <asp:Button  ID="btn1" runat="server" Text="ButtonConfirm"/>
<asp:Button ID="btn2" runat="server" Text="ButtonCofirm"/>
View Code
  btn1.Attributes.Add("onclick","return confirm('确认?')");
btn2.Attributes.Add("onclick", "if(confirm('are you sure...?')){return true;}else{return false;}");

  2.日期格式

View Code
  <asp:BoundField HeaderText="Create Date" DataField="Create_Date" HeaderStyle-CssClass="gdv_pdmItemRight"
ItemStyle-CssClass
="gdv_pdmItemRight" DataFormatString ="{0: yyyy/M/d}" SortExpression="Create_Date">

<HeaderStyle CssClass="gdv_pdmItemRight"></HeaderStyle>
<ItemStyle CssClass="gdv_pdmItemRight"></ItemStyle>
</asp:BoundField>

  3.Panel横向滚动,纵向自动延伸

View Code
 <asp:Panel runat="server" Style="overflow-x: scroll; overflow-y: auto;">
<asp:Button ID="Button1" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button3" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button9" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button8" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button7" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button6" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button5" runat="server" Text="ButtonConfirm"/><br />
<asp:Button ID="Button4" runat="server" Text="ButtonConfirm"/><br />
asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadsfasdfasdfasdfasfdfasdfasdfasdfasdfasdfasdfas
</asp:Panel>

  4.打印网页

View Code
  <fieldset>
<legend>打印打印网页</legend><a href='javascript:window.print ()'>Print</a> 打印页面
<div align="center">
<a class="content" href="javascript:doPrint();">打印本稿</a></div>
</fieldset>

  5.页面的前进、后退、刷新

View Code
 <fieldset>
<legend>后退</legend>
<input type="button" onclick="window.history.back()" value="back"/>
</fieldset>

<fieldset>
<legend>前进</legend>
<input type="button" onclick="window.history.forward()" value="forward"/>
</fieldset>

<fieldset>
<legend>刷新</legend>
<input type="button" onclick="document.location.reload()" value="Reload"/>
</fieldset>

  6.窗口最小化

View Code
   <fieldset>
<legend>最小化窗口</legend>
<input type="button" onclick="window.blur()" value="Blur"/>
</fieldset>

  7.禁止打开输入法

View Code
 <fieldset>
<legend>禁止在输入框打开输入法</legend>
<input style="ime-mode: disabled">
</fieldset>

  8.JS判断字符串是否包含字符  

View Code
  function CheckIsExist() {
var strA = "ABCDEFG";
var strB = "AB";

if (strA.indexOf(strB) <0) {
alert("False");
}
else {

alert("True" + strA.indexOf(strB));
}
}

  9.设置收藏夹、主页

View Code
  //设置为收藏
function addFav() {

if (ie)
window.external.AddFavorite(location.href, 'WWW.OGRISH.COM : GROTESQUE MOVIES AND PICTURES');
if (ns)
alert("Thanks for the bookmark!\n\nNetscape users click OK then press CTRL-D");
}

//设置为主页
function makeHome() {

netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");
navigator.preference("browser.startup.homepage", location.href);
}

  10.使用StopWatch检测程序段运行时间间隔

View Code
  Stopwatch timer = new Stopwatch();

timer = new Stopwatch();
timer.Start();

List<TestClass> list = new List<TestClass>();
for (int i = 0; i < 10000000; i++)
{
TestClass aa = new TestClass();
aa.Name = "测试数据" + i;
aa.ID = i;
list.Add(aa);
}
timer.Stop();
Console.WriteLine("循环耗时:"+ timer.Elapsed.Ticks);
Console.WriteLine("-------------------------------------------");
Console.ReadLine();

  11.读文件成流,并转成Base64的String

View Code
using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
myString += Convert.ToBase64String(bin);
}
}

  12.禁止通过右键菜单查看源文件

View Code
    <script language="JavaScript" type="text/javascript">
document.oncontextmenu = new Function("alert('Permit!');event.returnValue=false;");
</script>

  13. 刷新父页面,并关闭当前窗口

View Code
  public static void CloseWindowAndRefreshParent()
{
string Script = "window.opener.location.href = window.opener.location.href; window.close();";
ScriptManager.RegisterClientScriptBlock(HttpContext.Current.Handler as Control, HttpContext.Current.Handler.GetType(), "", Script, true);
}

  14.跳转页面前提示确定

View Code
  public static void RedirectConfirm(string message, string redirectUrl)
{
string Script = "if(confirm('" + message + "')){location.href = '" + redirectUrl + "';}";


ScriptManager.RegisterClientScriptBlock(HttpContext.Current.Handler as Control, HttpContext.Current.Handler.GetType(), "", Script, true);

}

  


  以后看到陆续更新~~~~











posted @ 2011-11-07 19:21  liver.wang  阅读(215)  评论(0编辑  收藏  举报