How can I dynamically access control inside DataList ItemDataBound event when SeparatorTemplate is used for DataList control?

Hello,

Usually I can dynamically access control (Label in this case) inside DataList ItemDataBound event using this code:

Dim Label1 As Label = e.Item.FindControl("Label1")
Label1.Text = "sample text"

But when custom SeparatorTemplate is used for DataList control, then this code no longer works and produces this error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
More details follow…

How can I dynamically access control inside DataList ItemDataBound event when SeparatorTemplate is used for DataList control?

 


Improving lifestyles for luxury pets: TheFabulousPet
0
goixq
2/6/2008 2:17:51 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
2448 Views

Similar Articles

[PageSpeed] 45

Qualify your code by ensuring that the ItemType is either of type Item or AlternatingItem

If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then


Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
2/6/2008 2:28:25 PM

Thank you!

Your solution works very well!

 


Improving lifestyles for luxury pets: TheFabulousPet
0
goixq
2/6/2008 5:42:27 PM

Please help

I would like to know how can i add sub headings to a repeater control when the category changes.

I did this in gridview and its working,

protected void gdvRowDatabound(object sender, GridViewRowEventArgs e)

{

// If the current row is a DataRow (and not a Header or Footer row), then do stuff.

if (e.Row.RowType == DataControlRowType.DataRow)

{

DataRowView drv = (DataRowView)e.Row.DataItem;if (tmpCategoryName != drv["q1option"].ToString())

{

tmpCategoryName = drv["q1option"].ToString();

// Get a reference to the current row's Parent, which is the Gridview (which happens to be a table)

Table tbl = e.Row.Parent as Table;if (tbl != null)

{

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

TableCell cell = new TableCell();

// Span the row across all of the columns in the Gridview

cell.ColumnSpan = this.GdProd.Columns.Count;

cell.Width = Unit.Percentage(100);

cell.Style.Add("font-weight", "bold");

cell.Style.Add("background-color", "#c0c0c0");

cell.Style.Add("color", "white");

 

HtmlGenericControl span = new HtmlGenericControl("span");

span.InnerHtml = tmpCategoryName;

cell.Controls.Add(span);

row.Cells.Add(cell);

tbl.Rows.AddAt(tbl.Rows.Count - 1, row);

}

}

}

 

}

is there any one...pls help me 

i tried to use this code in the onitemdatabound event but its not working.

0
ardraann
10/21/2008 10:34:20 AM
Reply: