ASP.NET AJAX Reflection

by dee 1. July 2008 09:46

Custom classes that are going to be used in ajax requests to web services' methods should meet the following requirements:

1. Have public default constructor (ASP.NET will not reflect the class on client side if it doesn't meet this condition - NO exception will be thrown)
2. Have setter for each public property (ASP.NET will not set values to the fields which don't have setter - NO exception will be thrown as well)

Tags:

ASP.NET

Firefox very slow on localhost connections on Vista

by dee 29. June 2008 07:21

Doing web development on my local machine - a Vista box - I was wondering why page rendering was so slow. First I thought my web application was not well done. But then I figured out that the pages rendered lightning fast on IE 7. And again later, I experienced, that the web application rendered pretty fast even in Firefox, when published to the live server.

The change of the Firefox setting network.dns.disableIPv6 to true did the trick. (about:config)
Original: http://codepoetry.wordpress.com/2007/03/27/firefox-very-slow-on-localhost-connections-on-vista/

FormsAuthentication.GetRedirectUrl

by dee 21. May 2008 15:00

FormsAuthentication.GetRedirectUrl is a very strange method :) and I will explain why. Developing some stuff I needed to get redirectUrl on loging page. It can be made by simply taking "ReturnUrl" parameter from query string. But at some moment I found GetRedirectUrl method which I hoped would be better solution for my task. But what I saw when opened its parameters - string userName, bool createPersistentCookie! Why do we need those parameters for just taking single parameter from query string? I decided to look at the documentation first and then at reflector :) MSDN says that createPersistentCookie is ignored .. ok.. it's much better. But what about userName which is "required" by manual? Let's take a look at the reflector:

public static string GetRedirectUrl(string userName, bool createPersistentCookie)
{
   if (userName == null)
   {
      return null;
   }
   return GetReturnUrl(true);
}

So, what do we see here? - createPersistentCookie is never used thus it's really ignored as we suspected. But let's take a closer look at userName variable - it's just checked for null and nothing more! Now tell me... why should we use that parameter if it is not used anywhere? Why documentation doesn't say about what actually happens? I am confused. The result of my investigation was a decision to use that method in following manner:

string returnUrl = FormsAuthentication.GetRedirectUrl(String.Empty, false);

and it's working perfectly as I wanted :)

Paging using GridView

by dee 27. April 2008 14:26

Step 1

Write SP for items selection:

Input parameters:
@StartIndex int,
@PageSize int,
@ItemType int -- this is sample 

-- Select count of all items. It will be VirtualItemsCount
SELECT COUNT(*) AS ItemsCount
FROM Table
WHERE ItemType = @ItemType;

-- Select items needed for current page only
WITH SelectedItems AS
(
   SELECT ROW_NUMBER OVER(ORDER BY ID) AS RowID, *
   FROM Table
   WHERE ItemType = @ItemType
)
SELECT TOP(@PageSize)
FROM SelectedItems
WHERE RowID > @StartIndex;

Step 2

Write data adapter which is used as ObjectDataSource. This step is needed because GridView doesn't support VirtualItemsCount property directly. If we want to know how many items we've selected we should add totalItemsCount variable (as "out") to the parameters list while configuring data source. Take into account that in such case GridView will call all methods with that parameter!

public class SampleDataAdapter
{
   private int itemsCount;

   public int GetItemsCount(int itemType, out int totalItemsCount)
   {
      totalItemsCount = -1 // We don't need to return it here
      return itemsCount;
   }

   public List<Items> GetData(int itemType, out int totalItemsCount, int startRow, int maxRows)
   {
      // retreive data using itemType, startRow, maxRows
      List<Items> result = ItemDAL.GetByType(itemType, startRow, maxRows, out itemsCount);
      totalItemsCount = itemsCount;
      return result;
   }
}

Step 3

Configure GridView to use ObjectDataSource with you SampleDataAdapter:

public void DataBind()
{
   ObjectDataSource objectDataSource = new ObjectDataSource();
   objectDataSource.EnablePaging = SampleGridView.AllowPaging;
   objectDataSource.EnableViewState = false;
   objectDataSource.TypeName = "SampleDataAdapter";
   objectDataSource.SelectMethod = "GetData";
   objectDataSource.SelectCountMethod = "GetItemsCount";
   objectDataSource.StartRowIndexParameterName = "startRow";
   objectDataSource.MaximumRowsParameterName = "maxRows";

   objectDataSource.SelectParameters.Add("itemType", itemType);
  
   Parameter itemsCountParameter = new Parameter("totalItemsCount");
   itemsCountParameter.Direction = ParameterDirection.Output;
   objectDataSource.SelectParameters.Add(itemsCountParameter);

   objectDataSource.Selected += new ObjectDataSourceStatusEventHandler(objectDataSource_Selected); // Subscribe for Selected event to be able to select totalItemsCount. Be aware - it is fired whenever any method of SampleDataAdapter is called.

   SampleGridView.DataSource = objectDataSource;
   SampleGridView.DataBind();
}

private void objectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
   int itemsCount = (int)e.OutputParameters["totalItemsCount"];

   // "Selected" event may be raised when GetItemsCount method of our SampleDataAdapter is called. So, we need to check that we are on the right step of data binding process
   if (itemsCount != -1)
   {
      // here we have totalItemsCount
   }
}

Enable html encoding in asp:Literal control

by dee 20. April 2008 12:51

Set Mode property to "Encode"

Url rewriting in HttpHandler

by dee 24. January 2008 03:10

Make the following:

public void ProcessRequest(HttpContext context)
{
    HttpRequest request = context.Request;
    context.RewritePath("new url");
    IHttpHandler handler = PageParser.GetCompiledPageInstance("page which should handle request", null, context); // Page name should be like "Default.aspx". Without url parameters!
    handler.ProcessRequest(context);
}

Suppress Application Error window on application crash

by dee 17. January 2008 01:19

The only way I could find is to call SetErrorMode() function of Win32 API:

SetErrorMode(SetErrorMode(SEM_NOGPFAULTERRORBOX) | SEM_NOGPFAULTERRORBOX);

Unfortunately I was not able to find the way of disabling that dialog for process which we are starting (Process.Start()).

How to rewrite SessionID?

by dee 17. January 2008 01:03

The only possibility to make that trick it is to change "ASP.NET_SessionId" cookie before ASP.NET has loaded session state. It can be achieved by handling PostMapRequestHandler application event. For example:

 

void Application_PostMapRequestHandler(object sender, EventArgs e)
{
    string aspNetSessionName = "ASP.NET_SessionId";
    if (Request.Cookies[aspNetSessionName] != null)
    {
        Request.Cookies[aspNetSessionName].Value = "new session ID";
    }
    else
    {
        HttpCookie sessionIDCookie = new HttpCookie(aspNetSessionName, "new session ID");
        Request.Cookies.Add(sessionIDCookie);
    }
}

Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen