Thursday, February 28, 2008

File Upload AJAX V 1.2

As you know that ASP.Net File Upload control can not be placed in multi view. If you put your file upload inside the multi view. You will lost your content when the multi view change the active view.

But the good news is there is Open source control called File Upload Ajax which in V1.2 can be placed inside Wizard, Multi View, invisible Panels, hidden divs, etc.

Download the control @ http://www.codeplex.com/fileuploadajax/Release/ProjectReleases.aspx?ReleaseId=8061

And try this.

  1. Add your FUA in your project
  2. Register in your page or in web.config
  3. <%@ Register Assembly="FUA" Namespace="Subgurim.Controles" TagPrefix="cc1" %>



  4. Add your Update panel, Multiview and file upload



  5. <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
    <asp:MultiView ID="multiPages" runat="server" ActiveViewIndex="0">
    <asp:View ID="v1" runat="server">
    </asp:View>
    <asp:View ID="v2" runat="server">
    upload:
    <cc1:FileUploaderAJAX ID="FileUploaderAJAX1" runat="server" MaxFiles="1" Directory_CreateIfNotExists="true" File_RenameIfAlreadyExists="true"/>
    </asp:View>
    </asp:MultiView>
    </ContentTemplate>
    </asp:UpdatePanel>



  6. Add Code behind code



  7. protected void Page_Load(object sender, EventArgs e)
    {
    if (FileUploaderAJAX1.IsPosting)
    {
    HttpPostedFileAJAX pf = FileUploaderAJAX1.PostedFile;
    FileUploaderAJAX1.SaveAs("~/upload", pf.FileName);
    }
    }


Tuesday, February 26, 2008

Put ConfigSections on the top

Today, I just find the problem why my code can not detect my custom config sections. because I put config sections after <AppSettings> -)

Use Ctrl + . to Resolve in VS2008

Use Ctrl + . when you notice red underscore to Select a Resolve solution for you

Wednesday, February 20, 2008

Live Writer for Coder

These are plugins which will help me to write better content 

  • CodeSnippetSetup.msi - Wrap your code into a container
  • vspaste.msi - Copy paste from Visual studio
public static class HelloClass
{
public static void HelloWorld()
{
//Hi I just found this plugin makes our live easier for writing a blog...
}
}

Wednesday, February 13, 2008

Improve your AJAX Performance

Just quick note what I just got this morning from RDN with Damian.

  1. Disable compilation debug mode into false compilation debug="false"
    - This will decrease the javascript code downloaded from ajax frameworks

  2. If you don't use update panel, just using control toolkit,
    disable partial rendering on Script Manager
    EnablePartialRendering="false"
    - This improve the process for common postback, without checking update panel

  3. Try to saperate update panel as small as possible and put UpdateMode="Conditional".
    this will not send back other update panel content on partial rendering.
    By Default it sets to Always means it will send the content even not rendered.

  4. For using Not friendly AJAX control such as Telerik, Reporting Services Viewer which can not use update panel to do partial postback.
    You may put it into partial postback but add an attribute in Update Panel ChildrenAsTriggers="false". This will perform partial postback but not update your panel.

    so to update another panel you can do manually __doPostback(%= ID%, ...) or just handle it in the control.

    The ChildrenAsTriggers property determines whether postbacks from a child ontrol in an UpdatePanel result in its contents being refreshed. By default, this property is set to True and can be set to False only when the UpdateMode is set to conditional. Attempting to do so without this condition results in an InvalidOperationException being thrown by the ScriptManager during the page’s PreRender event.

  5. In my experience I also turn off EnableViewState and It works fine, but I don't know if this improve the performance

And Don't forget to use Web Development Helper for IE , but before that you may need to install IE Developer tool
http://msmvps.com/blogs/paulomorgado/archive/2007/02/18/developer-tools-for-internet-explorer.aspx

Tuesday, February 12, 2008

KeyValueConfigurationElement VS NameValueConfigurationElement

After checking on the description form MSDN, http://msdn2.microsoft.com/en-us/library/system.configuration.namevalueconfigurationelement(VS.80).aspx

It is written that
"
There is a difference between the NameValueConfigurationElement class and KeyValueConfigurationElement class. The NameValueConfigurationElement class does not require the first string to be unique, whereas KeyValueConfigurationElement class requires the first string to be a key, and therefore unique in the collection.
"

However when I try to test it, it seems that
  1. NameValue also can not display duplicate Names in the collection moreover if the name is the same but the value is different , it will throw an error message which says that name has already has the value.
  2. On the other hand KeyPair, also can not have duplicate keys but if there is duplicate key but different values, it will be override with the last values. There is no error message return.
  3. KeyPair collection will be sorted , I guess it similar with Hashtable collection but NameValue is not sorted.
  4. KeyPair is case sensitive so Key1 and key1 will be consider the same key
    but Namevalue is incase sensitive Name1 and name1 will be consider different name

For solving this problem, I have build my custom collection - can be downloaded at
http://kkurni.googlepages.com/KKurni.CustomConfigurationElementCol.rar

IConfigurationSectionHandler is deprecated on .NET 2.0

I just heard a bad news while reading MCTS Book and MSDN.

It said that IConfugurationSectionHandler is deprecated on .Net 2.0.

We need to inherits from ConfigurationSection instead of implementing IConfigurationSectionHandler.

Somehow it is easier using ConfigurationSection but it is less customization.
  1. We can not have CDATA or text Element anymore. All value need to be placed on attribute.
    Otherwise it will throw this exception
    "The configuration section cannot contain a CDATA or text element."

    If implementing IConfigurationSectionHandler we can easily using XmlElement Declaration . [XmlElement("ElementName")]

  2. for multiple element values , We need to write ConfigurationElementCollection.

    public class ValueCollection : ConfigurationElementCollection
    {
    protected override ConfigurationElement CreateNewElement()
    {
    return new ValueElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
    return ((ValueElement)element).Value;
    }
    }

    public class ValueElement : ConfigurationElement
    {
    [ConfigurationProperty("value")]
    public string Value
    {
    get { return (String)this["value"]; }
    set { this["value"] = value; }
    }
    }

    [ConfigurationProperty("domains")]
    public ValueCollection Domains
    {
    get { return (ValueCollection)this["domains"]; }
    }

    Or you can use default configuration collection such as NameValueConfigurationCollection or KeyValueConfigurationCollection


    But If using IConfigurationSectionHandler we can just use XmlElementAttribute
    [XmlElement("ExtractColumn")]
    public ExtractColumn[] ExtractColumns;

====================
However The nice thing of using ConfigurationSection is
  1. Standard .Net Configuration
  2. Easier to validate and better error Messages
    [ConfigurationProperty("myAttrib1", DefaultValue = "test", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]

Sunday, February 10, 2008

MVC Framework for ASP.NET is coming

From my previous blog, I just read that Microsoft will provide MVC Framework for AsP.Net Developer.

Here is the good link for MVC Framework
http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

Wednesday, February 6, 2008

MVC VS MVP vs Passive View

As you know MVC is a Model View Controller Pattern. which focus on the controller to update the view trigger by event when model changed/ updated.

MVP is the variation of MVC where controller can hang together with View (Called Presenter).
This makes the UI more interactive. such as in Asp.net control, user can interact with the model interactively using the presenter.

Passive view is also the variation of both. It will enhance the testability. It does not have dependencies with View and Controller and all user responses are handled by the controller

Free sorting with CSS Friendly and Object Datasource

I just find a good article where you can implement sorting and paging easily using object data source.

Object data source will not do binding everytime postback anymore and It instantiate inside your page control.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!567.entry#post



All the presentation is implemented using CSS Friendly.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!562.entry

Tuesday, February 5, 2008

Trigger Javascript inside Ajax Partial Postback called

To run / trigger a javascript after ajax partial postback, We need to register our script in our code behind.

Here is the code :
//To find your script manager which belong to master pages
ScriptManager sm = Master.FindControl("ScriptManager1") as ScriptManager;

ScriptManager.RegisterStartupScript(this, this.GetType(), "title script", string.Format("javascript:window.open(\"{0}\",\"_blank\");", "kajaxnet.blogspot.com"), true);

Monday, February 4, 2008

Dig into Data Source

Data Source (Object Data Source and Sql Data Source) in .net 2.0 includes free sorting and paging. Here is a very good example the benefit of using data source.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!562.entry

Using Data Source we can use CSS friendly to structure and display our sorting and paging nicely.


However there is overhead when you implementing AJAX type of work.
  1. DataSource will trigger your select method, everytime, it is postback.
    If you use Data binding using data table / other data source , you can control manually so that it only bind when you need to refresh your data or there is data changes.

  2. Object Data Source is isolated from your page control. because It create a new instance of data source every time it postback. so you can not share your data into your page control. If you want to share you need to share it using session/ cache. View state/ class variable can not be shared.

Dynamically sorting your data easily using Linq & Lamda

Here is 1 line of code to sort data dynamically based on sorting parameter A

YourProducts.OrderByDescending(p => p.GetType().GetProperty(A.GetValue(c,null))

It will get your property value of Product class dynamically.

Here are some example to use lamda expression in C# 3.0:

x => x + 1 // Implicitly typed, expression body

x => { return x + 1; } // Implicitly typed, statement body

(int x) => x + 1 // Explicitly typed, expression body

(int x) => { return x + 1; } // Explicitly typed, statement body

(x, y) => x * y // Multiple parameters

() => Console.WriteLine() // No parameters

Sunday, February 3, 2008

TFS vs Others

Here is some of the features that I found in TFS but not in other version controls (CVS and SVN)
  • Rename - It will automatically rename your file without removing from CVS / SVN.
    Probably, Visual SVN also has this feature, But I never test it.

  • When Adding a project into source controls, It will efficiently exclude the bin folder and other files that will dynamically created. This will make source control very efficient. On the other hand, if using CVS/ SVN, you need to exclude the bin folder manually.

    TFS knows .net source files better than other source control.

  • TFS has integrated with VS nicely. more than Visual SVN.

  • TFS has integrated with Development Pattern methodology such as MSF Agile and CMMI
    More over you can add your custom development pattern and process.

  • It integrate with Sharepoint portal which enhance the functionality as document management and documentation process such as WIKI and Blogs.

TFS on VS 2008 Beta 2 - Adding a lost files

After playing around with TFS on VS 2008, I found a difficulties to add a new file which I have created outside VS 2008.

In another scenario may happen if you want to recover unintentionally delete and at the same time merge with the previous version.

Somehow, It displays an error after I merge my lost file which says that I need to choose the server version or my version. But I does not have an option to do that.

However after fiddling with it for 1 hour, Finally I found the easiest way to dealing with those issues.

  1. In source control , Right Click on directory which contains your new file or file that you want to recover.
  2. select Compare.
  3. TFS will list all new files or lost files and you can easily added into your source control.

Load Testing on AJAX Application

Sometimes we need to perform a web test and load test for AJAX Application especially in partial postback.

VS 2005 Test edition / Team System provides a functionality to perform a web test. However the limitation is , it can not capture a partial postback.

For that reason we need one of the web capture tool called fiddler to capture all ajax request and response and we save it as Visual studio web test. and After that we can perform our ajax Load Test using VS 2005 Test edition / Team system.

Here are the steps

Before we perform load test, we need to create a web test first.

You may create multiple web tests to perform different scenario test cases.

In order to test an AJAX, you need to capture using Fiddler.

Here are the steps:

  1. Open Fiddler application.

  2. Open Internet Explorer and perform a scenario of your test. Fiddler will automatically log your request and response even for partial Post back /AJAX

  3. Select All fiddler capture session responses and requests

  4. On Fiddler menu File>Save>Session(s)>as Visual Studio Web Test

  5. Import this web test into your VS Test project.

  6. And it's ready to perform load test by creating a load test file inside VS
Note: You may set a windows credential in your web test.
  • There is an icon on the top tool bar of your web test.