?
Solved

Positioning an array of textboxes in ASP.Net

Posted on 2005-04-18
4
Medium Priority
?
417 Views
Last Modified: 2013-11-25
I am trying to simply position an array of textboxes that have been dynamically created in the page load on a page.
Code I'm trying with is (actually copied from elsewhere for a button array!):
===========================================================
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Addtextboxes()
    End Sub
    Public textboxArray(9) As TextBox

    Public Sub Addtextboxes()
        For x As Integer = 0 To 9
            textboxArray(x) = New TextBox
            Me.Controls.Add(textboxArray(x))
            With textboxArray(x)
                .Tag = x
                .Text = "textbox " & x
                .Size = New Size(60, 20)
                .Location = New Point(20, (x * 20) + 4)
                .Parent = Me
                .Visible = True
            End With
        Next
    End Sub
==========================================================
Trouble is Size, Location, and Parent all give errors.
Does this mean that I CANNOT position and then reference an array of textboxes on an ASP.Net page, or is there another way of doing it.

The problem I'm trying to solve is to write a neat validation routine in codebehind, to validate the contents of 12  textboxes each of which contains the answer to a predefined question.
Its easy to write separate code for each, but a concise single routine would be technically good, and aesthetically pleasing.
(Sorry, running off at the mouth again!)
Cheers
Dave
0
Comment
Question by:davepruce
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13806275
aspx
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="b1xml2.ExpertExchange.VB.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="FlowLayout">

    <form id="Form1" method="post" runat="server">
      Hello World
      <asp:Panel ID="TextBoxPanel" Runat="server" style="POSITION:relative;top:40px;left:50px;"></asp:Panel>
    </form>

  </body>
</HTML>

aspx.vb
=====
Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents TextBoxPanel As System.Web.UI.WebControls.Panel

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        AddTextBoxes(8)
       
    End Sub

    Private Sub AddTextBoxes(ByVal amount As Integer)
        Dim i As Integer
        Dim box As TextBox
        For i = 1 To amount
            box = New TextBox
            With box
                .Text = "textbox" & i
                .Style("width") = "60px"
                .Style("height") = "20px"
                .Visible = True
                .Style("position") = "absolute"
                .Style("top") = CStr(((i - 1) * 22) + 6) & "px"
                .Style("left") = "20px"
            End With
            TextBoxPanel.Controls.Add(box)
        Next
    End Sub

End Class



0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13806297
That create an array of textboxes that are aligned.

Tips for positioning.
1. The Panel Control is emitted as a div. Now set the position to relative.
2. All controls inside the div can use the absolute position but they are still relative to the parent control.
3. In short, you can move the div element and all the textboxes are moved accordingly...

output
====

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="FlowLayout">

    <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxNzcyNjM0OTY0Ozs+MtJfl/e7FQDv7nhIGpLQ2NZeIG0=" />

      Hello World
      <div id="TextBoxPanel" style="POSITION:relative;top:40px;left:50px;">
      <input name="_ctl0" type="text" value="textbox1" style="width:60px;height:20px;position:absolute;top:6px;left:20px;" />
      <input name="_ctl1" type="text" value="textbox2" style="width:60px;height:20px;position:absolute;top:28px;left:20px;" />
      <input name="_ctl2" type="text" value="textbox3" style="width:60px;height:20px;position:absolute;top:50px;left:20px;" />
      <input name="_ctl3" type="text" value="textbox4" style="width:60px;height:20px;position:absolute;top:72px;left:20px;" />
      <input name="_ctl4" type="text" value="textbox5" style="width:60px;height:20px;position:absolute;top:94px;left:20px;" />
      <input name="_ctl5" type="text" value="textbox6" style="width:60px;height:20px;position:absolute;top:116px;left:20px;" />
      <input name="_ctl6" type="text" value="textbox7" style="width:60px;height:20px;position:absolute;top:138px;left:20px;" />
      <input name="_ctl7" type="text" value="textbox8" style="width:60px;height:20px;position:absolute;top:160px;left:20px;" />
</div>
    </form>

  </body>
</HTML>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13806323
you probably wouldn;t like referencing the controls with the names _ctl<number>...
 hence, you shd set the ID of the TextBox like so:

            With box
                .Text = "textbox" & i
                .ID = "textbox" & i
                .Style("width") = "60px"
                .Style("height") = "20px"
                .Visible = True
                .Style("position") = "absolute"
                .Style("top") = CStr(((i - 1) * 22) + 6) & "px"
                .Style("left") = "20px"
            End With

This will cause the output to be changed to so:
<div id="TextBoxPanel" style="POSITION:relative;top:40px;left:50px;">
      <input name="textbox1" type="text" value="textbox1" id="textbox1" style="width:60px;height:20px;position:absolute;top:6px;left:20px;" />
      <input name="textbox2" type="text" value="textbox2" id="textbox2" style="width:60px;height:20px;position:absolute;top:28px;left:20px;" />
      <input name="textbox3" type="text" value="textbox3" id="textbox3" style="width:60px;height:20px;position:absolute;top:50px;left:20px;" />
      <input name="textbox4" type="text" value="textbox4" id="textbox4" style="width:60px;height:20px;position:absolute;top:72px;left:20px;" />
      <input name="textbox5" type="text" value="textbox5" id="textbox5" style="width:60px;height:20px;position:absolute;top:94px;left:20px;" />
      <input name="textbox6" type="text" value="textbox6" id="textbox6" style="width:60px;height:20px;position:absolute;top:116px;left:20px;" />
      <input name="textbox7" type="text" value="textbox7" id="textbox7" style="width:60px;height:20px;position:absolute;top:138px;left:20px;" />
      <input name="textbox8" type="text" value="textbox8" id="textbox8" style="width:60px;height:20px;position:absolute;top:160px;left:20px;" />
</div>
0
 

Author Comment

by:davepruce
ID: 13806389
Thanks b1xml2 for the very detailed and elegant answer, you get the much deserved points.

Thanks

Dave
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question