Monday, July 27, 2009

What is AutoEventWireup?

ASP.NET page framework also supports an automatic way to associate page events and methods. If the AutoEventWireup attribute of the Page directive is set to true (or if it is missing, since by default it is true), the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case, no explicit Handles clause or delegate is needed.

The disadvantage of the AutoEventWireup attribute is that it requires that the page event handlers have specific, predictable names. This limits your flexibility in how you name your event handlers. Therefore, in Visual Studio, the AutoEventWireup attribute is set to false by default and the designer generates explicit code to bind page events to methods.

If you do set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. This can result in the same event code being called twice when the page runs. As a consequence, you should always leave AutoEventWireup set to false when working in Visual Studio.

For more information Click here

Wednesday, July 15, 2009

asp:Menu Server Control - Cross Browser Compatibility (Safari/Chrome)

 

Cross browser compatibility is upsetting while working with asp:Menu Server Control, and It was not rendering/working well with Safari and Chrome.

After bit of googling... :-) and found a solution for this.

Approach 1:

I have added below small piece of code snippet in my MasterPage's Page_Load event

        if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
        {
            Request.Browser.Adapters.Clear();
        }

This will tell asp.net not to use an adapter when rendering the menu control. This will work for both Safari and chrome as well because they both use webkit which is how asp.net identifies.

Approach 2:

You can force the menu to work by overwrite the Page_PreInit method on the page and telling the page that the client target is a modern “uplevel” browser:-

protected void Page_PreInit(object sender, EventArgs e)
{
if (Page.Request.ServerVariables["http_user_agent"].ToLower().Contains("safari"))
{
Page.ClientTarget = "uplevel";
}
}

Unfortunately, you can't just add the fix to your Master Page, it doesn't contain a PreInit method as it's a web control not a web page.

Please note that you have to do this in the page class file, and not in the master page class file. Obviously this means you have to do it for every page – which is not ideal.

Wednesday, May 20, 2009

Date Formats in SQL Server

---Yesterday 
select dateadd(d,-1,getdate()) as yesterday

--First Day of Current Week
select dateadd(wk,datediff(wk,0,getdate()),0) as [First Day of Current Week]

--Last Day of Current Week
select dateadd(wk,datediff(wk,0,getdate()),6) as [Last Day of Current Week]

--First Day of Last Week
select dateadd(wk,datediff(wk,7,getdate()),0) as [First Day of Last Week]

--Last Day of Last Week
select dateadd(wk,datediff(wk,7,getdate()),6) as [Last Day of Last Week]

--First Day of Current Month
select dateadd(mm,datediff(mm,0,getdate()),0) as [First Day of Current Month]

--Last Day of Current Month
select dateadd(ms,- 3,dateadd(mm,0,dateadd(mm,datediff(mm,0,getdate())+1,0))) as [Last Day of Current Month]

--First Day of Last Month
select dateadd(mm,-1,dateadd(mm,datediff(mm,0,getdate()),0)) as [First Day of Last Month]

-- First day of next month
SELECT dateadd(m, datediff(m, 0,getdate())+1, 0) AS MonthStart

-- Last day of next month
SELECT dateadd(m,datediff(m, 0, dateadd(m, +2 ,getdate()))+1, -1) AS MonthEnd

--Last Day of Last Month
select dateadd(ms,-3,dateadd(mm,0,dateadd(mm,datediff(mm,0,getdate()),0))) as [Last Day of Last Month]

--First Day of Current Year
select dateadd(yy,datediff(yy,0,getdate()),0) as [First Day of Current Year]

--Last Day of Current Year
select dateadd(ms,-3,dateadd(yy,0,dateadd(yy,datediff(yy,0,getdate())+1,0))) as [Last Day of Current Year]

--First Day of Last Year
select dateadd(yy,-1,dateadd(yy,datediff(yy,0,getdate()),0)) as [First Day of Last Year]

--Last Day of Last Year
select dateadd(ms,-3,dateadd(yy,0,dateadd(yy,datediff(yy,0,getdate()),0))) as [Last Day of Last Year]

select month('09/06/2008')
select year('09/06/2008')
select getutcdate()
select getdate()
select day('09/06/2008')
select datepart(dd,'09/06/2008')
select datename(yyyy,'09/06/2008')
select datediff(dd,'09/06/2008','09/18/2008')
select dateadd(dd,60,'09/06/2008')

Tuesday, March 31, 2009

Limiting the File Upload Size in ASP.NET

By default, the maximum size of a file to be uploaded to a server using the ASP.NET FileUpload control is 4MB. You cannot upload anything that is larger than this limit.

To change this size limit, you have to make some changes in the application's web.config:    

    

   

maxRequestLength - Attribute limits the file upload size for ASP.NET application. This limit can be used to prevent denial of service attacks (DOS) caused by users posting large files to the server. The size specified is in kilobytes. As mentioned earlier, the default is "4096" (4 MB). Max value is "1048576" (1 GB) for .NET Framework 1.0/1.1 and "2097151" (2 GB) for .NET Framework 2.0.

executionTimeout - Attribute indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by the application. The executionTimeout value should always be longer than the amount of time that the upload process can take.