|
|
Code Walkthroughs |
|
Datagrid
Formatting the Data
We are
able to format the content of the datagrid cell by one of two simple methods,
dependant upon whether the column is a bound column or whether it is a template
column. In our example we shall format the column to have to digits after the
decimal point , followed by a...
|
|
Datagrid
Highlight a Row With Click Through
It is
relatively easy to add alternating colours to the rows in your datagrid.
However, when we move the mouse over the rows we may want to highlight this
row, and possibly to add the option of a click through based on the row
selected...
|
|
Add
a Delete Button to a Datagrid
To add
a delete button to a datagrid follows a similar process to adding an edit
button. In the datagrid header...
|
|
Add
an Edit Button to a Datagrid
The
datagrid has a predefined editColumn for handling the editing of a datagrid.
Adding this simple column definition to a datagrid adds a powerful feature.
When a row is not in edit mode the column item shows the word...
|
|
Making
a Datagrid Row Editable
Two of
the most popular methods of editing a datagrid in asp.net are to either select
the row and take the user off to a different presentation of the data, or to
change the formatting of the row presented in the database with appropriate
edit text boxes, checkboxes and...
|
|
Adding
Tooltips to Datagrid Rows
Adding
tooltips to datagrid rows is easy, assuming that you have already created the
code for adding row highlighting. In this article I shall assume that you have
already read the article entitled Datagrid Highlight a Row With Click
Through...
|
|
Binding
a Datagrid to an Access Database
This
list covers the full lifecycle of a content management system, from initially
creating the content, through to delivering it to end users...
|
|
Adding
Data to a DropDownList
The aim
of this article is to answer the question 'How do I add items to a
DropDownList?' Initially as part of the declaration for the DropDownList we can
also define a number of items, much in the same way as in classic ASP...
|
|
Getting
Current Date Time
In
classic ASP we had now() which would return the current date and time. For
asp.net this no longer exists. So what should we use...
|
|
Test
if File Exists
Sometimes,
in order to reduce our chance of error, when working with the filesystem in
ASP.NET, we need to determine wether a file exists before performing an action
on it. The following short piece of code will enable us to test whether a file
exists...
|
|
Using
Javascript with ASP.NET Form Elements
Adding
simple pieces of Javascript to an Asp.net page can be acheived by adding to the
attributes of the particular imagebutton or linkbutton. if its normal ASP.Net
Button then you can...
|
|
Regular
Expressions
In the table below we list the characters used in .Net
regular expressions, together with their meaning, But first...
|
|
Authentication
in Asp.net
Forms
authentication in ASP.Net is far more easier and safe than Asp 3. It is
possible to place a web.config file in any directory of a web site.Therefore,
we are able to make most of a web site public, whilst providing authentication
on, say, one directory...
|
|
|
8.1 How to display alphabetically sorted data in Repeater?
|

 |
<asp:Repeater id="Repeater1"
runat="server">
|
<%#GetFirstAlphabet(DataBinder.Eval(Container.DataItem,
"LastName").ToString())%>
|
<%#DataBinder.Eval(Container.DataItem,
"LastName").ToString()%>
|
,<%#DataBinder.Eval(Container.DataItem,
"FirstName").ToString()%><br>
|
If Not Page.IsPostBack Then
|
'Populate
the Data in the Repeater
|
protected function
GetFirstAlphabet(ByVal strval As String) As String
|
Dim alphabet As String =
ViewState("alphabet")
|
If alphabet = Left(strval, 1) Then
|
alphabet
= Left(strval, 1)
|
ViewState("alphabet")
= alphabet
|
//Populate
the Data in the
Repeater
|
protected string
GetFirstAlphabet(string strval)
|
string
alphabet =(string) ViewState["alphabet"];
|
if(
alphabet == strval.Substring(0,1) )
|
alphabet
= strval.Substring(0,1);
|
ViewState["alphabet"]
= alphabet;
|
8.2 How to display the Directory names in the Repeater Control?
|

 |
<asp:Repeater id="Repeater1"
runat="server">
|
<%#
DataBinder.Eval(Container.DataItem, "Name").ToString() %>
|
Dim dir As New
DirectoryInfo(Server.MapPath("."))
|
Repeater1.DataSource =
dir.GetDirectories
|
DirectoryInfo dir = new
DirectoryInfo(Server.MapPath("."));
|
Repeater1.DataSource =
dir.GetDirectories();
|
8.3 How to define HeaderTemplate and ItemTemplate to the Repeater at runtime
TBD?
|

 |
Private Sub Page_Load(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
ds =
'Assign proper value to dataset
|
Dim
rpthdr As New TemplateBuilder
|
Dim
rptitem As New TemplateBuilder
|
For
Each dc In ds.Tables(0).Columns
|
strval
= dc.ColumnName & " "
|
strval
= strval & dc.ColumnName & " "
|
rpthdr.AppendLiteralString(strval)
|
Repeater1.HeaderTemplate
= rpthdr
|
Repeater1.DataSource
= ds
|
Function getData(ByVal objContainer
As System.Web.UI.WebControls.RepeaterItem) As String
|
Dim
strval As String = "<br>"
|
For i
= 0 To ds.Tables("table").Columns.Count - 1
|
strval
= objContainer.DataItem(i).ToString & " "
|
strval
&= objContainer.DataItem(i).ToString & " "
|
private void Page_Load(object
sender, System.EventArgs e)
|
TemplateBuilder
rpthdr = new TemplateBuilder();
|
TemplateBuilder
rptitem = new TemplateBuilder();
|
foreach
(int dc in ds.Tables(0).Columns)
|
strval
= dc.ColumnName + " ";
|
strval
= strval + dc.ColumnName + " ";
|
rpthdr.AppendLiteralString(strval);
|
Repeater1.HeaderTemplate
= rpthdr;
|
Repeater1.DataSource
= ds;
|
string
getData(System.Web.UI.WebControls.RepeaterItem objContainer)
|
for
(int i = 0; i <= ds.Tables("table").Columns.Count - 1; i++)
|
strval
= objContainer.DataItem(i).ToString + " ";
|
strval
+= objContainer.DataItem(i).ToString + " ";
|
strval
= strval + "<br>";
|
8.4 How to add a TemplateColumn dynamically to Repeater?
|

 |
Public Sub BindLabelColumn(sender As
Object, e As EventArgs)
|
Dim
lbl As Label = CType(sender, Label)
|
Dim
container As RepeaterItem = CType(lbl.NamingContainer, RepeaterItem)
|
Dim
strVals As String = Convert.ToString(DataBinder.Eval(CType(container,
RepeaterItem).DataItem, "LastName")) + ", " +
Convert.ToString(DataBinder.Eval(CType(container, RepeaterItem).DataItem,
"FirstName"))
|
public void BindLabelColumn(object
sender, EventArgs e)
|
Label
lbl = (Label)sender;
|
RepeaterItem
container = (RepeaterItem)lbl.NamingContainer ;
|
String
strVals =Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem,
"LastName"))
|
Convert.ToString(DataBinder.Eval(((RepeaterItem)container).DataItem,
"FirstName")) ;
|
8.5 How to implement sorting using Repeater?
|

 |
<asp:Repeater ID="Repeater1"
Runat="server" EnableViewState="False">
|
<table
width="100%" cellspacing="0" cellpadding="0">
|
<td
width=25%><%# DataBinder.Eval(Container.DataItem, "Employeeid")
%></td>
|
<td
width=25% ><%# DataBinder.Eval(Container.DataItem, "FirstName")
%></td>
|
<td
width=25% ><%# DataBinder.Eval(Container.DataItem, "LastName")
%></td>
|
<td
width=25% ><%# DataBinder.Eval(Container.DataItem, "Title")
%></td>
|
<table
width="100%" cellspacing="0" cellpadding="0">
|
<asp:LinkButton
ID="lnkEmployeeid" Runat="server"
OnClick="SortEmployeeIdClick">Employeeid</asp:LinkButton>
|
<asp:LinkButton
ID="lnkFirstName" Runat="server"
OnClick="SortFirstNameClick">FirstName</asp:LinkButton>
|
<asp:LinkButton
ID="lnkLastName" Runat="server"
OnClick="SortLastNameClick">LastName</asp:LinkButton>
|
<asp:LinkButton
ID="lnkTitle" Runat="server"
OnClick="SortTitleClick">Title</asp:LinkButton>
|
Dim myconnection As SqlConnection
|
Dim myda As SqlDataAdapter
|
Dim SQLStmt As String = "Select *
from Employees "
|
Private Sub Page_Load(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
' Put
user code to initialize the page here
|
strConn="Server=localhost;uid=sa;password=;database=northwind;"
|
myconnection
= New SqlConnection(strConn)
|
myda =
New SqlDataAdapter(SQLStmt, myconnection)
|
myda.Fill(ds,
"AllTables")
|
Repeater1.DataSource
= ds
|
Sub SortData(ByVal SortExpression As
String)
|
If
ViewState("SortOrder") Is Nothing Then
|
ViewState("SortOrder")
= " ASC"
|
If
ViewState("SortOrder").ToString() = " ASC" Then
|
ViewState("SortOrder")
= " DESC"
|
ViewState("SortOrder")
= " ASC"
|
SQLStmt
= SQLStmt + " ORDER BY " + SortExpression.ToString() + " " +
ViewState("SortOrder")
|
Protected Sub
SortEmployeeIdClick(ByVal sender As Object, ByVal e As EventArgs)
|
End Sub 'SortEmployeeIdClick
|
Protected Sub
SortFirstNameClick(ByVal sender As Object, ByVal e As EventArgs)
|
End Sub 'SortFirstNameClick
|
Protected Sub
SortLastNameClick(ByVal sender As Object, ByVal e As EventArgs)
|
End Sub 'SortLastNameClick
|
Protected Sub SortTitleClick(ByVal
sender As Object, ByVal e As EventArgs)
|
SqlConnection myconnection ;
|
string SQLStmt= "Select * from
Employees ";
|
private void Page_Load(object
sender, System.EventArgs e)
|
// Put
user code to initialize the page here
|
strConn="Server=localhost;uid=sa;password=;database=northwind;";
|
myconnection
=new SqlConnection(strConn);
|
myda =
new SqlDataAdapter(SQLStmt, myconnection);
|
myda.Fill(ds,
"AllTables");
|
Repeater1.DataSource
= ds;
|
void SortData(string SortExpression)
|
if
(ViewState["SortOrder"] ==null)
|
ViewState["SortOrder"]
= " ASC";
|
else
if (ViewState["SortOrder"].ToString () == " ASC" )
|
ViewState["SortOrder"]
= " DESC";
|
ViewState["SortOrder"]
= " ASC";
|
SQLStmt
= SQLStmt + " ORDER BY " + SortExpression.ToString () + " " +
ViewState["SortOrder"];
|
protected void
SortEmployeeIdClick(object sender ,EventArgs e )
|
SortField
= "EmployeeId";
|
protected void
SortFirstNameClick(object sender ,EventArgs e )
|
protected void
SortLastNameClick(object sender ,EventArgs e )
|
protected void SortTitleClick(object
sender ,EventArgs e )
|
8.6 Can I specify ItemStyle to a Repeater Control?
|

 |
|
The Repeater class is not derived from the WebControl class, like the DataGrid
and DataList. Therefore, the Repeater lacks the stylistic properties common to
both the DataGrid and DataList. What this boils down to is that if you want to
format the data displayed in the Repeater, you must do so in the HTML markup.
i.e using <b>/<u>.. tags
|
8.7 How to change the row color of the Repeater based on some condition?
|

 |
<asp:Repeater ID="Repeater1"
Runat="server" EnableViewState="False">
|
<tr
<%#
FormatColorRow(DataBinder.Eval(Container.DataItem,"UnitPrice").ToString())
%> >
|
<%#
DataBinder.Eval(Container.DataItem,"UnitPrice").ToString() %>
|
Protected Function
FormatColorRow(strUnitPrice As String) As String
|
Dim unitprice As Double =
Double.Parse(strUnitPrice)
|
Return
"style='backGround-color:red'"
|
Return
"style='backGround-color:green'"
|
End Function 'FormatColorRow
|
protected string
FormatColorRow(string strUnitPrice)
|
double
unitprice =double.Parse ( strUnitPrice);
|
return
"style='backGround-color:red'";
|
return
"style='backGround-color:green'";
|
|
|
|