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
Related
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();
}
}
I am trying to get Dropdownlist selected value using Datalist OnItemDataBound.
DlSubjects is main DataList and dlTests is nested DataList. dropdownlist is included with dlSubjects.
My code :
protected void dlSubjects_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlTests = e.Item.FindControl("dlTests") as DataList;
DropDownList drpTopic = e.Item.FindControl("drpTopic") as DropDownList;
string Value = drpTopic.SelectedValue;
}
}
Can anyone help me regarding this ???
Im tooooooo late, but Im doing same thing right now, if Im right, then I think answer should be something like that:
protected void dlSubjects_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlTests = e.Item.FindControl("dlTests") as DataList;
DropDownList drpTopic = e.Item.FindControl("drpTopic") as DropDownList;
string Value = ((DataRowView)e.Item.DataItem).Row.ItemArray[index].ToString(); //index is your dropdown index in the datalist
}
}
I have a Datalist and I'm trying to find the dropdown in the datalist to add in it text in first index in ddl I tried to do that but this appeared (object reference not set ....)
Here is my code:
private DropDownList DDLProduct;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DDlProduct_DataBound(object sender, EventArgs e)
{
DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
}
protected void DLProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
}
Regards
You should check the ItemDataBound event for the datalist and see if it is of type ListItemType.Item or ListItemType.AlternatingItem, otherwise you are hitting the null reference because you are on the header of the datalist:
in C#:
if ((e.item.ItemType == ListItemType.Item) | (e.item.itemType == ListItemType.AlternatingItem))
in VB.net:
if (e.Item.ItemType = ListItemType.Item) OR (e.Item.ItemType = ListItemType.AlternatingItem)
Then you want to see if you can find it:
in C#:
DropDownList d = (DropDownList) e.Item.FindControl("DDLProduct")
in vb.net
Dim d as DropDownList = CType(e.Item.FindControl("DDLProduct"), DropDownList)
Once you have found the dropdownlist box you can do:
d.Items.Insert(0, new ListItem("Switch Model", "0"));
Maybe you just need AppendDataBoundItems :-)
Try this.
protected void DLProduct_ItemDataBound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
private DropDownList DDLProduct = e.Item.FindControl("DDlProduct") as DropDownList;
DDLProduct.Items.Insert(0, new ListItem("Swithch Model", "0"));
}
}
Put you code within below if statement.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
// Your code goes here to find the drop down list.
}
You are getting the null reference exception because of header of footer rows, as this drop down is not there.
Use this, it's working:
sQuery = "select * from tbl_Ticket_Msg where us_ID=0 and t_status='Open' order by T_id asc";
ds3.Clear();
ds3 = cl.getDataSet(sQuery);
if (ds3.Tables[0].Rows.Count > 0)
{
DataList1.DataSource = ds3.Tables[0];
DataList1.DataBind();
lbltotal.Text = "Total Messages : " + ds3.Tables[0].Rows.Count.ToString();
int row = Convert.ToInt32(ds3.Tables[0].Rows.Count);
for (int i = 0; i < row; i++)
{
DropDownList ddl = (DropDownList)DataList1.Items[i].FindControl("DropDownList1");
ddl.DataSource = BindServicetoddl();
ddl.DataTextField = "name1";
ddl.DataValueField = "us_ID";
ddl.DataBind();
}
}
(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
I had datalist and My manager told me when user select item in datalist this item must have css or color .I did my code but it didnot work well and this error apeared ( Operator == cannot be applied to operands of type System.Web.UI.WebControls.ListItemType and System.Web.UI.WebControls.DataControlRowType )
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
{
if (e.Item.ItemType == DataControlRowType.DataRow)
{
e.Item.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Item.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Item.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
}
}
e.Item is a DataListItem; if you check its ItemType property, you'll see that it's a ListItemType, so you should use that enumeration.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemtype.aspx
Something like:
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
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);
...