Explore and Learn ASP.NET 4.5

I have written few posts on ASP.NET 4.5 and Visual Studio 11 that . All the post featured as Article of the day at Microsoft official website http://www.asp.net/community. I mainly focused Web development.

If you want to learn about new version of ASP.NET 4.5 and Visual Studio 11(Now Visual Studio 2012).

This post is part 1 of 2 series on Strongly Typed data controls and Model Binding in ASP.NET 4.5. In this post, I discussed about Strongly typed data control, Model Binding, filtering data, Value Providers and about writing own Custom value provider.

This is second and last of on the series of Strongly Typed data controls and Model Binding in ASP.NET. In this post I discussed the editing feature and custom validation attributes.

In this article, we discussed the support of Unobtrusive validation in ASP.NET 4.5. This reduces the rendered page size dramatically. Instead using  java-script function and other client side libraries. It uses HTML5 data-* attributes for all these validation.

In this post, we discussed the Request Validation enhancement made in ASP.NET 4.5. There are several times nowadays, we require to allow the user to script tag as Input. This is the backbone of many blog site. But if proper validation is in place then “A potentially dangerous request..”. Now ASP.NET 4.5 allows to partial unvalidated request to be submitted.I also discussed Lazy/Differed validation..

In this post, I discussed the enhance made in .NET 4.5 and ASP.NET 4.5 which allows you to write Asynchronous modules in very simple steps in the way you write the synchronous way. Asynchronous modules can be come a boon at certain situations for your Application performance..

As in Asynchronous HTTPModules, Asynchronous  HTTPHandlers can be written very easily with the version ASP.NET 4.5. It also can be a boos t for application performance..

This is one of the best things I like in Visual Studio 11. Page Inspector is integrated with VS 11 and provides the capabilities of web development tools and apart from this directly navigates you to the corresponding aspx/ascx/master… files also.

Hope you all will have fun while learning ASP.NET 4.5

Cheers,
Brij

Speaking on ASP.NET 4.5 new features

Hello friends,

I will be speaking on ASP.NET 4.5 new features in an online session organized by http://kolkatageeks.com/.

This would be a one hour session, In which I’ll be discussing on the enhancement made in Data Controls and the ModelBinding feature. It will be fully demo oriented session and you’ll enjoy it.

This is an online event and you can join from anywhere if you have PC/Laptop with internet connection.

If you have not registered yourself on http://kolkatageeks.com/ then Register and you’ll get the details.

Time : 20th May 2 PM to 3 PM

 This is absolutely free event.

Session details: http://kolkatageeks.com/WebCast.aspx

Online meeting details will sent registration.

Hope you’ll enjoy it..

Thanks,

Brij

How to access the input control’s data at Server side during Client Callback : A useful trick

Client callback is one way to update the webpage without performing the full page postback. It maintains the Client state while updating the Webpage. During Client callback the webpage runs through modifies version of Page Life Cycle.

The LifeCycle of a page in Client Call Back is  as

Page Lifecycle iin Callback

If you want to learn  more about Client Callback, click here.

But the main drawback of  Client Callback, The input data is not posted from Client to Server. Also,  It does not maintain the view state during partial postback as you can see the Page life cycle of Client callbak it is not saved or SaveViewState event is not fired.

Lets see it with an example.

I have created a simple form to collect some information of an person data. And it has a submit button, which initiates a Callback. As

 <table>
 <tr><td><asp:Label id="lblFName" runat="server" Text="First Name" /> </td>
 <td><asp:TextBox ID="tbFName" runat="server"></asp:TextBox></td></tr><tr>
 <td><asp:Label id="lblMName" runat="server" Text="Middile Name" /> </td>
 <td><asp:TextBox ID="tbMName" runat="server"></asp:TextBox></td></tr><tr>
 <td><asp:Label id="lblLName" runat="server" Text="Last Name" /></td><td><asp:TextBox ID="tbLName" runat="server"></asp:TextBox></td></tr><tr>
 <td><asp:Label id="lblAge" runat="server" Text="Age" /></td><td><asp:TextBox ID="tbAge" runat="server"></asp:TextBox></td></tr><tr>
 <td><asp:Label id="lblMailId" runat="server" Text="Email" /></td><td><asp:TextBox ID="tbEMail" runat="server"></asp:TextBox></td></tr><tr>
 <td><asp:Label id="lblSex" runat="server" Text="Sex" /></td><td><asp:TextBox ID="tbSex" runat="server"></asp:TextBox></td></tr><tr>
 <td colspan="2"><input id="btnCallBack" type="button" value="Submit" onclick="InitiateCallBack();"/></td></tr>
</table>

Server side code is as

public partial class Callback : System.Web.UI.Page,ICallbackEventHandler
{
    string result;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsCallback)
            return;
        //Creating a reference of Client side Method, that is called after callback on server
        String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg",
            "ReceiveServerData", "");

        //Putting the reference in the method that will initiate Callback
        String callbackScript = "function CallServer(arg, context) {" +
            cbReference + "; }";

        //Registering the method
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "CallServer", callbackScript, true);
    }
    public void RaiseCallbackEvent(String eventArgument)
    {
        string firstName = tbFName.Text;
        //Read other data as well

    }
    public string GetCallbackResult()
    {
        return result;
    }
}

And the client side method is

function InitiateCallBack() {
     //Intiate the callback
     CallServer("",'');
}

// Called after the server side processing is done
function ReceiveServerData(arg, context) {
    //arg: hold the result
    //Updating the UI
    document.getElementById('divCallbacl').innerHTML = arg;
}

After filling this form, I submitted it. Now if you try to access the entered using control’s property, you would not get it.

Let’s dig it more and check the form collection, whether the data is passed from Client to server or not

So as you can see that the data is not passed in form collection

But let’s put the following lines of code in java-script, just before initiating the call for Callback as

 function InitiateCallBack() {
	__theFormPostCollection.length = 0;
        __theFormPostData = "";
        WebForm_InitCallback();

        //Intiate the callback
        CallServer("",'');
}

Now let’s run the code and submit the form as earlier.

Now let’s check the form collection.

What a surprise, we got all the input data in form collection. That’s nice.

Now access the data using control’s property.

and you got it.

Now let’s try to see, what does the extra lines of code does

//This Clear the earlier the old data in form collection.
__theFormPostCollection.length = 0;
//It sets the forms data to empty
__theFormPostData = "";
//This method builds the body of the POST request when the page loads. The body of the page is a string 
//(that is __theFormPostData) filled with the contents 
of the view state and all of the input fields in the form.
WebForm_InitCallback();

But I would suggest to use these line at caution. Use only when you need to collect the update from Page during Callback. If you don’t need to get some data or it is in readonly page, don’t use it it introduce extra overhead to your page and costs at performance point of view.

Hope you  all have enjoyed the post.