How to add controls to ItemTemplate (Repeater) at run time? - c#

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>");
}
}

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";
}
}

Remove something added by a particular event - c#

I have a piece of code that will add the selected item of a combobox to a listbox when a checkbox is checked. I would like to remove that selected item to be removed from the listbox when the checkbox is unchecked.
My problem is I cant simply repeat the code for removal to be the same as add because the combobox selection will different or empty when unchecked.
This is how it currently looks:
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
listBox2.Items.Add(comboBox1.SelectedItem);
}
if (checkBox1.Checked == false)
{
listBox2.Items.Remove(comboBox1.SelectedItem);
}
So I need a way of removing whatever was added by that check change instead of removing the selected index of the combobox. Please consider that there may be multiple lines in the listbox added by multiple different checkboxes.
You simply need to store the added item and remove it.
private object addedItem;
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
addedItem = comboBox1.SelectedItem;
listBox2.Items.Add(addedItem);
}
else
{
listBox2.Items.Remove(addedItem);
}
}
You may also need to check SelectedItem is null before adding/removing the item.
Focusing on the part where you said there might be multiple different checkboxes,
you need to store one item per checkbox.
You can write your own child class of the checbox control to add this feature, or simply use the Tag property.
You can also indicate which checkbox is linked to which combobox in the same way. Either child class or use the Tag property.
In my example, I'll assume you've referenced the combobox from the checkbox using the Tag property.
You can do it manually like this
checkBox1.Tag = comboBox1;
or hopefully you can automate it if you are generating these on the fly.
Here is the general idea of how the checkbox event should look.
The event is is utilising the sender argument, which means you should hook up all your checkboxes CheckedChanged events to this one handler. No need to create separate handlers for each.
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
var checkBox = (CheckBox)sender;
var comboBox = (ComboBox)checkBox.Tag;
if (checkBox.Checked && comboBox.SelectedItem != null)
{
listBox2.Items.Add(comboBox.SelectedItem);
comboBox.Tag = comboBox.SelectedItem;
}
if (!checkBox.Checked && comboBox.Tag != null)
{
listBox2.Items.Remove(comboBox.Tag);
}
}

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");
}
}

How can I loop through Items in the Item Template from an asp:Repeater?

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
}

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