Labels

Tuesday, April 29, 2008

GridView Tips and Tricks

GridView Tips and Tricks using ASP.NET 2.0

Insert Row into Gridview without modifiying datasource

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If _SortedFlag Then

Dim table As Table = DirectCast(grid1.Controls(0), Table)

Dim htLookUp As New Hashtable

For Each row As GridViewRow In grid1.Rows

Dim realIndex As Integer = table.Rows.GetRowIndex(row)

Dim text As String = row.Cells(_SortColumnIndex).Text

If Not htLookUp.ContainsKey(text) Then
htLookUp.Add(text, Nothing)

Dim newHeaderRow As New GridViewRow(realIndex, realIndex, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim newCell As New TableCell()
newHeaderRow.Cells.Add(newCell)
newCell.ColumnSpan = grid1.Columns.Count
newCell.BackColor = Drawing.Color.Gray
newCell.ForeColor = Drawing.Color.White
newCell.Font.Bold = True
newCell.Text = _SortColumnHeader & " : " & text

table.Controls.AddAt(realIndex, newHeaderRow)

End If
Next

End If
MyBase.Render(writer)
End Sub

Monday, April 28, 2008

Multi-Header GridView

1. Add Header to existing header
Ref:http://www.codeproject.com/KB/aspnet/Merge_Header.aspx

2. Overwrite Delegates

Friday, April 25, 2008

JavaScripts

function isJavaEnabled()
{
var rc = false;
var agent = navigator.appName;
ver= parseInt(navigator.appVersion);
if (agent == "Microsoft Internet Explorer") {
if ((ver >= 4) && navigator.javaEnabled())
rc = true;
} else {
for (i=0; i < navigator.plugins.length; i++)
rc = rc || (navigator.plugins[i].name.toUpperCase().indexOf("JAVA") != -1);
}

return rc;
}

function ensureScriptLoad(){
if(typeof(lpConnLib)=='undefined'){
setTimeout('ensureScriptLoad()',200);
}else{
//send visitorWantsToChat
vaParams = new hcArrayStorage();
vaParams.add("cmd", "visitorWantsToChat");
vaParams.add("isOn", hasJavaSupport);
addChatParameters( vaParams );
addRequestToQueue( vaParams );
}
}

function isEnter(evt)
{
var keyCode = document.layers ? evt.which : evt.keyCode;
if (keyCode == 13)
{
SubmitForm();
return false;
}
return true;
}

setTimeout("loopVisitorTyping()",1000);

Wednesday, April 23, 2008

JavaScript in ASP.NET

1. Adding simple JavaScript to a Server Control
<body onload="javascript:document.forms[0]['Button1'].value=Date();">
<body onload="javascript:document.forms[0]['Button1'].value=Date();">
Button1.Attributes.Add("onclick", "javascript:alert('ALERT ALERT!!!')")
Body1.Attributes("onload") = "document.forms[0]['TextBox2'].focus();"

2. Using Larger JavaScript Functions
<strong>RegisterStartupScript</strong> and the <strong>RegisterClientScriptBlock</strong> methods
Page.RegisterStartupScript("MyScript", "<script language="javascript">function AlertHello() { alert('Hello ASP.NET'); }</script>")
Button1.Attributes("onclick") = "AlertHello()"
Button2.Attributes("onclick") = "AlertHello()"

The main <strong>difference</strong>: RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing </form>element. The RegisterClientScriptBlock method places the JavaScript directly after the opening
<form><p>element in the page.

3. Keeping JavaScript in a Separate File (.js)
Page.RegisterClientScriptBlock("MyScript", "<script language="javascript" src="'MyJavaScriptFile.js'">")
</p></form>

Best for Java function:Directly write in .aspx file
<script type="text/javascript">
function EmailValidation<%= CustomValidator1.ClientID %>(source, arguments)
{
var obj1=document.getElementById("<%= txtBrokerID.ClientID %>");
var obj2=document.getElementById("<%= txtEmail.ClientID %>");
if (obj1.value.length>0)
{
if (obj2.value.length>0)
arguments.IsValid=true;
else
arguments.IsValid=false;
}
}
</script>

Reference:
ASP.NET Client-Side Script FAQ

Tuesday, April 15, 2008

GridView FormatStrings

You can supply format strings to the columns in your GridView by setting the DataFormatString property of the column to something like this: “{0:d}”. However, you may have run across a case where you set this property and the system does not recognize the format string you provided. To make this work, you need to set the HtmlEncode property of that column to “False”. Then the DataFormatString will act as expected when displayed at run-time.

HtmlEncode="false"
DataField="AmountDue"
DataFormatString="{0:#,###.00;(#,###.00);-}

Format as Date:
{0:yyyy-MM-dd}

format as currency:
<%# Server.HtmlEncode( Eval("Price", "{0:c}").ToString() ) %>

<%# Server.HtmlEncode( Eval("Price", "{0:c}").ToString() ) %>

GridView control - Insert or Add

GridView control won't display anything if no data from source.

Best practise: Create seprate add button to display a add panle to insert or Add
Alternative:

1. Using JavaScript to add blank row to GridView when click on Add button
<script type="text/javascript">
function AddNewRow() { var tblGv =document.getElementById('myTable'); var newRow =tblGv.insertRow(); //Adding three columns to the row var firstCell = newRow.insertCell(); var secondCell = newRow.insertCell(); firstCell.innerHTML = "<input type=textbox id='txtName1' >, <input type=textbox id='txtName2' >"; secondCell.innerHTML = "<input type=textbox name ='txtName' id='txtName' >";}
2. Add fake data in data source when click on Add button
3. Rrewrite GridView to display footer even no source data

DataField in objectdatasource

When bind Object datasource to GridView, can't set DataFiled as a child property of property of datasource

Validation in ASP.NET

1. Validation Control provide both client side and server side validation
2. Using AJAX as validation
3. ClientScript.RegisterStartupScript

Error Handling in ASP.NET

1. Try Catch block
2. Page_Error Event
3. Application_Error Event