Live Chat Icon For mobile
Live Chat Icon

ASP.NET FAQ

Find answers for the most frequently asked questions
Expand All Collapse All

Refer : How to add Templatecolumn dynamically to DataList?

Now, instead of DataList use Repeater and make below changes

VB.NET


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'))
	lbl.Text = strVals
End Sub ’BindLabelColumn 

C#


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')) ;
	lbl.Text  = strVals;
		
}
Permalink

VB.NET


Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        	ds = ’Assign proper value to dataset
        	Dim dc As DataColumn
        	Dim dr As DataRow
        	Dim rpthdr As New TemplateBuilder
        	Dim rptitem As New TemplateBuilder
        	Dim strval As String
        	For Each dc In ds.Tables(0).Columns
            		If strval = '' Then
                		strval = dc.ColumnName & ' '
            		Else
                		strval = strval & dc.ColumnName & ' '
            		End If
        	Next
        	rpthdr.AppendLiteralString(strval)
        	Repeater1.HeaderTemplate = rpthdr
        	Repeater1.DataSource = ds
        	Repeater1.DataBind()
End Sub

Function getData(ByVal objContainer As System.Web.UI.WebControls.RepeaterItem) As String
        	Dim i As Integer
        	Dim strval As String = '<br>'
        	For i = 0 To ds.Tables('table').Columns.Count - 1
            		If strval = '' Then
                		strval = objContainer.DataItem(i).ToString & ' '
            		Else
                		strval &= objContainer.DataItem(i).ToString & ' '
            		End If
        	Next
        	strval = strval & '<br>'
        	Return strval
End Function

C#


DataSet ds; 
private void Page_Load(object sender, System.EventArgs e) 
{ 
	ds = something; 
	DataColumn dc; 
	DataRow dr; 
	TemplateBuilder rpthdr = new TemplateBuilder(); 
	TemplateBuilder rptitem = new TemplateBuilder(); 
	string strval; 
	foreach (int dc in ds.Tables(0).Columns) 
	{ 
		if (strval == '') 
		{ 
			strval = dc.ColumnName + ' '; 
		} 
		else 
		{ 
			strval = strval + dc.ColumnName + ' '; 
		} 
	} 
	rpthdr.AppendLiteralString(strval); 
	Repeater1.HeaderTemplate = rpthdr; 
	Repeater1.DataSource = ds; 
	Repeater1.DataBind(); 
}

string getData(System.Web.UI.WebControls.RepeaterItem objContainer) 
{ 
	int i; 
	string strval = '<br>'; 
	for (int i = 0; i <= ds.Tables('table').Columns.Count - 1; i++) 
	{ 
		if (strval == '') 
		{ 
			strval = objContainer.DataItem(i).ToString + ' '; 
		} 
		else 
		{ 
			strval += objContainer.DataItem(i).ToString + ' '; 
		} 
	} 
	strval = strval + '<br>'; 
	return strval; 
}
Permalink

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

Permalink

<asp:Repeater ID='Repeater1' Runat='server' EnableViewState='False'>
	<ItemTemplate>
	<table width='100%' cellspacing='0' cellpadding='0'>
	<tr>
		<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>
	</tr>
	</table>
	</ItemTemplate>
	<HeaderTemplate>
	<table width='100%' cellspacing='0' cellpadding='0'>
	<tr>
		<td width=25%>
		<asp:LinkButton ID='lnkEmployeeid' Runat='server' OnClick='SortEmployeeIdClick'>Employeeid</asp:LinkButton>
		</td>
		<td width=25%>
		<asp:LinkButton ID='lnkFirstName' Runat='server' OnClick='SortFirstNameClick'>FirstName</asp:LinkButton>
		</td>
		<td width=25%>
		<asp:LinkButton ID='lnkLastName' Runat='server' OnClick='SortLastNameClick'>LastName</asp:LinkButton>
		</td>
		<td width=25% >
		<asp:LinkButton ID='lnkTitle' Runat='server' OnClick='SortTitleClick'>Title</asp:LinkButton>
		</td>
	</tr>
	</table>
	</HeaderTemplate>
</asp:Repeater>

VB.NET


Dim SortField As String
Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
Dim strConn As String
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
        	BindData()
End Sub ’Page_Load


Sub BindData()
        	strConn = 'Server=localhost;uid=sa;password=;database=northwind;'
        	myconnection = New SqlConnection(strConn)
        	myda = New SqlDataAdapter(SQLStmt, myconnection)
        	ds = New DataSet
        	myda.Fill(ds, 'AllTables')
        	Repeater1.DataSource = ds
        	Repeater1.DataBind()
End Sub ’BindData

Sub SortData(ByVal SortExpression As String)
        	If ViewState('SortOrder') Is Nothing Then
            		ViewState('SortOrder') = ' ASC'
        	Else
            		If ViewState('SortOrder').ToString() = ' ASC' Then
                		ViewState('SortOrder') = ' DESC'
            		Else
                		ViewState('SortOrder') = ' ASC'
            		End If
        	End If
        	SQLStmt = SQLStmt + ' ORDER BY ' + SortExpression.ToString() + ' ' + ViewState('SortOrder')
	BindData()
End Sub ’SortData

Protected Sub SortEmployeeIdClick(ByVal sender As Object, ByVal e As EventArgs)
        	SortField = 'EmployeeId'
        	SortData(SortField)
End Sub ’SortEmployeeIdClick

Protected Sub SortFirstNameClick(ByVal sender As Object, ByVal e As EventArgs)
        	SortField = 'FirstName'
        	SortData(SortField)
End Sub ’SortFirstNameClick

Protected Sub SortLastNameClick(ByVal sender As Object, ByVal e As EventArgs)
        	SortField = 'LastName'
        	SortData(SortField)
End Sub ’SortLastNameClick

Protected Sub SortTitleClick(ByVal sender As Object, ByVal e As EventArgs)
        	SortField = 'Title'
        	SortData(SortField)
End Sub ’SortTitleClick

C#


string SortField;
SqlConnection myconnection ; 
SqlDataAdapter myda ; 
DataSet ds ; 
String strConn ;
string SQLStmt= 'Select * from Employees  ';

private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	BindData();
}

void BindData()
{	 
	strConn = 'Server=localhost;uid=sa;password=;database=northwind;';
	myconnection =new SqlConnection(strConn);
	myda = new SqlDataAdapter(SQLStmt, myconnection);
	ds = new DataSet();
	myda.Fill(ds, 'AllTables');
	Repeater1.DataSource = ds;
	Repeater1.DataBind();
}

void SortData(string SortExpression)
{
	if (ViewState['SortOrder'] ==null)
	{
		ViewState['SortOrder'] = ' ASC';
	}
	else if (ViewState['SortOrder'].ToString () == ' ASC' )
	{
		ViewState['SortOrder'] = ' DESC';
	}
	else
	{
		ViewState['SortOrder'] = ' ASC';
	}
	SQLStmt = SQLStmt + ' ORDER BY ' + SortExpression.ToString () + ' ' + ViewState['SortOrder'];
	BindData();
}

protected void SortEmployeeIdClick(object  sender  ,EventArgs   e   ) 
{
	SortField = 'EmployeeId';
	SortData  (SortField);
} 

protected void SortFirstNameClick(object  sender  ,EventArgs   e   ) 
{
	SortField = 'FirstName';
	SortData  (SortField);
} 

protected void SortLastNameClick(object  sender  ,EventArgs   e   ) 
{
	SortField = 'LastName';
	SortData  (SortField);
} 

protected void SortTitleClick(object  sender  ,EventArgs   e   ) 
{
	SortField = 'Title';
	SortData  (SortField);
} 
Permalink

<asp:Repeater ID='Repeater1' Runat='server' EnableViewState='False'>
<HeaderTemplate>
	<table>
</HeaderTemplate>
<ItemTemplate>
	<tr <%# FormatColorRow(DataBinder.Eval(Container.DataItem,'UnitPrice').ToString()) %> >
	<td >
	<%# DataBinder.Eval(Container.DataItem,'UnitPrice').ToString() %>
	</td>
</ItemTemplate>
<FooterTemplate>
	</table>
</FooterTemplate>
</asp:Repeater>

VB.NET


Protected Function FormatColorRow(strUnitPrice As String) As String
Dim unitprice As Double = Double.Parse(strUnitPrice)
If unitprice <= 15 Then
	Return 'style=’backGround-color:red’'
Else
	Return 'style=’backGround-color:green’'
End If
End Function ’FormatColorRow 

C#


protected string FormatColorRow(string strUnitPrice)
{
	double  unitprice  =double.Parse ( strUnitPrice);
	if ( unitprice <= 15)
	{
		return 'style=’backGround-color:red’';
	}
	else
	{
		return 'style=’backGround-color:green’';
	}
}
Permalink

Use namespace System.IO


<asp:Repeater id='Repeater1' runat='server'>
<ItemTemplate >
	<%# DataBinder.Eval(Container.DataItem, 'Name').ToString() %>
</ItemTemplate>
</asp:Repeater>

VB.NET


Dim dir As New DirectoryInfo(Server.MapPath('.'))
Repeater1.DataSource = dir.GetDirectories
Repeater1.DataBind()

C#


DirectoryInfo dir = new DirectoryInfo(Server.MapPath('.'));
Repeater1.DataSource = dir.GetDirectories();
Repeater1.DataBind();
Permalink

<asp:Repeater id='Repeater1' runat='server'>
<ItemTemplate >
	<b>
	<u><p>
	<%#GetFirstAlphabet(DataBinder.Eval(Container.DataItem, 'LastName').ToString())%>
	</p></u> 
</b>
<%#DataBinder.Eval(Container.DataItem, 'LastName').ToString()%>
,<%#DataBinder.Eval(Container.DataItem, 'FirstName').ToString()%><br>
</ItemTemplate>
</asp:Repeater>

VB.NET


If Not Page.IsPostBack Then
	’Populate the Data in the Repeater
End If

protected function GetFirstAlphabet(ByVal strval As String) As String
Dim alphabet As String = ViewState('alphabet')
If alphabet = Left(strval, 1) Then
	Return ''
Else
	alphabet = Left(strval, 1)
	ViewState('alphabet') = alphabet
	Return alphabet
End If
End Function

C#


if(!Page.IsPostBack)
{
	//Populate the Data in the Repeater		
}

protected string GetFirstAlphabet(string strval)
{
	string  alphabet   =(string) ViewState['alphabet'];
	if( alphabet == strval.Substring(0,1) )
	{
		return '';
	}
	else
	{
		alphabet = strval.Substring(0,1);
		ViewState['alphabet'] = alphabet;
		return alphabet;
	}
}
Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.