Total Pageviews

Monday, August 16, 2010

ASP.NET Page Life Cycle Events

Page Life Cycle Events:


1. PreInit: This is the first real event you might handle for a page. You typically use this event only if you need to dynamically (from code) set values such as master page or theme. This event is also useful when you are working with dynamically created controls for a page. You want to create the controls inside this event.

2. Init: This event fires after each control has been initialized. You can use this event to change initialization values for controls.

3. InitComplete: Raised once all initializations of the page and its controls have been completed.

4. PreLoad: This event fires before view state has been loaded for the page and its controls and before PostBack processing. This event is useful when you need to write code after the page is initialized but before the view state has been wired back up to the controls

5. Load: The page is stable at this time; it has been initialized and its state has been reconstructed. Code inside the page load event typically checks for PostBack and then sets control properties appropriately. The page’s load event is called first. Then, the load event for each child control is called in turn (and their child controls, if any). This is important to know if you are writing your own user or custom controls.

6. Control (PostBack) events: ASP.NET now calls any events on the page or its controls that caused the PostBack to occur. This might be a button’s click event, for example.

7. LoadComplete: At this point all controls are loaded. If you need to do additional processing at this time you can do so here.

8. PreRender: Allows final changes to the page or its control. This event takes place after all regular PostBack events have taken place. This event takes place before saving ViewState, so any changes made here are saved.

9. SaveStateComplete: Prior to this event the view state for the page and its controls is set. Any changes to the page’s controls at this point or beyond are ignored. This is useful if you need to write processing that requires the view state to be set.

10. Render: This is a method of the page object and its controls (and not an event). At this point, ASP.NET calls this method on each of the page’s controls to get its output. The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser. This method is useful if you are writing your own custom control. You override this method to control output for the control.

11. UnLoad: This event is used for cleanup code. You use it to release any managed resources in this stage. Managed resources are resources that are handled by the runtime, such as instances of classes created by the .NET common language runtime.

Friday, August 13, 2010

Sunday, August 1, 2010

SQL Server - Sending XML string to SP

Creating XML format to send to Parameters as string to the SP

StringBuilder sbSites = new StringBuilder();
sbSites.Append("");

if (ddlManagingSite.SelectedValue != string.Empty)
{
sbSites.AppendFormat("", ddlManagingSite.SelectedValue);
}
else
{
foreach (ListItem item in ddlManagingSite.Items)
{
if (item.Text != "")
{
sbSites.AppendFormat("", item.Value);
}
}
}

sbSites.Append("
");



SP need to look like this

CREATE PROC [dbo].[uspGetVacancyNoticeBoardByStateAndSite]

@vcSiteCode varchar(MAX)=''

AS

DECLARE @iDocSites INT
EXEC sp_xml_preparedocument @iDocSites OUTPUT, @vcSiteCode

SELECT
[ID],
[LastDataUpdateDate],
[LastDataUpdateBy]
FROM [dbo].[tblVacancyNoticeBoard]
WHERE [SiteCode] IN (SELECT SiteCodeID FROM OPENXML (@iDocSites, '/AllSITES/SiteCode', 1) WITH (SiteCodeID VARCHAR(5)))


Tuesday, July 20, 2010

Concatanating SQL table column

Table as follows

JSKID Tickets
8765 Bobcat
1001 Bobcat
1001 Confined Spaces
1001 Road

Turn this table to following

JSKID Tickets
1001 Bobcat,Confined Spaces,Road
8765 Bobcat

Select tk1.[JSKID], stuff((SELECT ',' + [Tickets] FROM [tblTickets] tk where tk.[JSKID]= tk1.[JSKID] FOR XML PATH('')),1,2,'' ) AS Tickets from [tblTickets] tk1 group by tk1.[JSKID]

Saturday, July 10, 2010

Read All appsetting on web.config

This will read all appsetting collection on web.config or app.config

NameValueCollection mySettings = System.Configuration.ConfigurationSettings.AppSettings;
string connStr = mySettings["connectionString"];

Thursday, July 1, 2010

SQL Server- Script to rebuild all tables' indexes

USE DatabaseName --Enter the name of the database you want to reindex

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR

SELECT table_name

FROM information_schema.tables

WHERE table_type = 'base table'

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@FETCH_STATUS = 0

BEGIN

DBCC DBREINDEX(@TableName,' ',90)

FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor

Printing a Div in JAVAScript - Extended Version

This works in IE and Firefox, Mutiple Divs can be printed.

function PrintAllDivs(printDiv) {
if (typeof HTMLElement != "undefined" && !
HTMLElement.prototype.insertAdjacentElement) {
HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {
switch (where) {
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode, this)
break;
case 'afterBegin':
this.insertBefore(parsedNode, this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling)
this.parentNode.insertBefore(parsedNode, this.nextSibling);
else this.parentNode.appendChild(parsedNode);
break;
}
}

HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr) {
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where, parsedHTML)
}


HTMLElement.prototype.insertAdjacentText = function(where, txtStr) {
var parsedText = document.createTextNode(txtStr)
this.insertAdjacentElement(where, parsedText)
}
}

var doc;
var contentDiv;
var content;

if (document.getElementById('printHiddenFrame') == null) {
document.body.insertAdjacentHTML("beforeEnd", "");
}
doc = printHiddenFrame.document;
doc.open();
doc.write("");
doc.write("");
doc.write("");
contentDiv = document.getElementById(printDiv);
if (contentDiv != null) {
doc.write(contentDiv.innerHTML);
}
ConvertToText(doc);
doc.write("");
setTimeout('document.getElementById(\'abc\')', 0);
doc.close();
return false;

}

function printHiddenPage() {

window.document.printHiddenFrame.focus();
window.document.printHiddenFrame.print();
}

function ConvertToText(doc) {
var f = doc.getElementsByTagName('textarea');

for (var i = 0; i < f.length; i++) {
var txt = doc.getElementById(f[i].id + 'Print');

f[i].style.display = 'none';
f[i].style.visibility = 'hidden';

if (txt != null) {
f[i].value == "" ? txt.innerHTML = " " : txt.innerHTML = f[i].value.replace(/\n\r/g, "

");
}
}
}