Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update text of many labels 1

Status
Not open for further replies.

Papa Bear

Programmer
Feb 1, 2003
37
GB
I have 10 labels in my mark up as follows

Code:
<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
[/code]
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)"
[/code]
Can anyone help me with the correct format please, or is there a better way.

Steve
 
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.
 
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
 
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
 
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 [red][b]Label[/b][/red] = (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)
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top