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";
}
}
Related
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'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");
}
}
I have a repeater, which is bound on preRender with items. In the Item template each row has a check box. This works fine.
I'm trying to loop through all the checkboxes in the item template after it has been bound. Is there any way of doing this?
It sounds to me like you want to use the ItemDataBound event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
You will want to check the ItemType of the RepeaterItem so that you don't attempt to find the checkbox in Header/Footer/Seperator/Pager/Edit
Your event would look something along the lines of:
void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var checkBox = (CheckBox) e.Item.FindControl("ckbActive");
//Do something with your checkbox...
checkBox.Checked = true;
}
}
This event can be raised by adding the event in your code behind like so:
rptItems.ItemDataBound += new RepeaterItemEventHandler(rptItems_ItemDataBound);
Or by adding it to the control on the client:
onitemdatabound="rptItems_ItemDataBound"
Alternatively you can do as the others suggested and iterate over the RepeaterItems, however you still need to check the itemtype.
foreach (RepeaterItem item in rptItems.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var checkBox = (CheckBox)item.FindControl("ckbActive");
//Do something with your checkbox...
checkBox.Checked = true;
}
}
You would want to do that in the Page PreRender, after the Repeater has been bound.
Try this.
for each (RepeaterItem ri in Repeater1.Items)
{
CheckBox CheckBoxInRepeater = ri.FindControl("CheckBox1") as CheckBox;
//do something with the checkbox
}
for (int item = 0; item < Repeater.Items.Count; item++)
{
CheckBox box = Repeater.Items[item].FindControl("CheckBoxID") as CheckBox;
if (box.Checked)
{
DoStuff();
}
else
{
DoOtherStuff();
}
}
A few different thoughts come to mind:
Is there a specific need to bind this repeater in preRender? Consider using the more typical way of binding after Page_Load event.
Why are you wanting to look for the checkboxes after the repeater has been bound? Can you do whatever you need to do while it is being bound by using this event:
OnItemDataBound="Repeater1_OnItemDataBound"
Either way, you can always go back and look inside the repeater by just iterating through it. Note that you might have to do a recursive search if the checkbox is nested in a different element inside the repeater item
for each (RepeaterItem r in Repeater1.Items) {
CheckBox c = r.FindControl("CheckBox1") as CheckBox;
//DO whatever
}
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;
}
}
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>");
}
}