in

SDT Community Server

SDT Forums, Blogs, Photos server.

Floating Heart

No description is bad.

April 2008 - Posts

  • Control State of ASP.Net 2.0

    Here is sample:

    [code language="C#"]
            protected override object SaveControlState()
            {
                object baseState = base.SaveControlState();
                object thisState = new object[] { baseState, _recordCount };
                return thisState;
            }

            protected override void LoadControlState(object savedState)
            {
                object[] stateLastRequest = (object[])savedState;
                object baseState = stateLastRequest[0];
                base.LoadControlState(baseState);
                _recordCount = (int)stateLastRequest[1];
            }

            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                ...
                Page.RegisterRequiresControlState(this);
            }
    [/code]

    Posted Apr 15 2008, 11:57 AM by wicky with no comments
    Filed under:
  • GridView 72般绝技

    http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx

     快速预览:
    GridView无代码分页排序
    GridView选中,编辑,取消,删除
    GridView正反双向排序
    GridView和下拉菜单DropDownList结合
    GridView和CheckBox结合
    鼠标移到GridView某一行时改变该行的背景色方法一
    鼠标移到GridView某一行时改变该行的背景色方法二
    GridView实现删除时弹出确认对话框
    GridView实现自动编号
    GridView实现自定义时间货币等字符串格式
    GridView实现用“...”代替超长字符串
    GridView一般换行与强制换行
    GridView显示隐藏某一列
    GridView弹出新页面/弹出新窗口
    GridView固定表头(不用javascript只用CSS,2行代码,很好用)
    GridView合并表头多重表头无错完美版(以合并3列3行举例)
    GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
    GridView加入自动求和求平均值小计
    GridView数据导入Excel/Excel数据读入GridView

    http://blog.csdn.net/21aspnet/ 清清月儿,
    不错的blog。

     

  • WebToolkit.Info

  • JavaScript的BASE64

    http://www.bccn.net/Article/web/javascript/jszl/200703/4535.html

    原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=15&id=107182

    /**
    * 我在网上看到过很多BASE64的JavaScript算法,都觉得不满意,于是自己写了一个,在这里分享一下。
    * 我的代码在质量的效率都较高,没有一些冗余的操作。总体来讲我觉得非常不错。
    * 如果大家有什么不懂的地方可以问我。
    */
    var BASE64={
        /**
         * 此变量为编码的key,每个字符的下标相对应于它所代表的编码。
         */
        enKey: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
        /**
         * 此变量为解码的key,是一个数组,BASE64的字符的ASCII值做下标,所对应的就是该字符所代表的编码值。
         */
        deKey: new Array(
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
            52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
            -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
            -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
        ),
        /**
         * 编码
         */
        encode: function(src){
            //用一个数组来存放编码后的字符,效率比用字符串相加高很多。
            var str=new Array();
            var ch1, ch2, ch3;
            var pos=0;
           //每三个字符进行编码。
            while(pos+3<=src.length){
                ch1=src.charCodeAt(pos++);
                ch2=src.charCodeAt(pos++);
                ch3=src.charCodeAt(pos++);
                str.push(this.enKey.charAt(ch1>>2), this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f));
                str.push(this.enKey.charAt(((ch2<<2)+(ch3>>6))&0x3f), this.enKey.charAt(ch3&0x3f));
            }
            //给剩下的字符进行编码。
            if(pos<src.length){
                ch1=src.charCodeAt(pos++);
                str.push(this.enKey.charAt(ch1>>2));
                if(pos<src.length){
                    ch2=src.charCodeAt(pos);
                    str.push(this.enKey.charAt(((ch1<<4)+(ch2>>4))&0x3f));
                    str.push(this.enKey.charAt(ch2<<2&0x3f), '=');
                }else{
                    str.push(this.enKey.charAt(ch1<<4&0x3f), '==');
                }
            }
           //组合各编码后的字符,连成一个字符串。
            return str.join('');
        },
        /**
         * 解码。
         */
        decode: function(src){
            //用一个数组来存放解码后的字符。
            var str=new Array();
            var ch1, ch2, ch3, ch4;
            var pos=0;
           //过滤非法字符,并去掉'='。
            src=src.replace(/[^A-Za-z0-9\+\/]/g, '');
            //decode the source string in partition of per four characters.
            while(pos+4<=src.length){
                ch1=this.deKey[src.charCodeAt(pos++)];
                ch2=this.deKey[src.charCodeAt(pos++)];
                ch3=this.deKey[src.charCodeAt(pos++)];
                ch4=this.deKey[src.charCodeAt(pos++)];
                str.push(String.fromCharCode(
                    (ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2), (ch3<<6&0xff)+ch4));
            }
            //给剩下的字符进行解码。
            if(pos+1<src.length){
                ch1=this.deKey[src.charCodeAt(pos++)];
                ch2=this.deKey[src.charCodeAt(pos++)];
                if(pos<src.length){
                    ch3=this.deKey[src.charCodeAt(pos)];
                    str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4), (ch2<<4&0xff)+(ch3>>2)));
                }else{
                    str.push(String.fromCharCode((ch1<<2&0xff)+(ch2>>4)));
                }
            }
           //组合各解码后的字符,连成一个字符串。
            return str.join('');
        }
    };

     

    使用方法:

    var str='hello world!';
    var enstr=BASE64.encode(str);
    alert(enstr);
    var destr=BASE64.decode(enstr);
    alert(destr);

  • Compress ViewState in your ASP.Net application with SimpleCompressedPageStatePersister

    Just put below code into your BasePage:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    private SimpleCompressedPageStatePersister _compressedPageStatePersister = null;
    private bool _enableCompressedPageState = true;
    public bool EnableCompressedPageState
    {
        get { return _enableCompressedPageState; }
        set { _enableCompressedPageState = value; }
    }
    protected override PageStatePersister PageStatePersister
    {
        get
        {
            if (_enableCompressedPageState)
            {
                if (_compressedPageStatePersister == null)
                    _compressedPageStatePersister = new SimpleCompressedPageStatePersister(this);
                return _compressedPageStatePersister;
            }
            else
            {
                return base.PageStatePersister;
            }
        }
    }

    Posted Apr 08 2008, 05:09 PM by wicky with no comments
    Filed under:
  • 20 Tips to Improve ASP.net Application Performance

    http://funjackyone.javaeye.com/blog/120226

    August 31, 2007
    20 Tips to Improve ASP.net Application Performance
    Not a .net Developer?
    Are you an asp.net developer? If you aren't don't worry, we have similar posts in the works for Ruby, PHP, and other developers out there. If you are an ASP.net developer, listen up!

    Get Ready for Massive Gains
    There are certain things you should take into account when you are developing your applications. Over the last 12 years or so of working with asp and asp.net, I have learned to avoid and do certain things that increase your application performance by a massive amount! Below are my top 20 tips to improving ASP.net application Performance.

    Disable Session State
    Disable Session State if you're not going to use it. By default it's on. You can actually turn this off for specific pages, instead of for every page:

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false" Inherits="WebApplication1.WebForm1"
    EnableSessionState="false" %>
    You can also disable it across the application in the web.config by setting the <sessionState> mode value to Off.

    Output Buffering
    Take advantage of this great feature. Basically batch all of your work on the server, and then run a Response.Flush method to output the data. This avoids chatty back and forth with the server.

    <%response.buffer=true%> Then use:

    <%response.flush=true%> Avoid Server-Side Validation
    Try to avoid server-side validation, use client-side instead. Server-Side will just consume valuable resources on your servers, and cause more chat back and forth.

    Repeater Control Good, DataList, DataGrid, and DataView controls Bad
    Asp.net is a great platform, unfortunately a lot of the controls that were developed are heavy in html, and create not the greatest scaleable html from a performance standpoint. ASP.net repeater control is awesome! Use it! You might write more code, but you will thank me in the long run!

    Take advantage of HttpResponse.IsClientConnected before performing a large operation:

    if (Response.IsClientConnected)
    {
    // If still connected, redirect
    // to another page.
    Response.Redirect("Page2CS.aspx", false);
    }What is wrong with Response.Redirect? Read on...

    Use HTTPServerUtility.Transfer instead of Response.Redirect
    Redirect's are also very chatty. They should only be used when you are transferring people to another physical web server. For any transfers within your server, use .transfer! You will save a lot of needless HTTP requests.

    Always check Page.IsValid when using Validator Controls
    So you've dropped on some validator controls, and you think your good to go because ASP.net does everything for you! Right? Wrong! All that happens if bad data is received is the IsValid flag is set to false. So make sure you check Page.IsValid before processing your forms!

    Deploy with Release Build
    Make sure you use Release Build mode and not Debug Build when you deploy your site to production. If you think this doesn't matter, think again. By running in debug mode, you are creating PDB's and cranking up the timeout. Deploy Release mode and you will see the speed improvements.

    Turn off Tracing
    Tracing is awesome, however have you remembered to turn it off? If not, make sure you edit your web.config and turn it off! It will add a lot of overhead to your application that is not needed in a production environment.

    <configuration>
    <system.web>
    <trace enabled="false" pageOutput="false" />
    <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>
    <compilation debug="false" />
    </system.web>
    </configuration>Page.IsPostBack is your friend
    Make sure you don't execute code needlessly. I don't know how many web developers forget about checking IsPostBack! It seems like such a basic thing to me! Needless processing!

    Avoid Exceptions
    Avoid throwing exceptions, and handling useless exceptions. Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications. Write your code so they don't happen! Don't code by exception!

    Caching is Possibly the number one tip!
    Use Quick Page Caching and the ASP.net Cache API! Lots to learn, its not as simple as you might think. There is a lot of strategy involved here. When do you cache? what do you cache?

    Create Per-Request Cache
    Use HTTPContect.Items to add single page load to create a per-request cache.

    StringBuilder
    StringBuilder.Append is faster than String + String. However in order to use StringBuilder, you must
    new StringBuilder()Therefore it is not something you want to use if you don't have large strings. If you are concatenating less than 3 times, then stick with String + String. You can also try String.Concat

    Turn Off ViewState
    If you are not using form postback, turn off viewsate, by default, controls will turn on viewsate and slow your site.

    public ShowOrdersTablePage()
    {
    this.Init += new EventHandler(Page_Init);
    }

    private void Page_Init(object sender, System.EventArgs e)
    {
    this.EnableViewState = false;
    }Use Paging
    Take advantage of paging's simplicity in .net. Only show small subsets of data at a time, allowing the page to load faster. Just be careful when you mix in caching. How many times do you hit the page 2, or page 3 button? Hardly ever right! So don't cache all the data in the grid! Think of it this way: How big would the first search result page be for "music" on Google if they cached all the pages from 1 to goggle ;)

    Use the AppOffline.htm when updating binaries
    I hate the generic asp.net error messages! If I never had to see them again I would be so happy. Make sure your users never see them! Use the AppOffline.htm file!

    Use ControlState and not ViewState for Controls
    If you followed the last tip, you are probably freaking out at the though of your controls not working. Simply use Control State. Microsoft has an excellent example of using ControlState here, as I will not be able to get into all the detail in this short article.

    Use the Finally Method
    If you have opened any connections to the database, or files, etc, make sure that you close them at the end! The Finally block is really the best place to do so, as it is the only block of code that will surely execute.

    Option Strict and Option Explicit
    This is an oldy, and not so much a strictly ASP.net tip, but a .net tip in general. Make sure you turn BOTH on. you should never trust .net or any compiler to perform conversions for you. That's just shady programming, and low quality code anyway. If you have never turned both on, go turn them on right now and try and compile. Fix all your errors.
    There are hundreds more where these came from, however I really feel that these are the most critical of the speed improvements you can make in ASP.net that will have a dramatic impact on the user experience of your application. As always if you have any suggestions or tips to add, please let us know! We would love to hear them!

    Have web development!
    Posted Apr 08 2008, 01:33 PM by wicky with no comments
    Filed under:
  • How to Create Linked Server for a MySQL in SQL Server

    http://www.sqlservercentral.com/Forums/Topic340912-146-1.aspx

    It took me about a day to figure this out, so I thought I'd try to save someone else the pain...

    Creating a Linked Server in SSMS for a MySQL database

    1. Download the MySQL ODBC driver from mysql.com
    2. Install MySQL ODBC driver on Server where SQL Server resides
            -Double Click Windows Installer file and follow directions.

    3. Create a DSN using the MySQL ODBC driver
    Start-> Settings -> Control Panel -> Administrative Tools -> Data Sources (ODBC)
            -Click on the System DSN tab
            -Click Add
            -Select the MySQL ODBC Driver
            -Click Finish
    On the Login Tab:
            -Type a descriptive name for your DSN.
            -Type the server name or IP Address into the Server text box.
            -Type the username needed to connect to the MySQL database into the user text box.
            -Type the password needed to connect to the MySQL database into the password text box.
            -Select the database you'd like to start in.
    On the Advance Tab:
    Under Flags 1:
            -Check Don't Optimize column width.
            -Check Return Matching Rows
            -Check Allow Big Results
            -Check Use Compressed protocol
            -Check BIGINT columns to INT
            -Check Safe
    Under Flags 2:
            -Check Don't Prompt Upon Connect
            -Check Ignore # in Table Name
    Under Flags 3:
            -Check Return Table Names for SQLDescribeCol
            -Check Disable Transactions
    Now Test your DSN by Clicking the Test button


    4. Create a Linked Server in SSMS for the MySQL database
    SSMS (SQL Server Management Studio -> Expand Server Objects
            -Right Click Linked Servers -> Select New Linked Server
    On the General Page:
            -Linked Server: Type the Name for your Linked Server
            -Server Type: Select Other Data Source
            -Provider: Select Microsoft OLE DB Provider for ODBC Drivers
            -Product name: Type MySQLDatabase
            -Data Source: Type the name of the DSN you created
    On The Security Page
            -Map a login to the Remote User and provide the Remote Users Password
            -Click Add under Local server login to remote server login mappings:
            -Select a Local Login From the drop down box
            -Type the name of the Remote User
            -Type the password for the Remote User

    5. Change the Properties of the Provider MSDASQL
    Expand Providers -> Right Click MSDASQL -> Select Properties
            -Enable Nested queries
            -Enable Level zero only (this one's the kicker)
            -Enable Allow inprocess
            -Enable Supports 'Like' operator

    6. Change settings in SQL Server Surface Area Configuration for Features
            -Enable OPENROWSET and OPENDATASOURCE support.
    7. Change settings in SQL Server Surface Area Configuration for Services and Connections
            -Enable Local and Remote connections via TCP/IP and named pipes

    8. Stop SQL Server and SQL Server Agent
    9. Start SQL Server and SQL Server Agent

    http://forums.devx.com/showthread.php?t=18963

    I got those kind of problems. My solution was simple ...

    Use Microsoft OLE DB Provider for ODBC Drivers

    Use the Connection String

    DRIVER={MySQL ODBC 3.51 Driver};SERVER=myserver.com;DATABASE=database;USER=user;PASSWORD=password;OPTION=3

    And in Provider Options select:

    Only level zero
    Non-transactional (something)
    Allow InProcess <--- not sure ... but the prev. two yes

    the linked thing will work as

    mylinkedserver...tablename
    Posted Apr 08 2008, 09:27 AM by wicky with 1 comment(s)
    Filed under:
Copyright SDT, 2006-2009. All rights reserved.