vagerent的asp.net开发笔记(二)

=======================================
css样式中border: solid 1px;与margin: 1px;联合使用才真正为1px
========================================
“~”表示当前虚拟目录
如:有虚拟目录abc    /abc
在abc中需要表示abc下def目录中的aa文件
可以像这样表示:
~/def/aa
包含以上代码的文件存于abc目录中。
如果没有“~”,变成/def/aa
则表示为根目录下的def目录中的aa文件。
========================================
//js执行本地程序
<script>
function exec (command) {
    window.oldOnError = window.onerror;
    window._command = command;
    window.onerror = function (err) {
      if (err.indexOf('utomation') != -1) {
        alert('命令' + window._command + ' 已经被用户禁止!');
        return true;
      }
      else return false;
    };
    var wsh = new ActiveXObject('WScript.Shell');
    if (wsh)
      wsh.Run(command);
    window.onerror = window.oldOnError;
  }
</script>

<input type=button onclick="exec('notepad')" value=执行>
=============================================================
请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
答:
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
======================================================
datalist删除项
if (e.CommandName == "Delete")//自然也可以if (e.CommandName == "a")
{
   string sID = dataLst.DataKeys[e.Item.ItemIndex].ToString();//如果DataKeyFiled=id,则可以获取id
   string sSql = "delete from PingLun where id=" + sID;
   DB.ExecuteNonQuery(sSql);
   dataLst.DataBind();
}
=====================================================
Response.write('sth');
Response.End();
=====================================================
#region 防止恶意刷新
  private bool CheckRefresh()
  {
   if (Request.Cookies["LastSub"] != null)
    return true;
   else
    return false;
  }
  private void AntiRefresh()
  {
   HttpCookie cookie = new HttpCookie("LastSub");
   cookie.Expires = DateTime.Now.AddSeconds(60);
   Response.Cookies.Add(cookie);
  }
#endregion
=======================================================
#region 发送email
    System.Web.Mail.MailMessage  mail  =  new  MailMessage();
    mail.To  = toMail;
    mail.Cc = ccMail;//抄送
    mail.From= ConfigurationSettings.AppSettings["from"];
    mail.From = "\"和讯(mis)\" <"+ConfigurationSettings.AppSettings["from"]+">";
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",  "1"); //basic  authentication
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",ConfigurationSettings.AppSettings["usr"]);  //set  your  username  here
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",ConfigurationSettings.AppSettings["psw"]); //set  your  password  here

    //设置主题
    mail.Subject  =  strSubject;
    //设置内容格式
    mail.BodyFormat = MailFormat.Html;
    //设置内容
    mail.Body  = strText;
    //设置邮件服务器地址,发送邮件
    SmtpMail.SmtpServer= "staff.hexun.com";
    SmtpMail.Send(mail);

  #endregion
================================================================
//记录在线人数,在global中
  protected void Session_Start(Object sender, EventArgs e)
  {
   if (Application["OnLine"] == null)
   {
    Application["OnLine"] = 0;
   }
   Application["OnLine"] = Convert.ToInt32(Application["OnLine"]) + 1;
  }
---------------------------------------------

  protected void Session_End(Object sender, EventArgs e)
  {
   if (Application["OnLine"] != null && Convert.ToInt32(Application["OnLine"]) > 0)
   {
    Application["OnLine"] = Convert.ToInt32(Application["OnLine"]) - 1;
   }
  }
==========================================================
#region 执行 Url 重写
 private void UrlRewriting()
 {
  OldUrlPath = "(.*)/([cn])(\\d+)(p?)(\\d*)\\.aspx";
  Expressions = new Regex(OldUrlPath, RegexOptions.IgnoreCase);
  if (Expressions.IsMatch(Request.Url.ToString()))
  {
   ReWriteUrl = Expressions.Replace(Request.Url.ToString(), (Expressions.Match(Request.Url.ToString()).Groups[2].Value.ToLower() == "c" ? "newslist.aspx?id=$3" : "shownews.aspx?id=$3") + (Expressions.Match(Request.Url.ToString()).Groups[4].Value.ToLower() == "p" ? "&page=$5" : null));
   Context.RewritePath(ReWriteUrl);
  }
 }
 #endregion

 

//将aa.aspx/ad解释为aa.aspx?id=ad
  public string GetCategory()
  {
   if (Request.PathInfo.Length ==0)
    return "";
   else
    return Request.PathInfo.Substring(1);
  }

======================================================
如果一个Page使用了一个MasterPage,2者之间事件的执行顺序如下:

MasterPage控件 Init 事件。

Page控件 Init 事件。

Page Init 事件。

Page Load 事件。

MasterPage Load 事件。

Page控件 Load 事件。

Page PreRender 事件。

MasterPage PreRender 事件。

MasterPage控件 PreRender 事件。

Page控件 PreRender 事件。

以上内容可以在MSDN上查找到。补充一点:New函数的执行顺序是 Page.New() --> MasterPage.New()
=====================================================
referURL可以判断url是否是通过search来的(即判断前一个url)
=====================================================
设置body的font-size并不可以改变全页的字体大小,可以这样
<style>
 a,span {font-size:12px;}
</style>
或者------------------------------------------
<style>
 body span {font-size:12px;}
</style>
其中,表示并列,空格表示下属
=====================================================
sqlServer中用convert获取日期部分:
convert(varchar(10),getdate(),120)
=======================================================
colspan:合并一行的两个或多个单元格,rowspan:合并一列中两个或多个单元格

colspan单元格的行跨度
默认为1
你可以指定一个单元格<TD rowspan=2>
这样它就会占据表格两行的高度
-------------------------------------------------------
footer中显示统计:
itemdatabound中,共3列:
if (e.Item.ItemType == ListItemType.Footer)
{
 e.Item.Cells[0].ColumnSpan = 2;
 e.Item.Cells[2].Visible = false;
 e.Item.Cells[0].Text = "登录数之和:";
 e.Item.Cells[1].Text = GetLoginCount();
}
-------------------------------------------------------
合并DataGrid单元格
  做项目有时也会遇到要合并DataGrid单元格的情况。在DataGrid单元格中有ColumnSpan、RowSpan、Visible属性,这跟Table控件中的colspan、rowspan属性相同,因此我们可以利用这些属性来合并DataGrid单元格。以下两个合并单元格的函数,是我在项目中实际用到的。说明:prevDg为DataGrid的变量。
void ColSpan()//合并列单元格
  {
   for(int i=0;i<prevDg.Items.Count;i++)
   {
    if (prevDg.Items[i].Cells[3].Text.Trim()=="0" && prevDg.Items[i].Cells[4].Text.Trim()=="0" )
    {
     prevDg.Items[i].Cells[2].ColumnSpan=3;
     prevDg.Items[i].Cells[3].Visible=false;
     prevDg.Items[i].Cells[4].Visible=false;
    }
   }
  }
  void RowSpan()//合并行单元格
  {
   int j;
   int n;
   for(int i=0;i<prevDg.Items.Count;i++)
   {
    n=1;
    for (j=i+1;j<prevDg.Items.Count;j++)
    {
     if(prevDg.Items[i].Cells[0].Text.Trim()==prevDg.Items[j].Cells[0].Text.Trim() && prevDg.Items[i].Cells[7].Text.Trim()==prevDg.Items[j].Cells[7].Text.Trim())
     {
      n += 1;
      prevDg.Items[i].Cells[0].RowSpan=n;
      prevDg.Items[j].Cells[0].Visible=false;
     }
     else break;
    }
    i=j-1;
   }              
  }
========================================================
按周统计:
select sum(销售金额), datename(week, 销售日期-1)
from sales where 销售日期 betwee begindate and enddate
group by datename(week, 销售日期-1)

注意:这里之所以要把销售日期-1是因为sql server默认的一周的第一天是星期天,
而我们习惯的统计是以星期一到星期天计算的,所以减一。
========================================================
//输入某年的第几周,返回这周的开始日期
//根据第几周得到这一周的起始日期和结束日期 c#
private DateTime GetnWeekDate(int iYear,int iWeek)

 DateTime dt = new DateTime(iYear,1,1);  
 dt = dt + new TimeSpan((iWeek-1)*7,0,0,0);  
 return dt.AddDays(-(int)dt.DayOfWeek+(int)DayOfWeek.Monday);  
}
========================================================
ie和firefox兼容性:

1.ie和firefox都支持的写法:document.frmTest.aaa不要用document.all
2.如果是链接点开的web页或者open开的,则window.close()在firefox支持,如果地址栏直接输的url,则不支持
3.js里面没有trim,下面是自己写的
  function trimStr(str) {
   var re = /\s*(\S[^\0]*\S)\s*/;
   re.exec(str);
   return RegExp.$1;
  }//trim函数
不如下面的好用:
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}
//调用时ss.trim();
=========================================================
getFullYear();//ie和firefox都支持的获取年份函数
getYear()在firefox里面获取的是当前年份减去1900,比如108为2008年

=========================================================
放大缩小textarea:(对firefox无效)

function zoomtextarea(objname, zoom) {
var zoomsize = zoom ? 60 : -60;
var obj = document.getElementById(objname);
obj.style.width = parseInt(obj.style.width)+zoomsize;
obj.style.height = parseInt(obj.style.height)+zoomsize;
}

调用,放大onclick="zoomtextarea('txb_allJS', 1)" 缩小onclick="zoomtextarea('txb_allJS', 0)"

放大缩小textarea:(兼容ie和firefox)不过如果设置了height和width属性则rows和cols对textarea无效
  <script language="javascript">
  function zoomtextarea(objname, zoom)
  {
   var zoomsize = zoom ? 10 : -10;
   var obj = document.getElementById(objname);
   //obj.setAttribute('height',600+zoomsize);
   if(obj.rows + zoomsize > 0 && obj.cols + zoomsize * 2 > 0)
   {
    obj.rows += zoomsize;
    obj.cols += zoomsize * 2;
   }
  }  
  
  </script>
========================================================
vs2003的treeview添加提示tootip
rootNode.Text = "<span title='"+datareader.GetString(4)+"'>"+ datareader.GetString(3)+"</span>";
它的text是可以加html的
========================================================
<DIV STYLE="width: 120px; height: 50px; border: 1px solid blue;
            overflow: hidden; text-overflow:ellipsis">
<NOBR>就是比如有一行文字,很长,表格内一行显示不下.</NOBR>
</DIV>
========================================================

posted on 2007-06-29 09:18  上午的绝缘杯  阅读(960)  评论(0编辑  收藏  举报