Thursday, December 30, 2010

ASP.NET Performance tip- Combine multiple script file into one request with script manager

We all need java script for our web application and we storing our JavaScript code in .js files. Now If we have more then .js file then our browser will create a new request for each .js file. Which is a little overhead in terms of performance. If you have very big enterprise application you will have so much over head for this. Asp.net Script Manager provides a feature to combine multiple JavaScript into one request but you must remember that this feature will be available only with .NET Framework 3.5 sp1 or higher versions.

Let’s take a simple example. I am having two javascript files Jscrip1.js and Jscript2.js both are having separate functions.

//Jscript1.js
function Task1() {
alert('task1');
}
Here is another one for another file.
////Jscript1.js
function Task2() {
alert('task2');
}
Now I am adding script reference with script manager and using this function in my code like this.
<form id="form1" runat="server">
<asp:ScriptManager ID="myScriptManager" runat="server" >
<Scripts>
<asp:ScriptReference Path="~/JScript1.js" />
<asp:ScriptReference Path="~/JScript2.js" />
</Scripts>
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
Task1();
Task2();         
</script>
</form>
Now Let’s test in Firefox with Lori plug-in which will show you how many request are made for this. Here is output of that. You can see 5 Requests are there.LoriPlugin

Now let’s do same thing in with ASP.NET Script Manager combined script feature. Like following

<form id="form1" runat="server">
<asp:ScriptManager ID="myScriptManager" runat="server" >
<CompositeScript>
<Scripts>
<asp:ScriptReference Path="~/JScript1.js" />
<asp:ScriptReference Path="~/JScript2.js" />
</Scripts>

</CompositeScript>
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
Task1();
Task2();         
</script>
</form>
Now let’s run it and let’s see how many request are there like following.LoriPlugin1

As you can see now we have only 4 request compare to 5 request earlier. So script manager combined multiple script into one request. So if you have lots of javascript files you can save your loading time with this with combining multiple script files into one request. Hope you liked it. Stay tuned for more!!!.. Happy programming..
Shout it
Share:
Wednesday, December 29, 2010

Programmatically updating one update panel elements from another update panel elements

While taking interviews for asp.net candidate I am often asking this question but most peoples are not able to give this answer. So I decided to write a blog post about this. Here is the scenario. There are two update panels in my html code in first update panel there is textbox hello world and another update panel there is a button called btnHelloWorld. Now I want to update textbox text in button click event without post back. But in normal scenario It will not update the textbox text as both are in different update panel. Here is the code for that.

<form id="form1" runat="server">
<asp:ScriptManager ID="myScriptManager" runat="server" EnableCdn="true"></asp:ScriptManager>
<asp:UpdatePanel ID="firstUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtHelloWorld" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="secondUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnHelloWorld" runat="server" Text="Print Hello World"
onclick="btnHelloWorld_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>

Here comes magic!!. Lots of people don’t know that update panel are providing the Update method from which we can programmatically update the update panel elements without post back. Below is code for that.

protected void btnHelloWorld_Click(object sender, System.EventArgs e)
{
txtHelloWorld.Text = "Hello World!!!";
firstUpdatePanel.Update();
}

That’s it here I have updated the firstUpdatePanel from the code!!!. Hope you liked it.. Stay tuned for more..Happy Programming..


Technorati Tags: ,

Shout it
Share:

Sending mail with Gmail Account using System.Net.Mail in ASP.NET

Any web application is in complete without mail functionality you should have to write send mail functionality. Like if there is shopping cart application for example then when a order created on the shopping cart you need to send an email to administrator of website for Order notification and for customer you need to send an email of receipt of order. So any web application is not complete without sending email. This post is also all about sending email. In post I will explain that how we can send emails from our Gmail Account without purchasing any smtp server etc.

There are some limitations for sending email from Gmail Account. Please note following things.

  1. Gmail will have fixed number of quota for sending emails per day. So you can not send more then that emails for the day.
  2. Your from email address always will be your account email address which you are using for sending email.
  3. You can not send an email to unlimited numbers of people. Gmail ant spamming policy will restrict this.
  4. Gmail provide both Popup and SMTP settings both should be active in your account where you testing. You can enable that via clicking on setting link in gmail account and go to Forwarding and POP/Imap.

So if you are using mail functionality for limited emails then Gmail is Best option. But if you are sending thousand of email daily then it will not be Good Idea.

Here is the code for sending mail from Gmail Account.

using System.Net.Mail;

namespace Experiement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender,System.EventArgs e)
{
MailMessage mailMessage = new MailMessage(new MailAddress("[email protected]")
,new MailAddress("[email protected]"));
mailMessage.Subject = "Sending mail through gmail account";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "<B>Sending mail thorugh gmail from asp.net</B>";

System.Net.NetworkCredential networkCredentials = new
System.Net.NetworkCredential("[email protected]", "yourpassword");

SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredentials;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Send(mailMessage);

Response.Write("Mail Successfully sent");

}
}
}

That’s run this application and you will get like below in your account.


SendMail


Technorati Tags: ,,
Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews