某个偏技术的内向男的一个分析代码,无限YY的小窝。
  • 我是一个程序猿,啦啦啦

    2011-09-28

    爱生活,爱网络,
    爱代码,爱加班,也爱自拍,
    爱家人,爱朋友,也爱Debug,
    爱真诚,不爱虚伪,爱付出,不爱不劳而获。
    爱三块五一个的鸡蛋灌饼,更爱一块五一袋的雀巢咖啡,
    爱晚睡,也爱工作到深夜,
    我痴迷技术,崇上简洁和完美,
    我低调而孤僻,沉默但爱憎分明,
    我没什么亮点,我就是焦点,
    我不是什么工程师,我是一个程序员,我和你一样,为了理想而奋斗。

    作者:天南一隅 | 分类目录:代码乐趣程序人生 | 标签:
  • 自己琢磨的一个Gridview的分页

    2011-06-17

    本分页实现没使用Gridview的自带分页,因为我很懒,自己根据公司现有的情况写了一个,很烂,是个回发分页,想看url分页的就当打酱油了,贴上来求大牛指点一下,我还停留在代码的阶段,泪奔…

    数据库有一个获取数据的存储过程,我感觉写的很好,我的这个自定义分页完全依托与这个存储过程。

    先上存储过程

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    
    create   PROCEDURE [dbo].[sp_PageData]
    	@TableName NVARCHAR(50),
    	@SelectFields NVARCHAR(500),
    	@PageIndex INT,
    	@PageSize INT,
    	@Condition NVARCHAR(4000),
    	@Sort NVARCHAR(100),
    	@RowCount INT output
    AS
    
    BEGIN
    
    	IF LEN(@Condition) > 0
    	BEGIN
    		SET @Condition = ' WHERE ' + @Condition;
    	END
    
    	IF LEN(@Sort) = 0
    	BEGIN
    		SET @Sort = 'id DESC';
    	END
    
    	--获取@RowCount信息
    	DECLARE @sql NVARCHAR(4000)
    	SET @sql = 'SELECT @TEMP = COUNT(id) FROM ' + @TableName + @Condition
    	EXEC sp_executesql @sql, N'@TEMP INT OUTPUT', @RowCount OUTPUT
    
    	DECLARE @StartRowIndex INT;
    
    	--根据页码和每页大小计算起始行
    	IF @PageIndex < 1
    	BEGIN
    		SET @PageIndex = 1
    	END
    
    	DECLARE @PageCount INT
    	SET @PageCount = ceiling(cast(@RowCount as float) / @PageSize)
    
    	IF @PageIndex > @PageCount
    	BEGIN
    		SET @PageIndex = @PageCount
    	END
    
    	SET @StartRowIndex = ( @PageIndex - 1 )* @PageSize + 1 
    
    	EXEC
    	('
    	 --声明一个读取主键的游标
    	 DECLARE PagingCursor CURSOR DYNAMIC READ_ONLY FOR
    	 --这里只读取主键,并插入表里
    	 SELECT id FROM ' + @TableName + @Condition + ' ORDER BY ' + @Sort
    	)
    
    	DECLARE @IDs NVARCHAR(2000)
    	SET @IDs = '0'
    
    	DECLARE @PK NVARCHAR(50)
    
    	--打开游标
    	OPEN PagingCursor
    	--直接跳到起始行
    	FETCH RELATIVE @StartRowIndex FROM PagingCursor INTO @PK
    
    	--不返回统计的行数
    	SET NOCOUNT ON
    	--开始循环读取记录
    	WHILE @PageSize > 0 AND @@FETCH_STATUS = 0
    	BEGIN
    	   --INSERT @tblPK (PK)  VALUES ( @PK )
    	   SET @IDs = @IDs + ',' + @PK
    	   FETCH NEXT FROM PagingCursor INTO @PK
    	   SET @PageSize = @PageSize - 1
    	END
    
    	CLOSE       PagingCursor
    	DEALLOCATE  PagingCursor
    	EXEC('SELECT ' + @SelectFields + ' FROM ' + @TableName + ' WHERE ID IN (' + @IDs+ ') ORDER BY ' + @Sort )
    END
    

    aspx页面代码

         
    
    
                
    
                    
                    
                    
                    
                    
                    
                    
                        
                            
                            
                        
                    
                
    
    
    共有< %# grRowCount%>条记录   第页 共

    aspx.cs代码

        public int grRowCount = 0;
        public int currentIndex = 0;
        public int grPageCount = 0;
      protected void LinkClickPage(object sender, EventArgs e)
        {
            LinkButton clickBtn = sender as LinkButton;
            int pageIndex = Convert.ToInt32(clickBtn.CommandArgument);
            pageIndex = (pageIndex > -1) ? pageIndex : Convert.ToInt32((clickBtn.Parent.FindControl("txtNewPageIndex") as TextBox).Text) - 1;
            pageIndex = (pageIndex > -1) ? pageIndex : 0;
            BandData(pageIndex);
        }
        private void BandData(int pageIndex)
        {
            GridView gr = this.FindControl("GridView1") as GridView;
            DataTable source = new DataTable();
            source = GetPagerData(ConnectString, gr.PageSize, string.Empty, pageIndex + 1, string.Empty, "*", TableName, out grRowCount);
            grPageCount = (grRowCount - (grRowCount % gr.PageSize)) / gr.PageSize + (((grRowCount % gr.PageSize) == 0) ? 0 : 1);
            pageIndex = (pageIndex < 0) ? 0 : pageIndex;
            pageIndex = (pageIndex + 1 > grPageCount) ? grPageCount - 1 : pageIndex;
            currentIndex = pageIndex;
            hidPageIndex.Value = currentIndex.ToString();
            //绑定数据
            gr.DataSource = source;
            gr.DataBind();
        }
           public  DataTable GetPagerData(string connstr,int pageSize, string condition, int pageIndex, string sortField, string selectField, string tableName, out int rowCount)
            {
                DataTable da = null;
                SqlParameter[] parms = new SqlParameter[] {
                new SqlParameter("@TableName",tableName),
                new SqlParameter("@SelectFields",selectField),
                new SqlParameter("@PageIndex",pageIndex),
                new SqlParameter("@PageSize",pageSize),
                new SqlParameter("@Condition",condition),
                new SqlParameter("@Sort",sortField),
                new SqlParameter("@RowCount",SqlDbType.Int)
                };
                parms[parms.Length - 1].Direction = ParameterDirection.Output;
                da = SqlHelper.ExecuteDatatable(connstr, CommandType.StoredProcedure, "sp_PageData", parms);
                rowCount = Convert.ToInt32(parms[parms.Length - 1].Value);
                return da;
            }
    //强制让gridview的PagerTemplate显示
    protected void GridView1_DataBound(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count != 0)
            {
                Control table = GridView1.Controls[0];
                int count = table.Controls.Count;
                table.Controls[count - 1].Visible = true;
            }
        }
    

    实现原理,这是一个回发分页,通过LinkButton的点击事件处理分页,以CommandArgument来获取pageIndex。还通过一个hidden控件保存了pageIndex,避免别的控件引起回发(例如LinkButton删除某条记录)后无法绑定当前页数据。


    作者:天南一隅 | 分类目录:代码乐趣程序人生 | 标签:
  • 【原创】.NETweb开发中烦人的路径问题../和~/

    2011-03-18

    在WEB开发中经常会碰到路径问题,取图片、CSS、js让人烦恼的../的问题。

    某一天南哥闲的蛋疼,写了个方法解决了~~,目前方法仅适用于ASP.NET,但是思路可以适应于任何语言,

    废话不多说,上代码。

    方法写在了page基类里面。

            //主方法
            //url 例如~/upload/images/aa.jpg
            public virtual string CusResolveUrl(string url)
            {
                string currentUrl = Request.AppRelativeCurrentExecutionFilePath;
                string locationUrl = url;
                currentUrl = currentUrl.ToLower();
                locationUrl = locationUrl.ToLower();
                string theSameUrl = RetTheSameString(currentUrl, locationUrl);
                currentUrl = currentUrl.Replace(theSameUrl, "");
                locationUrl = locationUrl.Replace(theSameUrl, "");
                string[] currentUrlArrya = currentUrl.Split('/');
                string[] urlArrya = locationUrl.Split('/');
                StringBuilder father = new StringBuilder();
                for (int i = 0; i < currentUrlArrya.Length - 1; i++)
                {
                    father.Append("../");
                }
                return father.Append(locationUrl).ToString();
            }
            //辅助方法
            private string RetTheSameString(string strA, string strB)
            {
                StringBuilder sb = new StringBuilder();
                string[] tempA = strA.Split('/');
                string[] tempB = strB.Split('/');
                int forTimes = (tempA.Length > tempB.Length) ? tempB.Length : tempA.Length;
                for (int i = 0; i < forTimes; i++)
                {
                    if (tempA[i] == tempB[i])
                    {
                        sb.Append(tempA[i] + "/");
                    }
                    else
                    { break; }
                }
                return sb.ToString();
            }
            //调用示例
            
    

    本方法写在page基类里,项目所有页面均已继承,每次调用时传入要引用文件的相对路径(~/xx/xx/xx)无需考虑当前页面与引用文件的位置关系,大大方便了开发。
    有什么问题,留言,博主always online。

    作者:天南一隅 | 分类目录:代码乐趣程序人生 | 标签:
  • C#利用webrequest 通过图片src保存到本地

    2011-03-15

    闲话不多说,公司一个抓取程序用到了于是保存下来。

    //url 图片地址 例如 http://hiphotos.baidu.com/2d61d91b8385ed81ac6e7521.jpg
    //记得引用using System.Net;
     public void getimages(string url)
        {
            //创建一个request 同时可以配置requst其余属性
            System.Net.WebRequest imgRequst = System.Net.WebRequest.Create(url);
            //在这里我是以流的方式保存图片
             System.Drawing.Image downImage = System.Drawing.Image.FromStream
    (imgRequst.GetResponse().GetResponseStream());
            string dertory = string.Format(@"I:\imgfg.cn\5r\{0}\", DateTime.Now.ToString("yyyy-MM-dd"));
            string fileName = string.Format("{0}.jpg", DateTime.Now.ToString("HHmmssffff"));
            if (!System.IO.Directory.Exists(dertory))
           { System.IO.Directory.CreateDirectory(dertory); }
             downImage.Save(dertory + fileName);
             downImage.Dispose();//用完一定要释放
         }
    

    代码稍微简单一点,有什么问题可以留言。

    作者:天南一隅 | 分类目录:代码乐趣程序人生 | 标签:
  • 关于vs2005无法调试问题~~运行调试后失效!

    2011-03-04

    最近调试的时候总是发现,vs2005调试网页时,在IE网页打开后vs调试就自动关闭了,很让人头疼,Chrome 和FF都可以,不知

    道是哪里问题。

    今天终于受不了了,上网搜到了一个解决方法,试了试还行,分享给大家~~ 引用地址:

    微软官方方案:

    IE 8 has a feature called Loosely-Coupled Internet Explorer (LCIE) which results in IE running across multiple processes.
    http://www.microsoft.com/windows/internet-explorer/beta/readiness/developers-existing.aspx#lcie

    Older versions of the Visual Studio Debugger get confused by this and cannot figure out how to attach to the correct process.  You can work around this by disabling the process growth feature of LCIE.  Here’s how:

    • 1)  Open RegEdit
    • 2)  Browse to HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main
    • 3)  Add a dword under this key called TabProcGrowth
    • 4)  Set TabProcGrowth to 0

    Since you are running on Windows Server 2003, this is all you should need to do.  If you run into the same problem on Vista or newer, you will also need to turn off protected mode.

    中文意思就是:

    IE8中的LCIE功能会与VS调试器冲突,导致IE进程无法附加。因此设置LCIE为禁用即可。

    • 1、打开注册表,展开HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main项;
    • 2、添加一个dword键TabProcGrowth,设置键值为0。

    然后就OK了~~

    作者:天南一隅 | 分类目录:程序人生 | 标签: