Repeater Highlight Item in ItemCommand - c#

I have a repeater, which inside I bind some usercontrol with Panel. The panel has a OnClick event and raise a ItemCommand. I am aware that the DataItem is not persist throughout the postback and therefore it will be null during the ItemCommand event, my requirement is to change the color of the particular usercontrol when it is clicked (without using Javascript). Anyone has an idea?

You can try altering class names of the particular usercontrols onClick in itemDataBound event like below
protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//Add eventhandlers for highlighting
//a DataListItem when the mouse hovers over it.
e.Item.Attributes.Add("onmouseover",
"this.oldClass = this.className;" +
" this.className = 'EntryLineHover'");
e.Item.Attributes.Add("onmouseout",
"this.className = this.oldClass;");
//Add eventhandler for simulating
//a click on the 'SelectButton'
e.Item.Attributes.Add("onclick",
this.Page.ClientScript.GetPostBackEventReference(
e.Item.Controls[1], string.Empty));
}
}

Related

How to add Pre render Event to a repeater control

Need help adding a pre render event to repeater control to change a value of a text box which is placed inside that repeater control.
how to write code behind for this? I started like this
protected void rptServices_PreRender(object sender, EventArgs e)
{ }
how to access the text box with in that repeater control and assign a new value to it
You can add this to your event to loop the items in the repeater finding the textbox you need and making the edits needed.
foreach (RepeaterItem item in rptServices.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var textbox = (TextBox)item.FindControl("tbx");
//Do something with your textbox
textbox.Text = "Test";
}
}

How to change imagebutton image in datalist programatically

I have a Datalist on aspx page which contains an ImageButton on it. I want to change its image when a user is offline versus online. Specifically, when a user is online, I want the image to be green, when offline, I want it to be red.
Can anyone tell me how to do this?
You can use DataList.ItemDataBound to go through each record and set the required image based on the value of login status.
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton imagebutton = (ImageButton )e.Item.FindControl("imagebutton");
bool onlineStatus = bool.Parse(DataBinder.Eval(Container.DataItem, "OnlineStatusDbColumn").ToString());
if(onlineStatus)
imagebutton.ImageURL = path1;
else
imagebutton.ImageURL = path2;
}
}
You can use Itemcreated event of datalist.
"The ItemCreated event is commonly used to control the content and appearance of a row in the DataList control.(MSDN)"

control.click triggering after ItemDataBound

I have a repeater object that displays a number of rows. I also have a button after the repeater which allows a new repeater row to be added. The issue it that the ItemDataBound event is getting triggered before the click event. So when the page reloads it does not have the newest item that would normally show up. The only way to see the item is to refresh the page.
Code for data bound:
protected void default_ItemDataBound(object source, RepeaterItemEventArgs e)
{
// Bind the controls
if (e.Item.ItemType == ListItemType.AlternatingItem
|| e.Item.ItemType == ListItemType.EditItem
|| e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.SelectedItem)
{
XElement otherStructureElement = XML.XPathSelectElement(e.Item.DataItem.ToString());
HtmlAnchor lnkRemove = e.Item.FindControl("lnkRemoveForm") as HtmlAnchor;
if (lnkRemove != null && otherStructureElement != null)
lnkRemove.Attributes.Add("onclick", string.Concat("return deleteOtherStructure('",otherStructureElement.Attribute("id").Value, "');"));
}
}
and the click event
protected void SaveOtherStructuress_Click(object sender, EventArgs e)
{
AddOtherStructure();
}
Is there a way to trigger the click event before the ItemDataBound?
"The issue it that the ItemDataBound event is getting triggered before
the click event"
Why do you databind the repeater on postbacks from page_load?
You should do that only from event handlers like your button-click(if at all).
If EnableViewState is true (default), you don't need to databind it on every postback.

Trying to use controls in my repeater control (asp.net)

I'm using a repeater control in my application and I want it to have other controls in it like a linkbutton (to allow people to make comment to the item) another linkbutton (that show the item and to be clickable so that I can redirect it to another page). All my attempt to code it in the code behind was denied. I can't find them in the code behind. Note: All these linkbuttons are rapped with a panel control in the repeater.
You can find your controls in ItemCreated or ItemDataBound.
void Repeater_ItemCreated(Object Sender, RepeaterItemEventArgs e)
{
var control = (LinkButton)e.Item.FindControl("yourLinkButtonId");
}
Or
void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var control = (LinkButton)e.Item.FindControl("yourLinkButtonId");
}
}

Having problems attempting to grab the row of a ASP.NET DataGrid Object

So I have a ASP.NET DataGrid and in the SomeName_ItemBound I am trying to set the visibility of the row if certain conditions are met. However, I can't seem to find a way to acquire the row.
How do I select the current row in the:
SomeName_ItemBound(object sender, DataGridItemEventArgs e)
DataGrid is an old control you should use GridView, I guess you must be using GridView already.
You can get current row by e.Item inside your ItemBound event
like
protected void SomeName_ItemBound(Object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control. In this example, the
// ItemDataBound event is used to format the items in the
// CurrencyColumn in currency format.
if((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
// e.Item // is your current row
e.Item.Visible = false;
}
}

Categories