INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Update text of many labels

Update text of many labels

(OP)
I have 10 labels in my mark up as follows

CODE --> English

<asp:Label ID="lblHead1" runat="server"></asp:Label>
 <asp:Label ID="lblHead2" runat="server"></asp:Label>
 <asp:Label ID="lblHead3" runat="server"></asp:Label>
 <asp:Label ID="lblHead4" runat="server"></asp:Label>
 <asp:Label ID="lblHead5" runat="server"></asp:Label>
 <asp:Label ID="lblHead6" runat="server"></asp:Label>
 <asp:Label ID="lblHead7" runat="server"></asp:Label>
 <asp:Label ID="lblHead8" runat="server"></asp:Label>
 <asp:Label ID="lblHead9" runat="server"></asp:Label>
 <asp:Label ID="lblHead10" runat="server"></asp:Label> 
I am trying to set the text in a select in the code behind

CODE --> English

COL = 0
                While MyMenu.Read
                    Select Case MyMenu.GetInt32(1)
                        Case 1 
                            COL = COL + 1
                            lblHeadCOL.Text = "MyMenu.GetString(4)"

                        Case 2 
                        Case 3 
                        Case 4
                     End Select
                End While 

Obviously this will not work as "lblHeadCOL" does not exist, but I cannot find the correct format. I think I have to use findcontrol. I have tried

CODE --> English

Dim myLabel As Object = FindControl("lblHead1")
                            myLabel.Text = "MyMenu.GetString(4)" 
Can anyone help me with the correct format please, or is there a better way.

Steve

RE: Update text of many labels

Can you post your entire mark up and code? Trying to understand the total layout and what you are trying to accomplish.
You say you tried FindControl(), but you didn't say what the result was.

RE: Update text of many labels

(OP)
This is the full listing of my mark up code

CODE -->

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ShowVanMenu.aspx.vb" Inherits="htdocs_Lawn_LawnProg_ShowVanMenu" MasterPageFile="Lawn.master"  %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h3> <asp:label runat="server" id="LblDescription" /></h3>
    <br />

    <div style="width:100%">
        <asp:Table runat="server" CssClass="MainTable">
           <asp:TableRow><asp:TableCell>
            <asp:TableRow><asp:TableCell>Space</asp:TableCell></asp:TableRow>
            <asp:Label ID="lblHead1" runat="server"></asp:Label>
            <asp:Label ID="lblHead2" runat="server"></asp:Label>
            <asp:Label ID="lblHead3" runat="server"></asp:Label>
            <asp:Label ID="lblHead4" runat="server"></asp:Label>
            <asp:Label ID="lblHead5" runat="server"></asp:Label>
            <asp:Label ID="lblHead6" runat="server"></asp:Label>
            <asp:Label ID="lblHead7" runat="server"></asp:Label>
            <asp:Label ID="lblHead8" runat="server"></asp:Label>
            <asp:Label ID="lblHead9" runat="server"></asp:Label>
            <asp:Label ID="lblHead10" runat="server"></asp:Label>
            </asp:TableCell></asp:TableRow>
        </asp:Table>
        <br />
        <asp:Label runat="server" ID="label1" />
        <asp:Label runat="server" ID="label2" />
        <asp:Button runat="server" ID="sbmtButton" Text="Update User"/>
    </div>
</asp:Content> 
And this is my code behind (very much simplified)

CODE -->

Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Partial Class htdocs_Lawn_lawnProg_ShowVanMenu
    Inherits System.Web.UI.Page
    Dim LawnCareConn As Odbc.OdbcConnection = New Odbc.OdbcConnection
    Dim MyDataTable As DataTable
    Dim MyMaster As Odbc.OdbcDataReader
    Dim MyMenu As Odbc.OdbcDataReader

    Protected Sub Page_Load(ByVal sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LblDescription.text = "&nbsp;&nbsp;&nbsp;&nbsp;List of Users"

            Dim LawnCareConn As Odbc.OdbcConnection = New Odbc.OdbcConnection

            Dim LoginCookie As HttpCookie = Request.Cookies.Get("userid")
            Dim MyReader As Odbc.OdbcDataReader
            If Not LoginCookie Is Nothing Then
                label1.Text = LoginCookie.value
            End If

            Dim UserIDCookie As HttpCookie = Request.Cookies.Get("UserID")
            Dim UserID As String = UserIDCookie.Value
            Dim Col As Integer, Row As Integer

            LblDescription.text = "&nbsp;&nbsp;&nbsp;&nbsp;Van Menu"

            LawnCareConn.ConnectionString = ConfigurationManager.ConnectionStrings("LawnCareConn").ConnectionString
            LawnCareConn.Open()

            '  Read LAWN_Menu_Items
            Dim MenuSql As String = "select * from lawn_menu_items order by Menu_Order"
            Dim MenuCommand As Odbc.OdbcCommand = New Odbc.OdbcCommand(MenuSql, LawnCareConn)
            MyMenu = MenuCommand.ExecuteReader()
            MyMenu.Read()
            Col = 0

            If MyMenu.HasRows Then
                While MyMenu.Read
                    Select Case MyMenu.GetInt32(1)
                        Case 1 'must be a menu header
                            Col = Col + 1
                            '   Label myLabel = this.FindControl("myLabel") as Label;
                            '   myLabel.Text = "my text";

                            'Dim myLabel As FindControl("lblHead" + CStr(Col)) asLabel)
                            'Dim myLabel As Object = FindControl("lblHead1")
                            Dim myLabel As Object = (Page.FindControl("lblHead" + CStr(Col)))
                            'Dim c As Label = CType(b.Parent.FindControl("Label1"), Label)


                            myLabel.Text = MyMenu.GetString(4)

                        Case 2 'Child item - Always wanted by master user (van = 0)
                        Case 3 'Child Item - Only wanted if multi van and Master user allows it
                        Case 4 'Dev users only - UserID < 010
                    End Select

                    '                    myMenuItem = Nothing
                End While
            End If

            LawnCareConn.Close()
        End If
    End Sub

    Public Sub sbmtButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbmtButton.Click
        'LawnCareConn.ConnectionString = ConfigurationManager.ConnectionStrings("LawnCareConn").ConnectionString
        'LawnCareConn.Open()

        'Dim EditSql As String = "update lawn_user_master set Extra_Instructions=@Name " & _
        '                        "where UserID=@UserID and Van=@VanNo"
        'Dim MyCommand As Odbc.OdbcCommand = New Odbc.OdbcCommand(EditSql, LawnCareConn)

        'Response.Write("<script language='javascript'>alert('Text box is " & txtExtra.Text.ToString() & " ');</script>")


        'MyCommand.Parameters.AddWithValue("@Name", txtExtra.Text.ToString())
        'MyCommand.Parameters.AddWithValue("@Name", "This is a test. I will add a bit more!!!")
        'MyCommand.Parameters.AddWithValue("@UserID", Request.QueryString("UserID"))
        'MyCommand.Parameters.AddWithValue("@VanNo", Request.QueryString("VanNo"))
        '
        'MyCommand.ExecuteNonQuery()

        'MyCommand.Dispose()
        'LawnCareConn.Close()
        'Response.Redirect("ViewDB.aspx")

    End Sub

End Class 

What I am trying to do is read a table, and for every type 1 record (MyMenu.GetInt32(1) = 1) (There will be 10 of them) I want to change the .text values of lblHead1, lblHead2.......ldlHead10 with a field (MyMenu.GetString(4)) from each record read. Obviously, these values can change or I would just hard code the text values. These will be headings for data displayed below them. Hope this explains.

Steve

RE: Update text of many labels

(OP)
I forgot to say, the error is occurring on the line "myLabel.Text = MyMenu.GetString(4)" and the error message is "Object variable or With block variable not set. "

Steve

RE: Update text of many labels

I would first change the code below from this:

CODE

Dim myLabel As Object = (Page.FindControl("lblHead" + CStr(Col))) 
To something like this:

CODE

Dim myLabel As Label = (Page.FindControl("lblHead" + Col.ToString()))
If myLabel is not nothing then
   myLabel.Text = MyMenu.GetString(4)
End If 

First make sure you are getting the label control,
Next, make sure you have a value from "MyMenu.GetString(4)"

RE: Update text of many labels

(OP)
Hi All

I now have this working. I used the following code.

CODE -->

Col = Col + 1
Dim myLabel As Label = TryCast(Master.FindControl("ContentPlaceholder1").FindControl("lblHead" + CStr(Col)), Label)
myLabel.Text = MyMenu.GetString(4) 
As you can see, I was trying to find a control within a contents place holder.

Thanks for all your help jbenson001

RE: Update text of many labels

Great, glad to help and glad you got it working

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close