Answered by:
cant call label, error label not declared

Question
-
User1717218719 posted
Hi all I am recieving an error that says " 'label2' is not declared. It may be inaccessible due to its protection level.". I am gettuing this error for label2, 3 and 4.
I have double checked naming to ensure its correct and still recieve an error. If anyone has any idea as to why this might be happening please let me knoe.
Thanks
The code I have is:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Call UPDATEtoRepeater(TextBox1, TextBox2, TextBox3, TextBox4, Label2, Label3, Label4, repeater1) End Sub
<FooterTemplate> <tr> <td> <asp:Label CssClass="Label" ID="Label1" runat="server" Text="Total"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label2" runat="server"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label3" runat="server"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label4" runat="server"></asp:Label></td> <td> <td></td> </tr> </table> </FooterTemplate>
Monday, June 17, 2019 10:32 AM
Answers
-
User475983607 posted
E.RU
my template footer is within my repeater? so I dont see wht that would be an issue ? could you explain further pleaseRepeater reference docs.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater?view=netframework-4.8
Example that uses FindControl to get the Text values of TextBoses in a Repeater footer.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.aspx.cs" Inherits="WebFormsDemo.RepeaterDemo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <table border="1"> <tr> <td><b>Company</b></td> <td><b>Symbol</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td><%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> <table> <tr> <td> <asp:TextBox ID="Name" runat="server"></asp:TextBox></td> <td> <asp:TextBox ID="Ticker" runat="server"></asp:TextBox></td> <td> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td> </tr> </table> </FooterTemplate> </asp:Repeater> </div> <div> <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label> <asp:Label ID="lblTicker" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebFormsDemo { public partial class RepeaterDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add(new PositionData("Microsoft", "Msft")); values.Add(new PositionData("Intel", "Intc")); values.Add(new PositionData("Dell", "Dell")); Repeater1.DataSource = values; Repeater1.DataBind(); } } public class PositionData { private string name; private string ticker; public PositionData(string name, string ticker) { this.name = name; this.ticker = ticker; } public string Name { get { return name; } } public string Ticker { get { return ticker; } } } protected void Button1_Click(object sender, EventArgs e) { TextBox Name = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("Name") as TextBox; TextBox Ticker = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("Ticker") as TextBox; lblName.Text = Name.Text; lblTicker.Text = Ticker.Text; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 17, 2019 1:47 PM
All replies
-
User475983607 posted
The designer file contains the server control members like labels and has the same name and namespace as the code behind. This error usually indicates a change made to the page directive or page class related to the namespace.
I recommend starting over with a new web form. Alternatively you can try to fix the naming conflict.
Monday, June 17, 2019 10:46 AM -
User-1038772411 posted
Hello E.RU,
you declared your labels inside footertemplate. If you label is inside some sort of template, its not available directly in the page scope.
Here's a rough example of how you might access it:
foreach ( DataListItem item in datalist1.Items ) { if ( item.ItemType == ListItemType.Footer ) { Label lbl = (Label)item.FindControl("Label1"); } }
For refrence https://stackoverflow.com/questions/5522104/label-id-not-declared-error-in-vb-net/5522241#5522241
I hope this will help you to solve your issue
Thank You
Monday, June 17, 2019 10:52 AM -
User1717218719 posted
Thank you for your reply. I created a whole new project coppied my code into it and still had the same problems? any idea why ? Mant Thanks.
Monday, June 17, 2019 11:01 AM -
User475983607 posted
Thank you for your reply. I created a whole new project coppied my code into it and still had the same problems? any idea why ? Mant Thanks.
You edited your initial post and added code. The error, as stated above, is due to adding the labels to another server control. You'll need to use FindControl in the code behind to reference the nested server controls.
Monday, June 17, 2019 11:15 AM -
User1717218719 posted
I am already using a find control. This is my code for the find control:
'-- Update footer totals Dim itmFtr As RepeaterItem = CType(argrpt.Controls(argrpt.Controls.Count - 1), RepeaterItem) CType(itmFtr.FindControl("argLbl2"), Label).Text = intComAIR.ToString("0.00") CType(itmFtr.FindControl("argLbl3"), Label).Text = intComAGT.ToString("0.00") CType(itmFtr.FindControl("argLbl4"), Label).Text = intComDIF.ToString("0.00")
Monday, June 17, 2019 11:19 AM -
User475983607 posted
I am already using a find control. This is my code for the find control:
'-- Update footer totals Dim itmFtr As RepeaterItem = CType(argrpt.Controls(argrpt.Controls.Count - 1), RepeaterItem) CType(itmFtr.FindControl("argLbl2"), Label).Text = intComAIR.ToString("0.00") CType(itmFtr.FindControl("argLbl3"), Label).Text = intComAGT.ToString("0.00") CType(itmFtr.FindControl("argLbl4"), Label).Text = intComDIF.ToString("0.00")
The original code is referencing the labels directly.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Call UPDATEtoRepeater(TextBox1, TextBox2, TextBox3, TextBox4, Label2, Label3, Label4, repeater1) End Sub
Do you fix this?
The new code has no context. Is itnFtr the name of the repeater, is this snippet of code from an event handler?
Please try to solve one issue at a time and include the changed source code. Otherwise, this thread will be like you others; very hard to follow.
Monday, June 17, 2019 11:25 AM -
User1717218719 posted
This is my full code I am not changing any code? I am using a call function that is why it might seem as if the variable names are changing
Itmftr Is a variable name for RepeaterItem
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Call UPDATEtoRepeater(TextBox1, TextBox2, TextBox3, TextBox4, Label2, Label3, Label4, rptTaxCom) End Sub Protected Sub UPDATEtoRepeater(ByRef argTxtBox1 As TextBox, ByRef argTxtBox2 As TextBox, ByRef argTxtBox3 As TextBox, ByRef argTxtBox4 As TextBox, ByRef argLbl2 As Label, ByRef argLbl3 As Label, ByRef argLbl4 As Label, ByRef argrpt As Repeater) '-- Update footer totals Dim itmFtr As RepeaterItem = CType(argrpt.Controls(argrpt.Controls.Count - 1), RepeaterItem) CType(itmFtr.FindControl("argLbl2"), Label).Text = intComAIR.ToString("0.00") CType(itmFtr.FindControl("argLbl3"), Label).Text = intComAGT.ToString("0.00") CType(itmFtr.FindControl("argLbl4"), Label).Text = intComDIF.ToString ("0.00") End Sub
Monday, June 17, 2019 11:46 AM -
User475983607 posted
As stated above... this code will NOT work if the server controls are inside another!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Call UPDATEtoRepeater(TextBox1, TextBox2, TextBox3, TextBox4, Label2, Label3, Label4, rptTaxCom) End Sub
You MUST user FindControl inside of an event handler filtered by the template footer not the repeater!!! The controls are within the Repeater within the template footer.
Lastly always post all the relevant code otherwise we are guessing.
Monday, June 17, 2019 12:45 PM -
User1717218719 posted
Hi mgebhard
my template footer is within my repeater? so I dont see wht that would be an issue ? could you explain further please
Monday, June 17, 2019 1:12 PM -
User475983607 posted
E.RU
my template footer is within my repeater? so I dont see wht that would be an issue ? could you explain further pleaseRepeater reference docs.
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater?view=netframework-4.8
Example that uses FindControl to get the Text values of TextBoses in a Repeater footer.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.aspx.cs" Inherits="WebFormsDemo.RepeaterDemo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <table border="1"> <tr> <td><b>Company</b></td> <td><b>Symbol</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td><%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> <table> <tr> <td> <asp:TextBox ID="Name" runat="server"></asp:TextBox></td> <td> <asp:TextBox ID="Ticker" runat="server"></asp:TextBox></td> <td> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td> </tr> </table> </FooterTemplate> </asp:Repeater> </div> <div> <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label> <asp:Label ID="lblTicker" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebFormsDemo { public partial class RepeaterDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add(new PositionData("Microsoft", "Msft")); values.Add(new PositionData("Intel", "Intc")); values.Add(new PositionData("Dell", "Dell")); Repeater1.DataSource = values; Repeater1.DataBind(); } } public class PositionData { private string name; private string ticker; public PositionData(string name, string ticker) { this.name = name; this.ticker = ticker; } public string Name { get { return name; } } public string Ticker { get { return ticker; } } } protected void Button1_Click(object sender, EventArgs e) { TextBox Name = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("Name") as TextBox; TextBox Ticker = Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("Ticker") as TextBox; lblName.Text = Name.Text; lblTicker.Text = Ticker.Text; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 17, 2019 1:47 PM