How to access the item being data bound during ItemDataBound? - asp.net

I want to get at the item that is being data bound, during the ItemDataBound event of an asp:repeater.
I tried the following (which was an unaccepted answer in a stackoverflow question):
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Object dataItem = e.Item.DataItem;
...
}
but e.Item.DataItem is null.
How can I access the item being data bound during the event called ItemDataBound. I assume the event ItemDataBound happens when an item is being data bound.
I want to get at the object so I can take steps to control how it is displayed, in addition the object may have additional helpful properties to let me enrich how it is displayed.
Answer
Tool had the right answer. The answer is that e.Item.Data is only valid when e.Item.ItemType is (Item, AlternatingItem). Other times it is not valid. In my case, I was receiving ItemDataBound events during header (or footer) rows, where there is no DataItem:
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// if the data bound item is an item or alternating item (not the header etc)
if (e.Item.ItemType != ListItemType.Item &&
e.Item.ItemType != ListItemType.AlternatingItem)
{
return;
}
Object dataItem = e.Item.DataItem;
...
}

Right off the bat I would have to guess you need this:
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//Put stuff here
}
After all, the item itself could be representing a header or footer row.

I just wanted to add that I did accomplished this by doing the following:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//determine if the row type is an Item
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView row = (DataRowView)e.Item.DataItem;
if (row["RowName"].ToString() == "value")
{
//INSERT CODE HERE
}
}
}

For repeater
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType...
Can be modified to:
if (e.Item.DataItem != null) ...

Use dynamic
dynamic item = e.Item.DataItem;
string style = item.Style;

If you're dealing with an asp:ListView, you can do something like this:
protected void myLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType != ListViewItemType.DataItem)
return;
object dataItem = ((ListViewDataItem)e.Item).DataItem;
}
(The title of the question doesn't mention an asp:repeater.. so I thought it might be helpful to post the code for an asp:listview)

For a repeater with a custom template binding; you can use the following template. I used this to create a table which breaks down each data item into two rows for print view.
Repeater1.HeaderTemplate = new PrintTemplate(ListItemType.Header);
Repeater1.ItemTemplate = new PrintTemplate(ListItemType.Item);
Repeater1.AlternatingItemTemplate = new PrintTemplate(ListItemType.AlternatingItem);
Repeater1.FooterTemplate = new PrintTemplate(ListItemType.Footer);
public class PrintTemplate : ITemplate
{
ListItemType templateType;
public PrintTemplate(ListItemType type)
{
templateType = type;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch(templateType)
{
case ListItemType.Header:
lc.Text = "<TABLE>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
case ListItemType.AlternatingItem:
//lc.Text = "<TR><TD>";
lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
container.Controls.Add(lc);
break;
case ListItemType.Footer:
lc.Text = "</TABLE>";
container.Controls.Add(lc);
break;
}
}
private void TemplateControl_DataBinding(object sender,
System.EventArgs e)
{
Literal lc;
lc = (Literal)sender;
RepeaterItem container = (RepeaterItem)lc.NamingContainer;
ListItemType itmType = container.ItemType;
//construct the repeater row using a custom function that switches on item type; HEADER vs ITEM vs ALTERNATINGITEM
lc.Text += GetPopulatedRepeaterRow(dataInterface, container.DataItem, container.ItemType);
...

Related

dropdown disabled in table asp.net

i try to disable dropdown in table.. like when admin view any new documents and then select value from dropdown i.e aprove/reject when he select and click on button then this dropdown must be disabled and then when amdin again view new documents then in this dropdown must bt enable until admin approve/reject this document .....how i done this ..
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddldrop = e.Item.FindControl("DropDownList4") as DropDownList;
HiddenField hfDepartmentId = e.Item.FindControl("hfDepartmentId") as
HiddenField;
if (ddldrop != null && hfDepartmentId != null)
{
if(hfDepartmentId.Value != string.Empty && hfDepartmentId.Value.Trim() !=
"3")
{
ddldrop.SelectedValue = hfDepartmentId.Value.Trim();
ddldrop.Enabled = false;
}
}
I assume you want to enable if the departID is "3" but you don't have an else in your if. Here is a shortened and simplified version of your approach:
protected void Repeater2_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddldrop = (DropDownList) e.Item.FindControl("DropDownList4");
HiddenField hfDepartmentId = (HiddenField) e.Item.FindControl("hfDepartmentId");
ddldrop.Enabled = hfDepartmentId.Value == "3";
if(ddldrop.Enabled) ddldrop.SelectedValue = hfDepartmentId.Value;
}
}
Note that i've checked the ListItemType which makes your null-reference check redundant. You should also use more menaingful names for your controls, i would suggest DdlDepartment instead of DropDownList4.

Get Dropdown Selected Value in Datalist ItemDataBound

I am using Dropdown list inside datalist. Now, I want dropdown selected value onDatalistItemBound.
How to get it ???
Your ItemDataBound handler should look like :
protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myDropDownList = e.Item.FindControl("YourDropDownListID") as DropDownList;
int currentItemID = int.Parse(this.dl.DataKeys[e.Item.ItemIndex].ToString());
myDropDownList.DataSource = GetDDLDataSource(currentItemID);
myDropDownList.DataBind();
}
}

How to find the DataListItem item type without using FindControl?

I want to be able to determine if my DataListItem type is of type RadioButton in C# code behind.
Is this possible?
Alternatively if it is not type DropDownList that would also work.
Isn't there a way to some kind of check such as
if(item.ItemType.Equals(HtmlInputRadioButton)){
//
}
item.ItemType is an enum. Type will never be HtmlInputRadioButton
public enum ListItemType
{
Header,
Footer,
Item,
AlternatingItem,
SelectedItem,
EditItem,
Separator,
Pager,
}
Instead, the code should be like this -
void Item_XXXX(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// Make sure MyRadioButtonId is an ID of HtmlInputRadioButton
var htmlInputRadioButton = e.Item.FindControl("MyRadioButtonId")
as HtmlInputRadioButton;
}
}
The best option is:
var radio = item as RadioButton;
if(null != radio)
{
// It's a radio button!
// The "as" keyword will return null if the cast fails
}
Alternatively, you can use the clearer
if(item is RadioButton)
{
var radio = (RadioButton)item;
}
But that results in two casts, which is less efficient.

How to enable disable buttons in datalist

I have got a datalist with some databound fields with 2 buttons. I want to enable disable button depending on the column(state) value of each row, so for instance if the value of state is 0 , the remove button should be disable and add button should be enabled similarly when the value of state is 1 , vice versa..
protected void dlEditCaravans_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button addtoFeauture = e.Item.FindControl("btnAddToFeature") as Button;
Button removetoFeauture = e.Item.FindControl("btnRemoveFeature") as Button;
int id = Convert.ToInt32(dlEditCaravans.DataKeys[e.Item.ItemIndex]);
int check = caravans.GetfeautureValue(id);
if (check == 0)
{
addtoFeauture.Enabled = true;
}
else
{
removetoFeauture.Enabled = true;
}
}
I have tried something like above but it gives object reference not set to an instance error.
You need to use ItemDatabound event of Datalist
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRow dr = ((DataRowView)e.Item.DataItem).Row;
if (Convert.ToBoolean(dr["StateColumnName"])
{
((Button)e.Item.FindControl("Button1")).Enable = True;
}
}
}
void DataListProduct_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Button BT = e.Item.FindControl(“ButtonID“) as Button;
BT.Enable = True or false depends upon your condition
}
}

How to access datasource fields in an ASP.NET Repeaters ItemDataBound event?

I have a Repeater control that is being bound to the result of a Linq query.
I want to get the value of one of the datasource's fields in the ItemDataBound event, but I'm not sure how to do this.
Depending on the DataSource...
If your DataSource is a DataTable, then your DataItem contains a DataRowView:
protected void rptMyReteater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button b = e.Item.FindControl("myButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
b.CommandArgument = drv.Row["ID_COLUMN_NAME"].ToString();
}
}
You can use: e.Item.DataItem.
Example: Repeater.ItemDataBound Event
// This event is raised for the header, the footer, separators, and items.
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (((Evaluation)e.Item.DataItem).Rating == "Good")
{
((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
}
}
}
The data that is used for the current item can be found from the EventArgs.
RepeaterItemEventArgs e
e.Item.DataItem

Resources