Search code examples
asp.netvb.netpositioncontrols

Dynamically created Labels and TextBoxes are unable to be positioned


I am building a dynamically created popup window that contains a dynamically created grid and controls (Labels and TextBoxes). The grid part is working correctly, and not an issue. However, the controls are giving me issues. The idea is to create a Label and a TextBox for each of the columns in the Grid. I am able to create them fine, but am unable to position them at all. label.Location, label.Top, label.point, etc. return an error saying that it is not part of Label. The same thing happens with the TextBox. When they do display, they appear in a horizontal row with the TextBox overlapping the Label unless the width is set sufficiently large enough (which of course leave a bunch of empty space). I would like the to be vertically aligned with the TextBox immediately after the Label. Something like: labelText: TextBox

These controls are being added into an asp:Panel (pnlFields)

For Each col As DataColumn In dataTable.Columns
      Dim label As New Label()
      label.Text = col.ColumnName & ": "
      label.Height = 24
      label.Width = label.Text.Length()
      pnlFields.Controls.Add(label)
      Dim textBox As New TextBox()
      textBox.Height = 24
      textBox.Width = 100
      pnlFields.Controls.Add(textBox)
Next

I would like to try to have the Label width be as long as the text it contains instead of static, that way I would (hopefully) be able to set the TextBox location to be right after the Label.

In any event, I can't seem to specify a position at all in order to do this. Any ideas?

Thanks!

Edit: Markup

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Panel runat="server" ID="pnlFields" CssClass="FormStyle" Height="600px" />  
      <asp:Panel runat="server" ID="pnlFinder" CssClass="FormStyle">
       <telerik:RadGrid ID="batchRecords" runat="server" Skin="Windows7" ShowHeader="true" AutoGenerateColumns="true" Width="600px" AutoPostBack="true">
           <MasterTableView CommandItemDisplay="Bottom" AllowPaging="true">
               <CommandItemSettings ShowAddNewRecordButton="false" />
               <Columns>
                   <telerik:GridButtonColumn UniqueName="selectRecord" HeaderText="Edit" CommandName="Select" Text="Edit Record" ButtonType="ImageButton" ImageUrl="~/images/icons/pencil.png">
                   </telerik:GridButtonColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
   </asp:Panel>
</asp:Content>

Solution

  • Edit: I solved this by creating an HTML Table in the markup, then programmatically adding Rows and Cells with controls (Labels and TextBoxes) and unique IDs. I was then able to find and populate those controls with the correct data. Worked MUCH easier than trying to manually position each control.

    I was able to solve the issue of positioning the controls. I needed to use label.Style.Add() and put in the values needeed. It seems to be working correctly now.

    Thanks!