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)"
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 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 know you can utilize a ModalPopupExtender and have it be displayed when the user clicks a button or something of the sort by assigning the TargetControlID. What I'm looking to do is display this popup when an error occurs on my page. So by using conditional logic in the C# side of things, for example, if a certain variable is set to something, display this popup.
Is there a way I can do this, or something similar?
Yep, in your C# code you can call
my_ModalPopupExtender.Show();
Where my_ModalPopupExtender is the name of your popup extender.
It's that easy!
If, loading page, you know condition to show or not popup you can remove or not ModalPopupExtender!
In my case, populating a table using repeater :
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
ModalPopupExtender ModalPopupExtenderLinkButton =
e.Item.FindControl("ModalPopupExtenderLinkButton") as ModalPopupExtender;
if (condition)
e.Item.Controls.Remove(ModalPopupExtenderLinkButton);
}
}
Hope this will help!
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;
}
}