control.click triggering after ItemDataBound - c#

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.

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

setting datadource of a DataGrid nested in a parent DataList row

I have a simple question
I have a DataList having a DataGrid in each row of it which must be filled based on one of the fields of the current row of DataList at run time.
Can anybody suggest a method to set the grid's datasource dynamically?
Thnx
You can use datalist itemdatabound event.
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
GridView grd= (GridView )e.Item.FindControl("GridView1");
grd.DataSource = dt;
grd.DataBind();
}
}

Repeater Highlight Item in ItemCommand

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

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

Categories