C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

10 Code Snippets for ASP.NET Developers


Posted Date: 05 Oct 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: DotNET-HelperMember Level: Gold    
Rating: 1 out of 5Points: 10





1. How to Freeze DataGrid header.



Define a Class Freezing in StyleSheet as

.Freezing
{
position:relative;
top:expression(this.offsetParent.scrollTop);
z-index=10;
}

Add HeaderStyle cssclass as Freezing..

Works only in IE 5.0 and Above

2. How to Freeze Columns of Datagrid



Define a Class FreezeCol in Stylesheet as

.FreezeCol
{
LEFT:expression(document.getElementById("freezingDiv").scrollleft);
POSITION: relative;
z-index: 1;
}
Assign Columns CssClass as FreezeCol.


3. Selecting a DataGrid row by Clicking Any Column



Step 1# Add a Select button (LinkButton or PushButton) to your datagrid (in first column) and make it Visible = false.
step 2 # In Page_Load event, Add:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'.......
End If
Page.RegisterHiddenField("originalCssClass", "")
'this hidden field is used to store original cssclass name
End Sub

step 3 # Assign cssclass to Datagrid item and alternate item




setp 4 # Add following code in DataGrid1_ItemCreated event handler:



Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then

Dim cssClassName As String = IIf(e.Item.ItemType = ListItemType.Item, "GridItem", "GridAltItem")
e.Item.Attributes.Add("onmouseover", "document.getElementById('originalCssClass').value='" & cssClassName & "';this.style.backgroundColor='Red';")
e.Item.Attributes.Add("onmouseout", "this.className=document.getElementById('originalCssClass').value;this.style.backgroundColor='';")

e.Item.Attributes.Add("onclick", "javascript:__doPostBack('" & "DataGrid1:" & "_ctl" & (e.Item.ItemIndex + 2) & ":_ctl0','')")
'Code Above: DataGrid1 is the name of the datagrid, you may need to change it to name of ur datagrid
' _ctl0: the Select Button is the column 0, you may change it if the select button is not in column 0.

End If

End Sub


4. Create a Date Picker Calendar Popup




Add a textbox (TextBox2) and a button (Button2) to TestCalendar.aspx that we created in solution 1
Add following code to code-behind of TestCalendar.aspx.

Private Sub OpenModalWindow(ByVal opener As WebControl, ByVal txtBox As WebControl, ByVal url As String, ByVal arg As String, ByVal opt As String)
Dim clientScript As New StringBuilder("")
clientScript.Append("var val= window.showModalDialog('" & url & "','" & arg & "','" & opt & "');" & NewLine)
clientScript.Append("if (val) {Form1." & txtBox.ClientID & ".value=val;}")
opener.Attributes.Add("onclick", clientScript.ToString)
End Sub

in Page_Load event add this:
OpenModalWindow(Me.Button2, Me.TextBox2, "CalendarModalDialog.aspx?DefaultDate= " & TextBox2.Text,"Calendar","dialogHeight= 240px;dialogWidth= 240px;center= Yes;")

Add a new webform page(CalendarModalDialog.aspx) to your project.
In HTML view of CalendarModalDialog.aspx, add following code between tag:


Add a calendar control (ID=Calendar1) to CalendarModalDialog.aspx, auto-format to the way you like. Add following code to Calendar1_SelectionChanged event:
Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
Page.RegisterStartupScript("closethepopup", "")
End Sub
Add following code in Page_Load event:

If Not IsPostBack Then
Try
Dim dte As String = Request.Params("DefaultDate")
Dim _dt As DateTime = Convert.ToDateTime(dte).ToShortDateString
Me.Calendar1.VisibleDate = _dt
Me.Calendar1.SelectedDate = _dt
Catch
End Try
End If
You are all set! The good thing is: it is a modal dialog, it will never lose the focus unless you explicitly close the dialog or pick the date.


5. Custom Control - Numeric Only Text Box




Public Class NumericTextbox
Inherits TextBox
Private m_AllowDecimal As Boolean = False

Public Property AllowDecimal() As Boolean
Get
Return m_AllowDecimal
End Get
Set(ByVal Value As Boolean)
m_AllowDecimal = Value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
If Not Page.IsClientScriptBlockRegistered("ValidateNumeric") Then
Dim _script As New StringBuilder("")
_script.Append(" ")
Page.RegisterClientScriptBlock("___NumericTextboxValidateNumeric", _script.ToString())
End If
Attributes.Add("onKeyPress", "_NumericTextbox_ValidateNumeric()")
MyBase.OnPreRender(e)
End Sub
End Class



6. Set Focus to Web Control




Private Function SetFocus(ByVal controlToFocus As Control) As String
' The SetFocusScript method set focus to a control on a page load
Dim sb As New StringBuilder
sb.Append(" ")
If Not Page.IsStartupScriptRegistered("focus") Then
Page.RegisterStartupScript("focus", sb.ToString())
End If
End Function

In the Page_Load add me.setFocus(Me.txtUserName) in Vb.net



7. CuStom Paging in T-SQL



For SQL Server T-SQL:
SELECT * FROM
(SELECT TOP {0} * FROM
(SELECT TOP {1} * FROM TableName
ORDER BY ColumnName) AS t1
ORDER BY ColumnName DESC) AS t2
ORDER BY ColumnName

{0}: Page Size
{1} = PageNumber * {0} (Note: PageNumber is 1-based)


8. How to get Referring Page..



Dim _urlReferrer as String
if Not Request.UrlReferrer is Nothing then
_urlReferrer = Request.UrlReferrer
Else
_urlReferrer = ""
End If


9. How to make a Datagrid Scrollable



Add a div tag as









Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: The Cool asp:xml control for displaying xml data.
Previous Resource: How to get the Selected Value of all List controls that are present in a Page
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use