SitePoint Sponsor

User Tag List

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

    Too many Textboxes

    How do you configure if you have too many textboxes.

    I have set up in Default.aspx.vb:

    Code:
    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
            textbox4.text=JD
         end sub
    end class

    Where am I going wrong?

  2. #2
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    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.

    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <p><asp:Literal ID="Output" runat="server" /></p>
        </form>
    </body>
    </html>

    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>";
                    form1.Controls.Add(beginPara);
     
                    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;
     
                    _textBoxes.Add(textBox);
                    form1.Controls.Add(textBox);
     
                    Literal endPara = new Literal();
                    endPara.Text = "</p>";
                    form1.Controls.Add(endPara);
     
                }
     
            }
     
            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.


Bookmarks

Posting Permissions

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