Update shortcut c #
When the page is first loaded, I have a label with 0 or 1. Look at the code and you will do what I am trying to do. But it doesn't work because the page has already been loaded.
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
if (lblName.Text == "1")
{
lblName.Text = lblName.Text + "Under arbete";
}
else if (lblName.Text == "0")
{
lblName.Text = "Fixad";
}
else { }
}
}
You may have already solved this, but I think the problem with the code you posted is this:
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
Because you are referencing a control in a relay, the controls within each relay item are named dynamically based on their context. Most likely, the name of the control looks like:
"rptBugStatus $ repeaterItem0 $ lblBugStatus"
To find out the exact name, hardcode it, run the page in a browser, and look at the HTML output (via View Source in the browser menu). You should be able to scroll down and see your relay (it will show up as a tag <table>
), its elements, and the controls that are inside each element. The ids / names will be set and you can copy / paste them into your FindControl method.
Hope it helps,
Nat
a source to share
Okay, I'm assuming you've set this label (assuming you're in a relay, from your code) to make a difference. I tried to bind data to something exactly the same using the code below.
protected override void OnPreRender(EventArgs e)
{
base.OnLoad(e);
List<string> strList = new List<string>();
strList.Add("1");
rptBugStatus.DataSource = strList;
rptBugStatus.DataBind();
}
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblBugStatus = e.Item.FindControl("lblBugStatus") as Label;
// have added this just so we are actually setting the text property on the bug status
// - i have assumed you do this.
lblBugStatus.Text = e.Item.DataItem.ToString();
if (lblBugStatus.Text.Equals("1"))
{
lblBugStatus.Text = lblBugStatus.Text + "Under arbete";
}
else if (lblBugStatus.Text.Equals("0"))
{
lblBugStatus.Text = "Fixad";
}
}
}
When using aspx
<asp:Repeater runat="server" ID="rptBugStatus" OnItemDataBound="rptBugStatus_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblBugStatus" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
And I have no problem.
- I will give below on the page.
1More
I think you need to post more code if you hid from us :)
Tim
a source to share