ASP.NET and Javascript

on Wednesday, April 8, 2009

Hi all,
The tip of the day now is how to create JavaScripts and associate it to your ASP.NET pages in Runtime.

Many times we face problems regarding passing server side values to a javascript function.
Why you don`t try to create your javascript functions in runtime?

Ya, it`s possible using the RegisterClientScriptBlock function. Let`s take a look:

The sintax is:
RegisterClientScriptBlock (type, key, script, addScriptTags);

Let`s take a look on the parameters:
type - The type of the client we`ll register, in most of cases will be our own ASPX page;
key - Keyword that allow you to get your function programatically;
script - Your JavaScript source;
addScriptTags - Boolean parameter that indicates if the function will include the tags
automatically in your JavaScript function.

Wow! easy!
Let`s take a look on how it works in pratice.

Create a new ASPX page and add these controls:


<asp:TextBox ID="txtPhrase" runat="server" Text="Hello ASP.NET and JavaScript" />
<br />
<asp:Button
ID="btnExecuteScript"
runat="server"
OnClientClick="javascript:return AlertHello();"
Text="Execute JavaScript function" />


Basically we have a textbox and a button control.

In the button we have an Event on OnClientClick calling a javascript function "AlertHello()".
We will not add this JavaScript function here (.ASPX page), we`ll add it in runtime.

Go to your C# (VB too) code and let`s start to code:

protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"function AlertHello() { alert('" + txtPhrase.Text + "'); }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myScript, true);
}


Create a string to hold your JavaScript function and then register your function with the RegisterClientScriptBlock function.

Do you remember the parameters?
RegisterClientScriptBlock (type, key, script, addScriptTags);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myScript, true);

Cool..
Run your page and you`ll get a textbox and button...
Fill your textbox with some text and click the button..

You`ll be alerted by a javascript.

It`s cool, ìt`s simple... it`s .NET
Making things easy!

have a good one!

C# - Getting the first and last day of a month

on Friday, April 3, 2009

Hi folks..
Here comes a simple tip that can be useful...


01. DateTime myDate = DateTime.Parse("10/28/1985");
02. DateTime firstDay = DateTime.Parse(myDate.Month + "/01/" + myDate.Year);
03. DateTime lastDay = firstDay.AddMonths(1).AddDays(-1);
04. Console.WriteLine("Current Date: {0}", myDate.ToShortDateString());
05. Console.WriteLine("First Day: {0}", firstDay.ToShortDateString());
06. Console.WriteLine("Last Day: {0}", lastDay.ToShortDateString());
07. Console.ReadKey();

In the first line we have a simple creation of DateTime var with the value 10/28/1985 (My birthday =D)
To convert your date to be the first day of each month is simple as showed in line 2. Just split your data and inform the day part as 01, that will ever be the first day of the month (Or not?)
Now comes the challenge... there are some months that have 30, 31, 29 and 28 days. How can we discover which is the last day of these months?
Easy... pick your date (Must be converted to the first day of the month), add one month using the method AddMonth...
At this time you'll have for exaple "10/01/1985" + 1 month = "11/01/1985"
Then, subtract one day of this date with the method AddDays (Yes it accepts negative numbers).
The AddDays method will take care to remove one day of your date, and if your current date is the first day of the month, it will automatically go to the last day of the previous month, like this:

10/01/1985 + 1 Month = 11/01/1985 - 1 Day = 10/31/1985
This is what the line 03 do!

This is cool, this is .NET!
Make things easy!

see ya!

Andre Baltieri
andre.baltieri@insidedotnet.com.br