Hi everyone,
I tried this technique as suggested on another forum and a couple of other things, suggested on SitePoint as well as others but to no avail. Im trying to find the Label in the repeater to display information depending on the output. However I was presented with the following error:
[I]"Index was out of range. Must be non-negative and less than the size of the collection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"[/I]
It highlights the problematic script as:
Label lblStatus = rptGeneralEnq.Items[0].FindControl(“lblStatus”) as Label;
I’m trying to access the Label, in the repeater, so that depending on if the text origionally displays true or false (as the label grabs the ‘bit’ datatype for that record to display) to then change the text to something more appropriate (completed/outstanding) - if at all possible.
Heres my attempt:
C#:
1. protected void Page_Load(object sender, EventArgs e)
2. {
3.
4. Label lblStatus = rptGeneralEnq.Items[0].FindControl("lblStatus") as Label;
5.
6. if (lblStatus.Text == "True")
7. {
8. lblStatus.Text = "<font color='green'>Completed</font>";
9. }
10. else
11. {
12. lblStatus.Text = "<font color='red'>Outstanding</font>";
13. }
14. }
.ASPX
<asp:Repeater ID="rptGeneralEnq" runat="server" DataSourceID="SqlGeneralEnq" onitemcommand="rptGeneralEnq_ItemCommand">
<ItemTemplate>
<div id="generalenquiry">
<div class="right_col">
<span class="enqhist_title floatright">Received: <strong><%#((System.DateTime)DataBinder.Eval(Container.DataItem, "Received")).ToShortDateString()%></strong></span>
<span class="enqhist_title">Status: <strong><asp:Label ID="lblStatus" runat="server" Text="<%#Eval("Responded")%>" /></asp:Label></strong></span>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I had, before hand, tried the code in the repeater itself, but without rptGen… instead was just
Label lblStatus = (Label)e.Item.FindControl(“lblStatus”); …Then the logic after it. - The result of that was the text displays ‘/>’
Label in the aspx page and repeater reads: Status: <strong><asp:Label ID=“lblStatus” runat=“server” Text=“<%#Eval(“Responded”)%>” /></asp:Label></strong></span>
Any help is appreciated,
Thanks,
Daniel