SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    0 Post(s)
    0 Thread(s)

    Too many Textboxes

    How do you configure if you have too many textboxes.

    I have set up in Default.aspx.vb:

    Imports Microsoft.VisualBasic
    Imports System.Diagnostics
    Public Class _Default
        Inherits System.Web.UI.Page
        Dim WithEvents Button1 As New Button
        Dim textbox1, textbox2, textbox3, textbox4 As New TextBox
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pi, mo, dy, yr, lon, lat As Double
            mo = CDbl(textbox1.Text)
            dy = CDbl(textbox2.Text)
            yr = CDbl(textbox3.Text)
            JD=367*yr-int(7/4*(yr+int((mo+9)/12))) +dy+1721013.5
         end sub
    end class

    Where am I going wrong?

  2. #2
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    26 Post(s)
    0 Thread(s)
    Using old-style control arrays no longer works. But there's a way to get around it! Instead of drag-dropping the controls onto your form, add a control array to your underlying class, create your controls dynamically, add it to your custom array, then write it out to the form. Here's a better explanation (though long). This article also explains how to hook up your event handlers and so on. Good luck.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    26 Post(s)
    0 Thread(s)
    Here is a simple example (you'll have to convert to VB as I don't do VB):

    Code ASP:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
        <p><asp:Literal ID="Output" runat="server" /></p>

    Code Csharp:
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace WebApplication1
        public partial class WebForm1 : Page
            private IList<TextBox> _textBoxes = new List<TextBox>();
            protected void Page_Init(object sender, EventArgs e)
                Output.Text = "Waiting for input...";
                for (int index = 0; index < 10; index++)
                    Literal beginPara = new Literal();
                    beginPara.Text = "<p>";
                    TextBox textBox = new TextBox();
                    // setting ID, the event handler and autopostback is important here!
                    textBox.ID = String.Format("TextBox{0}", index.ToString());
                    textBox.TextChanged += new EventHandler(TextBox_TextChanged);
                    textBox.AutoPostBack = true;
                    Literal endPara = new Literal();
                    endPara.Text = "</p>";
            protected void TextBox_TextChanged(object sender, EventArgs e)
                Output.Text = (sender as TextBox).Text;

    There is probably a much easier way using the asp:repeater control, but I've been away from WebForms for far too long to provide that answer.

    In your case, forego attaching a custom handler to the boxes themselves. Attach it to a single button. Then use the IList variable to access the controls. It saves having to use FindContol later.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts