Friday, September 30, 2011

Simple javascript to compare date


function CompareDate()
{
var Date1 = document.getElementById('uxDate');
var SplitDate1 = Date1.value.split("/");
var dd1=SplitDate1[1];
var mm1=SplitDate1[0];
var yy1=SplitDate1[2];
var startDate = new Date(yy1,mm1-1,dd1);
var endDate = new Date();

if(startDate >= endDate)
{
alert("Start date should not greater then end date");
return false;
}
else
{
return true;
}

}

Create XML file in javascript from Gridview

Please find below JS for creating xml file from grid using javascript




function CreateXMLFromGrid()
{

var gvControl = document.getElementById('<%= grid1.ClientID %>');
var gvControlID = '<%= grid1.ClientID %>';
// hidden id so xml is stored here
var uxhdnXML = document.getElementById('<%= uxhdnXML.ClientID %>');

var strOut;
strOut = "";

for (i = 0; i <= gvControl.rows.length - 2; i = i + 1)
{
// will set grid row index
if (parseInt(i + 2) <= 9)
midfix = '0' + parseInt(i + 2);
else
midfix = parseInt(i + 2);

// text box control in grid
var txtbox = document.getElementById(gvControlID + '_ctl' + midfix + '_txt1');
// Drop down list control in grid
var ddllist = document.getElementById(gvControlID + '_ctl' + midfix + '_ddl1');
// Radio button list control in grid
var rblist = document.getElementById(gvControlID + '_ctl' + midfix + '_rdl1');


var resvalue = null;

if (txtbox != null)
{
resvalue = txtbox.value;
if (resvalue == "")
{
resvalue = "NULL";
}
}

if (ddllist != null)
{
resvalue = ddllist.value
if (resvalue == "")
{
resvalue = "NULL";
}
}

if (rblist != null)
{
// below will check from dropdown list weather radio button is checked or not

var checkBoxArray = rblist.getElementsByTagName("input");
for (var j = 0; j < checkBoxArray.length; j++)
{
if (document.getElementById(gvControlID + '_ctl' + midfix + '_rdlSetupvalue' + '_' + j.toString()).checked == true)
{
var checkBoxRef = checkBoxArray[j];
var labelArray = checkBoxRef.parentNode.getElementsByTagName("label");
resvalue = labelArray[0].innerHTML;
break;
}
}
if (resvalue == "")
{
resvalue = "NULL";
}
}

strOut = strOut + "";
strOut = strOut + "" + resvalue + "";
strOut = strOut + "
";

}

strOut = strOut + "
";
// assign created xml to hidden value
uxhdnXML.value = strOut;

}

Friday, August 26, 2011

Maintain scroll position after postback in Asp.Net

Way 1.


Write below mention directive in page directive section of html source of aspx page to maintain scroll position of only one page or selected pages rather then whole web application.


Page Language="VB" AutoEventWireup="false" MaintainScrollPositionOnPostback="true" CodeFile="Default.aspx.vb" Inherits="_Default"

Way 2.

To maintain scroll position programmatically use code mentione below.

System.Web.UI.Page.MaintainScrollPositionOnPostBack = true;

Way 3.

To maintain scroll position application wide or for all pages of web application we can write below mentioned code in pages section of web.config file so that we don't need to add page directive in each and every page.

in web.config page section add maintainScrollPositionOnPostBack="true"

Saturday, April 2, 2011

Remove Duplicates from Generic list

Hi Guys,

Please find below function to remove duplicate irems/objects from generic list

Tuesday, February 22, 2011

.Net 4.0 New Features

1. Setting Meta Tags

Setting Meta Tags with the Page.MetaKeywords and Page.MetaDescription Properties
Two properties have been added to the Page class: MetaKeywords and MetaDescription. These two properties represent corresponding meta tags in the HTML rendered for a page, as shown in the following example:

Keywords="This is the default page" Description="This is the default page" in page or code file

These two properties work like the Title property does, and they can be set in the @ Page directive. For more information, see Page.MetaKeywords and Page.MetaDescription.

Wednesday, February 2, 2011

Copy To Clipboard

Just call below javascript in your code. it will copy div tag data to excel sheet

function copyToClipboard(){
//document.getElementById('copyDiv').focus();
var text = document.getElementById('copyDiv').innerHTML;
if (window.clipboardData.setData("TEXT", text)){
alert("switch to excel and click paste");
}else{
alert("Oops, that didn't work?");
}
}

Friday, January 7, 2011

Creating Windows Service

Create Simple Windows Service

-- Open Studio 2010 >> File >> New >> Project >>
-- Select Visual C# >> Windows >> Windows Service
-- Gave Name to "TestWinService"
-- In .cs file there is two functions where service start/stop
protected override void OnStart()
{
}
protected override void OnStop()
{
}

-- we can use two approch to implement service to run

1) Using System.Timers

// initialize timer object
System.Timers.Timer timertest;

protected override void OnStart()
{
EventLog.WriteEntry("Timer Started at " + DateTime.Now, System.Diagnostics.EventLogEntryType.SuccessAudit);
timertest = new System.Timers.Timer();

this.timertest.Interval = Convert.ToDouble("12000");
this.timertest.Enabled = true;
this.timertest.Elapsed += new System.Timers.ElapsedEventHandler(timertest_Tick);
}


void timertest_Tick(object sender, EventArgs e)
{
try
{
// Service Logic

}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
}
}

protected override void OnStop()
{
timertest.Enabled = false;
}

2) Using Thread

System.Threading.Thread workerThread1;

protected override void OnStart(string[] args)
{

System.Threading.ThreadStart st1 = new System.Threading.ThreadStart(FunctionServicelogic);
workerThread1 = new Thread(st1);
// set flag to indicate worker thread is active
service1Started = true;
// start the thread
workerThread1.Start();
}

protected override void OnStop()
{
service1Started = false;
}

private void FunctionServicelogic()
{
while (service1Started)
{
// Service logic
if (service1Started)
{
Thread.Sleep(new TimeSpan(0, 10, 0));
}
}
// time to end the thread
Thread.CurrentThread.Abort();
}

--- You can Build service. Service build sucessfully

--- In the Properties dialog box, click Add Installer.
--- In the Properties dialog box for ServiceInstaller1, change the ServiceName property to LogWriterService.
--- In Design view, click ServiceProcessInstaller1 in the Code Editor.
--- In the Properties dialog box, change the Account property to LocalSystem. The LocalService value and the NetworkService value are only available in Microsoft Windows XP and later operating systems.

-------- Install Windows Service -----------
1) Command Prompt C:\Windows\Microsoft.Net\Framework\v2.0.50727\installutil -i "Service.exe path"
-- -i is for install
-- -u is for uninstall

-------- Varify Windows Service ------------

--- Click Start, point to Control Panel, point to Administrative Tools, and then click Services.
--- Right-click LogWriterService, and then click Start.
--- To verify that an event is logged in the event log, use one of the following methods:
Click Start, point to Control Panel, point to Administrative Tools, and then click Event Viewer. In the left pane, click Application Log. In the right pane, locate the event log for your service.