Placing labels on the panel(and not on the webpage) at runtime

Joined
Aug 12, 2010
Messages
6
Programming Experience
Beginner
Hello all,

What I am trying to do here is I want to add the dynamically created labels in the Panel on the Webpage and not directly on the Webpage. My panel has scroll bars and thus when I scroll horizontally or vertically only the background moves whereas the labels are static to the page.

And some labels are even visible on the panel borders on scrollbars (some on the page outside the panel) which are suppose to be placed inside the Panel. So when I scroll either of the ways even the labels should move along with the background image.

So how can I make these labels stick to the Panel and not the Page? How do I do it?

Please do help me out on this one.

Regards,

Prajakta

PS: I have already used Panel.Controls.Add(label1)..Please reply.
 
set the ScrollBars attribute e.g. ScrollBars="Both" or ScrollBars="Vertical"

For example:

PHP:
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Add a Few Labels" />
        <br />
            <asp:Panel ID="Panel1" runat="server" Height="100px" Width="300px" ScrollBars="Both" />
        </div>    
    </form>
</body>

The just add e.g. 10 Label controls inside the panel:

VB.NET:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 1 To 10
            Dim nLabel As New Label
            nLabel.ID = "Label" & i
            nLabel.Text = "Label: " & i

            Panel1.Controls.Add(nLabel)
            Panel1.Controls.Add(New LiteralControl("<br /><br />"))
        Next
    End Sub

Now test it and tell me if it helped or not
 
Hi,

That is what I have done so far. As in created and added labels to the panel at runtime. However what I want is the labels should be placed at a particular position on the panel. Label position and labels being placed on the panel are really important in this project.

Please check the code which I have written. And tell me what changes need to be done to the code.
dim i as integer=0
Do While i<countr
l1 = New Label
Me.Panel1.Controls.Add(l1)

l1.Text = items(0).ToUpper
l1.Font.Name = "Times New Roman"
l1.Font.Size = 8
l1.Font.Bold = True
l1.ForeColor = Color.Red
l1.Attributes.Add("style", "position:absolute; top:" & checkedX & "px; left:" & checkedY & "px")
i=i+1
Loop

In the code, the checkedX and checkedY coordinates given are very important. But when the panel size smaller than the position specified, the labels come out of the panel on the page. Which I dont want. I want the labels to stay inside the panel at that particular position and when i scroll the panel the labels are visible.

Please can any one help with this? What al changes to make in the code?

Prajakta
 
Meanwhile try this:
VB.NET:
        Panel1.Attributes.Add("style", "position: relative;") ' PAY ATTENTION HERE
        Dim nLabel As Label = Nothing
        Dim countr As Integer = 10
        Dim i As Integer = 0
        Do While i < countr
            nLabel = New Label
            nLabel.Text = "Label " & i
            nLabel.Font.Name = "Times New Roman"
            nLabel.Font.Size = 8
            nLabel.Font.Bold = True
            nLabel.ForeColor = Drawing.Color.Red
            nLabel.Attributes.Add("style", "position:absolute; top:" & 20 * i & "px; left:" & 10 * i & "px")
            Me.Panel1.Controls.Add(nLabel)
            i += 1
        Loop

However you should modify the way you currently set top and left position of the labels. Please explain better and i will help further.
 
Me.Panel1.Controls.Add(l1) should be moved to the bottom .. just after adding attributes. Also you should apply some CSS style to the panel as well.

Hmm please post entire code and don't make me guess.

Nope I have posted the required code.As you suggested I have written "Me.Panel1.Controls.Add(l1)" line after setting the label attributes.

So which CSS style to panel is to be changed?

Thanks
 
VB.NET:
        [COLOR="#000080"]Panel1[/COLOR].Attributes.Add("style", "position: relative;") [COLOR="#008000"]' PAY ATTENTION HERE[/COLOR]
        Dim nLabel As Label = Nothing
        Dim countr As Integer = 10
        Dim i As Integer = 0
        Do While i < countr
            nLabel = New Label
            nLabel.Text = "Label " & i
            nLabel.Font.Name = "Times New Roman"
            nLabel.Font.Size = 8
            nLabel.Font.Bold = True
            nLabel.ForeColor = Drawing.Color.Red
            nLabel.Attributes.Add("style", "position:absolute; top:" & 20 * i & "px; left:" & 10 * i & "px")
            Me.Panel1.Controls.Add(nLabel)
            i += 1
        Loop

I repost the code. Use the code AS IS and see what happens.
 
Worked!

Hey thats gr8.....it indeed did work for me! thanks a lot..... :).. By the way later I might need your suggestions(help) on other logic as I am into my disseration(project) phase.

Thanks Again

Cheers,
Prajakta
 

Latest posts

Back
Top