Thursday, July 24, 2008

Compare Time using Custom Validator

Hi,

In ASP.Net, you can use Compare Validator to compare Date but you can't compare the Time.
For that you need to write a JavaScript function and should be called in the property 'ClientValidationFunction' of Custom Validator.

You can do it by writing code in CodeBehind, but you have to be patient for postback.
So, the code you will write InLine is :-

<head runat="server">

<title>Compare Time</title>

<script language="javascript" type="text/javascript">

function compareTimes(sender, args)

{

var difference = 0;

//Get the current date

var dd= new Date();

//Set the start time and end time in the format: 'MM/dd/yyyy textvalue'

startTime = new Date(dd.getMonth()+1 +"/"+dd.getDate()+"/"+dd.getFullYear()+" "+document.getElementById('<%=txtStartTime.ClientID%>').value);

endTime = new Date(dd.getMonth()+1 +"/"+dd.getDate()+"/"+dd.getFullYear()+" "+document.getElementById('<%=txtEndTime.ClientID%>').value);

//Compute difference and then convert to minutes

difference = endTime - startTime;

minutes = Math.round(difference/(1000*60));

//If the result is negative or zero,

if (minutes<=0)

{

//show error

args.IsValid = false;

return;

}

//Otherwise pass the error

args.IsValid = true;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

Start Time: <asp:TextBox ID="txtStartTime" runat="server" </asp:TextBox>

<br />

End Time: <asp:TextBox ID="txtEndTime" runat="server"></asp:TextBox>

<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="txtEndTime" ErrorMessage="Start Time can't be greater than or equal to End time" ClientValidationFunction="compareTimes">

</asp:CustomValidator>

<br />

<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click"
Text="Save" />

</div>

</form>

</body>

Tuesday, June 3, 2008

AssociatedControlID Property of Label Control

In ASP.Net Label control, there is a property called AssociatedControlID. This property changes the behaviour of the label control after rendering.

e.g. We have a label control...

<asp:Label ID="lblSearch" Text="Search" runat="server" />

After running the page, go to so View --> Source... and you will get like this :

<span id="lblSearch">Search</span>

This means label contol render to a simple html element.

So, after adding the AssociatedControlID property and taking a contol (I'm taking a text box) to be associated...

<asp:Label ID="lblSearch" Text="Search" runat="server" associatedcontrolid="txtSearch"/>
<asp:textbox id="txtSearch" runat="server"
/>


Now run the page and go to View --> Source...You will find something different.

<label id="lblSearch" for="txtSearch">Search</label>
<input id="txtSearch" name="txtSearch">

Now the Label control rendered a

Monday, May 26, 2008

Get the current version of ASP.Net

We can get the current version of ASP.Net running in our system by executing the following code on page load event.

protected void Page_Load(object sender, EventArgs e)
{

//Get the current version of asp.net running in your system
Response.Write(System.Environment.Version.ToString());

}