Friday, November 16, 2007

Datagrid VS ViewState

This week, I try to optimized my web application which using 10 million record.

The first thing that I notice that EnableViewState by default is set to true.

So my datagrid will store all the view state into client. so every time there is postback or asyn postback, it will transfer heaps of data through networks. and It is not very efficient.

So I try to disable them by EnablingViewState = false.
However by disabling view state in the data , all the events in datagrid is not fired..


one of the solution is to disable view state on each item.
_dg.DataBind();
foreach (DataGridItem item in _dg.Items)
{
item.EnableViewState=false;
{


however, if your all commandArguments is not work,..

so the other solution is you need to bind every time in page back...

Tuesday, November 6, 2007

Resurrection

Just in case if you don't know.

Sometimes it is you may need to resurrects your object.
for example you want the object to clean itself gracefully everytime the object dies.

Here how to do resurrection....

public class MyBaseClass
{
Protected override void Finalize()
{
//TODO: Some Clean up....

GC.ReRegisterForFinalize(this);
}
}

By calling GC.ReRegisterForFinalize method, it will appends the address of the specified object to the end of the finalization queue. When GC detects that this object is unreadchable again, It will queue the object's pointer ob the freachable queue and Finalize method will get called again.

This is example show how to create an object that constantly resurrects itself and never dies...
which usually undesireable. It is far more common to conditionally set a root to a reference the object inside the finalize method.

Tuesday, October 30, 2007

Sys.WebForms.PageRequestManagerParserErrorException

“Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.”


this is the most annoyed error , That I ever had.

It happens only the first time after I restart my web application / after change web.config.

I still keep getting that error even I have try to alter my code based on this
--------------------------------------------------------------------------------------
  1. Calls to Response.Write():
    Place an or similar control on your page and set its Text property. The added benefit is that your pages will be valid HTML. When using Response.Write() you typically end up with pages that contain invalid markup.
  2. Response filters:
    The fix might just be to not use the filter. They're not used very often anyway. If possible, filter things at the control level and not at the response level.
  3. HttpModules:
    Same as response filters.
  4. Server trace is enabled:
    Use some other form of tracing, such as writing to a log file, the Windows event log, or a custom mechanism.
  5. Calls to Server.Transfer():
    I'm not really sure why people use Server.Transfer() at all. Perhaps it's a legacy thing from Classic ASP. I'd suggest using Response.Redirect() with query string parameters or cross-page posting.

Another way to avoid the parse error is to do a regular postback instead of an asynchronous postback. For example, if you have a button that absolutely must do a Server.Transfer(), make it do regular postbacks. There are a number of ways of doing this:

  1. The easiest is to simply place the button outside of any UpdatePanels. Unfortunately the layout of your page might not allow for this.
  2. Add a PostBackTrigger to your UpdatePanel that points at the button. This works great if the button is declared statically through markup on the page.
  3. Call ScriptManager.RegisterPostBackControl() and pass in the button in question. This is the best solution for controls that are added dynamically, such as those inside a repeating template.

-------------------------------------------------------

After I give up... The only solution is to add enableEventValidation="false" in the Page attribute.

Probably it is not the best solution because the event validation mechanism reduces the risk of unauthorized postback requests and callbacks. When the EnableEventValidation property is set to true, ASP.NET allows only the specific events that can be raised on the control during a postback request or callback. In this model, a control registers its events during rendering and then validates the events during postback or callback handling.

However after debugging I relies that It throw that error after I write into a session which I never write anything in the beginning. which cause the event validation not valid

So another solution is if EnableEventValidation is so important then
put it back EnableEventValidation = true then
in your Page_Load Event,
put
----------------------
Session["something"] = true; in your page load event.

This will create the cookie needed for the session variable to work, and therefore anything using a session will also work without throwing the error.

---------------------

This solution needed if there is no session ha s been initialize but when there is asynchronous postback, you put a session and EnableEventValidation is true

Monday, October 29, 2007

The Simplest Javascript to convert to hexadecimal in Javascript

function toHexa(d) {return d.toString(16);}

function toDec(h) {return parseInt(h,16);}

Saturday, September 22, 2007

As Keyword in C#

The "as" keyword in C# attempts to cast an instance to a specific type and if it fails returns null.

What is the different between explicitly cast ?

string str = (string) Request["nullSession"];
string str = Request["nullSession"] as string;

Both of them return the same (NULL)...

However probably, I will stick with my casting style because
1. It confused me when I need to convert from C# to VB
2. I can not using as keyword to replace
(int) 123.23
where I expect to get 123 as integer

----
123.21 as int

//throws The as operator must be used with a reference type ('int' is a value type)

123.21 as int16
//throws The as operator must be used with a reference type ('short' is a value type)

123.21 as double
//throws The as operator must be used with a reference type ('double' is a value type)


Saturday, September 8, 2007

JSON AJAX Localization

It may be annoying when you need to have several languages in your web sites..

There is several way... to implement localization your web content in the client..

you either create a control and attached those assembly to your web site...
or just use... JSON object...

My preference is using JSON ( Javascipt Object Notation)....

* Create.. JSON Resource JS file for each.. languange...
Resource.js
MyReourceText={
"HeaderTitle":"Hello World",
"FooterText":"CopyRight By Kurniawan"
}

Resource.fr.js
MyReourceText={
"HeaderTitle":"Header Title in France",
"FooterText":"CopyRight By Kurniawan in france"
}


* Create.. UpdateResource js - this script will update your content use specific localize language.
Note:
-saperate this file from Resource.js...so you will not duplicate your code


UpdateResource.js

Sys.Application.add_load(SetupForm);
function SetupForm()
{
$get('HeaderTitle').innerText = MyReourceText.HeaderTitle;
$get('FooterText').innerText = MyReourceText.FooterText;
}



*
In your aspx... add enable EnableScriptLocalization="true" in your ScriptManager
This will enable the


*
Add those script reference into your script Manager
<asp:ScriptReference Path="Resource.js" ResourceUICulture="fr" />
<asp:ScriptReference Path="UpdateResource.js" />


**** THAT'S ALL ****

You may change your language from the browser or
from the your own control.... by using this...
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture(cmbLang.SelectedValue);

and you can get the current language from your browser when not postback
cmbLang.Items.FindByValue(System.Threading.Thread.CurrentThread.
CurrentUICulture.TwoLetterISOLanguageName).Selected = true;

****

You may enable globalization.. to formating your date / currency into specific languange../ culture...

by activate EnableScriptGlobalization = "true" in Script Manager...
You can use
d.localeFormat("dddd, dd MMM yyyy");

****

Thursday, September 6, 2007

XAML ???

X A M L ??? (ZAMMEL) Yes, That's the correct spelling...

It extends for Extendsible Application Markup Language.
ZAMMEL is Xml based language to define object and their properties ...

It focused on UI for WPF/E (Windows Presentation Foundation/Everywhere)

You may check out Silverlight to begin with XAML.

Here is the Zammel Sample
<object>
<child property="x" property="y">
<child.property>
<class property="u" property="v"/>
</child.property>
</child>
<child>
</child>
</object>


Note:
* Xaml is different with WPF - Xaml is Xml based Markup
WPF is a graphics API

*
XAML also supports things like 3D and controls, which SVG does not. It's different from SVG

* Saparate Design and Development