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");
}
}
Related
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";
}
}
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)"
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));
}
}
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.
I'm desinging a rich repeater control which needs some controls (specifically just an unordered list) added to it at runtime.
The solution I've opted for is to inject the nesseccary markup, onInit, into the header, item and footer templates respectively.
I can get the templates out (using InstantiateIn) and then add the markup as needed, but I don't know a way to add the template back in to the repeater?
In the past I've simply handled the ItemDataBound Event and modified the current RepeaterItem with whatever I needed to do.
Example:
private void Repeater1_ItemDataBound(object Sender, RepeaterItemEventArgs e)
{
// Make sure you filter for the item you are after
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
PlaceHolder listLocation = (PlaceHolder)e.Item.FindControl("listPlaceHolder");
var subItems = ((MyClass)e.Item.DataItem).SubItems;
listLocation.Controls.Add(new LiteralControl("<ul>");
foreach(var item in subItems)
{
listLocation.Controls.Add(new LiteralControl("<li>" + item + "</li>"));
}
listLocation.Controls.Add(new LiteralControl("</ul>");
}
}