随笔-4  评论-0  文章-2  trackbacks-0
  2010年9月28日

在.aspx的文件中经常会碰到如下的代码,如:

1、<%=%> 里面放的变量名,如:

<div>
<h1>Hello World</h1>
<p>Welcome to Beginning ASP.NET 3.5 on <%=
DateTime.Now.ToString() %>
</p>
</div>
输出结果为:

Hello World

Welcome to Beginning ASP.NET 3.5 on 2009-11-10 15:53:08

2、 <%#%> 这里是数据的绑定

:<%# DataBinder.(Container.DataItem, "ClassName") %>

完整代码:  <asp:DataList ID="ClassList" runat="server">
<ItemTemplate> <%# DataBinder.(Container.DataItem, "ClassName") %>
</ItemTemplate>
</asp:DataList></td>

3、<%@ %> 表示:引用

如在很多.aspx页面中,都可以看到如下的代码:

<%@ Page Language="C#"   CodeFile="Default.aspx.cs" Inherits="_Default" %>

4、<%%>中间一般放函数或者方法,典型的asp程序写法。

例如:<tr bgcolor="#ffffff">
<td height="20">
<div align="center">类别:</div>
</td>
<td height="9">&nbsp;
<%ST_getList();%>
</td>
</tr>

posted @ 2010-09-28 11:24 雪夜 阅读(158) 评论(0) 编辑
  2010年4月11日
方法1、你写一个方法,到页面上:
StrLength 这个方法就是处理字符串的,写到cs文件中。
protected string StrLength (string str)
{
if (str.Length > 10)
{
return str.Substring(0, 10) + "...";
}
else
{
return str;
}

}
GridView 中用模板列
<asp:TemplateField>
<ItemTemplate>
<%# StrLength(DataBinder.Eval(Container.DataItem,"title").ToString())%>
</ItemTemplate>
</asp:TemplateField>
方法2、 用css。

方法3、在数据库里面直接栽掉...
select case when len(field)> 10 then left(field,10)+'...' else field end as news_name,news_id from tablename
方法4、 在RowDataBound方法中:
如果是BoundField,则:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text.Length > 10)
{
e.Row.Cells[2].Text = e.Row.Cells[2].Text.Substring(0, 10) + "...";
}
else
{
e.Row.Cells[2].Text=e.Row.Cells[2].Text
}
}
}
如果是TemplateField,则:
方法5、 如果是 TemplateField 列,可以直接帮定时操作
<asp:TemplateField HeaderText="Hehe">
<ItemTemplate>
<asp:Label runat="server" Text= <%#Eval("job_id").ToString().Substring(0,10)+"..."%> >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>

protected void GridView1_RowDataBound ( object sender, GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
if ( ( ( Label ) e.Row.Cells [    2   ].FindControl ( "Hehe" ) ).Text   .Length >    10 )
{
( ( Label ) e.Row.Cells [   2   ].FindControl ( "Hehe" ) ).Text = ( ( Label ) e.Row.Cells [    2   ].FindControl ( "Hehe" ) ).Text.Substring ( 0, 10 ) + "...";
}

posted @ 2010-04-11 14:14 雪夜 阅读(114) 评论(0) 编辑
  2010年4月2日

因為覺得每次要用條件式查詢很煩(如範例一),就想有沒有更方便的用法,試出一種方法還不錯,分享給大家。

測試環境

DBMS:Sql Server 2008 Express

DB:AdventureWorks

Table:Sales.SalesOrderDetail(Count:121,317)

image

 

情境

對Sales.SalesOrderDetail的OrderId與ProduceId與SaleDate這三個欄位做查詢(如圖一),如果沒有輸入條件為顯示全部。

 image

圖一 查詢畫面

 

範例一 傳統的條件式

   1:  StringBuilder sb = new StringBuilder();
   2:   
   3:  if (!string.IsNullOrEmpty(Order.Text))
   4:      sb.AppendFormat("SalesOrderID='{0}'", Order.Text);
   5:   
   6:  if (!string.IsNullOrEmpty(Produce.Text))
   7:  {
   8:      if (sb.Length > 0)
   9:          sb.Append(" AND ");
  10:   
  11:      sb.AppendFormat("ProductID='{0}'", Produce.Text);
  12:  }
  13:   
  14:  if (!string.IsNullOrEmpty(StartDate.Text))
  15:  {
  16:      if (sb.Length > 0)
  17:          sb.Append(" AND ");
  18:   
  19:      sb.AppendFormat("ModifiedDate > '{0}'", StartDate.Text);
  20:  }
  21:   
  22:  if (!string.IsNullOrEmpty(EndDate.Text))
  23:  {
  24:      if (sb.Length > 0)
  25:          sb.Append(" AND ");
  26:   
  27:      sb.AppendFormat("ModifiedDate < '{0}'", EndDate.Text);
  28:  }
  29:   
  30:  if (sb.Length > 0)
  31:      sb.Insert(0, " WHERE ");
  32:   
  33:  sb.Insert(0,"SELECT * FROM Sales.SalesOrderDetail");

如果是以前的我,我會這樣寫,落落長看的都累了,下一個範例展表直接在SQL中作掉。

 

範例二 更方便的寫法

   1:  string sql = string.Format("SELECT * FROM Sales.SalesOrderDetail WHERE" +
   2:                             "('{0}' = '' OR SalesOrderID = '{0}') AND " +
   3:                             "('{1}' = '' OR ProductID = '{1}') AND " +
   4:                             "('{2}' = '' OR ModifiedDate > '{2}') AND " +
   5:                             "('{3}' = '' OR ModifiedDate < '{3}')",
   6:                             Order.Text, Produce.Text, StartDate.Text, EndDate.Text);

這個方法是不是寫起來簡單,看起來乾淨,其作用為('' = '' OR SalesOrderID = '')參數是空的不查詢,('12345' = '' OR SalesOrderID = '12345')參數不是空的執行查詢。

 

應用一 SqlDataSource中使用

   1:  <asp:SqlDataSource ID="SqlDataSource" runat="server" CancelSelectOnNullParameter="false"
   2:  SelectCommand="SELECT * FROM Sales.SalesOrderDetail WHERE
   3:                (@Order IS NULL OR SalesOrderID = @Order) AND 
   4:                (@Produce IS NULL OR ProductID = @Produce) AND 
   5:                (@StartDate IS NULL OR ModifiedDate > @StartDate) AND 
   6:                (@EndDate IS NULL OR ModifiedDate < @EndDate)">
   7:  <SelectParameters>
   8:      <asp:ControlParameter ControlID="Order" Name="Order" ConvertEmptyStringToNull="true" />
   9:      <asp:ControlParameter ControlID="Produce" Name="Produce" ConvertEmptyStringToNull="true"/>
  10:      <asp:ControlParameter ControlID="StartDate" Name="StartDate" ConvertEmptyStringToNull="true"/>
  11:      <asp:ControlParameter ControlID="EndDate" Name="EndDate" ConvertEmptyStringToNull="true"/>
  12:  </SelectParameters>
  13:  </asp:SqlDataSource>

用這個方式,在DataSource中也可以做到DataSource條件式查詢,使用CancelSelectOnNullParameter=”false”,不管有沒有結果都要執行,因為設定ConvertEmptyStringToNull="true",所以SQL中也可以使用(@Order IS NULL OR SalesOrderID = @Order)

 

應用二 StoreProcedure中使用

   1:  CREATE PROCEDURE SearchSalesOrderDetail
   2:      @Order varchar(10) = null ,
   3:      @Produce varchar(10) = null,
   4:      @StartDate datetime = null,
   5:      @EndDate datetime = null
   6:  AS
   7:  BEGIN
   8:  SELECT * FROM Sales.SalesOrderDetail WHERE
   9:  (@Order IS NULL OR SalesOrderID = @Order) AND 
  10:  (@Produce IS NULL OR ProductID = @Produce) AND 
  11:  (@StartDate IS NULL OR ModifiedDate > @StartDate) AND 
  12:  (@EndDate IS NULL OR ModifiedDate < @EndDate)
  13:  END
  14:  GO

當然也可以寫成Store Procedure直接在資料庫操作。

 

效能測試

各位最擔心的還是效能,就測試結果來說效能比較差,不過這樣物件導向是一樣的,為了方便多少會犧生一些效能,能不能接受,就看各位了。

 

EXEC dbo.SearchSalesOrderDetail NULL,NULL,NULL,NULL

 image

 

 

 

SELECT * FROM Sales.SalesOrderDetail

 image

 

EXEC dbo.SearchSalesOrderDetail '43659','776'

image

 

SELECT * FROM Sales.SalesOrderDetail WHERE SalesOrderID = '43659' AND ProductID = '776'

 

 image

posted @ 2010-04-02 08:28 雪夜 阅读(76) 评论(0) 编辑
  2008年4月3日
2008年4月2号~
posted @ 2008-04-03 17:10 雪夜 阅读(23) 评论(0) 编辑
昵称:雪夜
园龄:4年8个月
粉丝:1
关注:3
<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

搜索

 
 

常用链接

随笔分类

随笔档案

文章分类

最新评论