MessageBoard总结(一)

1、 分层

UI:只放与数据、控件显示有关的代码,不做任何逻辑处理

BLL:放置与逻辑处理,计算相关的代码

DAL:放置与数据获取相关的代码,如DataSetORM

Security:放置与安全相关的代码,如权限处理、用户处理等

Utility:放置公用代码,如CommonFunctionConstant

2、 序号

repeater,datalist里面用 <%#Container.ItemIndex+1%>

GridView<%#Container.DataItemIndex+1 %>

3、 <%=  %><%#  %>

<%#  %>一般用于数据绑定

<%=  %>一般用于后台向前台数据传递和方法传递

4、 分页

PagedDataSource有自带的分页功能,所以Repeater,DataList等不带分页功能的Data控件可以以它为数据源写分页方法。

 

ContractedBlock.gif ExpandedBlockStart.gif Paging
 public void RepeaterPaging(Label lblCurrentPage, Label lblTotalPage, Repeater repeater, SqlDataSource sds, LinkButton lbtnPrev, LinkButton lbtnNext, LinkButton lbtnFirst, LinkButton lbtnLast)
        {
            PagedDataSource pds 
= new PagedDataSource();
            pds.AllowPaging 
= true;
            
//use sqldatasource as the datasource of pageddatasource
            pds.DataSource = sds.Select(new DataSourceSelectArguments());
            pds.PageSize 
= 10;
            lblTotalPage.Text 
= pds.PageCount.ToString();
            
int CurrentPage;
            
//this control can add page to the url by itself
            if ( HttpContext.Current.Request.QueryString["Page"!= null)
            {
                CurrentPage 
= Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"]);
            }
            
else
            {
                CurrentPage 
= 1;
            }
            
//pagedatasource's index begin with 0
            pds.CurrentPageIndex = CurrentPage - 1;
            lblCurrentPage.Text 
= CurrentPage.ToString();
            
if (!pds.IsFirstPage)
            {
                lbtnPrev.PostBackUrl 
= HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
            }
            
if (!pds.IsLastPage)
            {
                lbtnNext.PostBackUrl 
= HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
            }
            lbtnFirst.PostBackUrl 
= HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" +1;
            lbtnLast.PostBackUrl 
= HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + lblTotalPage.Text;
            repeater.DataSource 
= pds;
            repeater.DataBind();
        }

 

 

5、 上传图片到服务器

 

ContractedBlock.gif ExpandedBlockStart.gif insert picture
        public string InsertPicture(FileUpload fu,string src)
        {
            
string place =Convert.ToString(DateTime.Now.ToString("yyyyMMddhhmmss"))+ 
                Path.GetExtension(fu.PostedFile.FileName);
            
if (fu.HasFile)
            {
                fu.SaveAs(src 
+ place);
              
            }  
        }

其中,src要是服务器路径,对于我的系统来说

src=Server.MapPath("images") + \\banners\\

6、 解决lable显示时没回车

可以用Literal控件,它可以显示html编译后的结果因此只需要把/r/n转换为</br>就可以了。

因此对于图片的显示,也可以用5上传图片,然后在textbox中自动加上<image>标签。

7、 找到datakeyname

在RowDataBound中用gridview.DataKeys[e.Row.RowIndex].Value.ToString()

如果有多个datakeyname在Value后加["datakeyname"]

8、自定义参数

当数据控件中的linkbutton后台需要某个绑定的值时,可以用CommandArgument属性获得值后,在后台调用这个属性。

9、JQurey给控件赋值时一定要对客户端id

当用JQurey给控件赋值时先运行页面,再用浏览器查看代码,找到其客户端代码后在用。

function reply(a)
{
 $("#ctl00_ContentPlaceHolder1_txtTitle").attr("value","Reply To: "+a+" Floor");
}

10、密封类和静态属性的应用

ContractedBlock.gif ExpandedBlockStart.gif sealed
public sealed class CommonFunction
    {
        
private static readonly CommonFunction instance = new CommonFunction();

        
private CommonFunction() { }
        
public static CommonFunction Instance
        {
            
get
            {
                
return instance;
            }
        }
        
public string GetLoginName()
        {
         }
     }

其中用sealed修饰class是为了防止继承。代码前面一部分是将这个类变成静态变量,那么其中的方法就都成为了静态方法。

调用时就CommonFunction.Instance.GetLoginName()

11、日志文件

 string logAddress = string.Format("{0}Log.txt", Request.PhysicalApplicationPath);
        try
        {

         }
        catch (Exception ex)
        {
            File.WriteAllText(logAddress, ex.Message);
        }

在Log.txt中记录错误日志,同理可以记入数据库

转载于:https://www.cnblogs.com/liuweicfyj/archive/2009/08/18/1548676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值