Don’t mess with my textboxes

As I was putting together some quick ‘n dirty code to render some user controlled number of text boxes, I got this funny behavior.

This was the code for my aspx file:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1">
<div>
    <asp:DropDownList ID="n" runat="server" AutoPostBack="true" OnSelectedIndexChanged="NSelectedIndexChanged">
         <asp:ListItem Text="1" Value="1" />
         <asp:ListItem Text="2" Value="2" />
         <asp:ListItem Text="3" Value="3" />
         <asp:ListItem Text="4" Value="4" />
         <asp:ListItem Text="5" Value="5" />
         <asp:ListItem Text="6" Value="6" />
         <asp:ListItem Text="7" Value="7" />
    </asp:DropDownList><br />
        <% for(int i= 0; i < Total; i++)  {%>
<asp:TextBox ID="txt" runat="server" Text="" /%><br /%>
        <%  }%>
</div>
</form>
</body>
</html>

and this was the code-behind:

using System;
using System.Web.UI;

public partial class _Default : Page
{
	private int _total;

	public int Total
	{
		get { return _total; }
		set { _total = value; }
	}

	protected void NSelectedIndexChanged(object sender, EventArgs e)
	{
		_total = Convert.ToInt32(n.SelectedValue);
	}
}

Now… when I run the app, and choose some value from the dropdown list it runs OK (beside all the text boxes having the same id). But when i select some, different value 2 or 3 times, some funny values (random number of ‘,’ come in to play on each of text box’s value).

I’m running Visual Studio 2005 here, targeting .NET Framework 2.0. Is the same happening to you ?

Here is the attached TestCode.

Share this post

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Send to twitter

5 Comments to “Don’t mess with my textboxes”

  1. Duarte Leonardo says:

    And do you have an explanation for this funny behavior?

  2. Luis de Matos says:

    Oi Paulo,

    olha deve ser um problema do asp.net a tua rotina esta correcta

    corrigi o problema fazendo uma pequena alteração:

    alterei o objecto asp.net textbox para um input type text

    fica bem e bom natal.

    Untitled Page

    <% for(int i= 0; i

  3. Luis de Matos says:

    na linha 21 altera de:

    para

    e desaparece esse situação anormal ;)

  4. Paulo Nobre says:

    Certo, mas a questão não é mudar de asp.net server controls para html server controls. Este comportamento é estranho, mas é by design. Desculpa lá o atraso, mas só agora vi os comentários.

Leave a Reply