page events - please help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: page events - please help

  1. #1
    Join Date
    Nov 2008
    Posts
    1

    Question page events - please help

    please help to understand why it is working the way it is working....if I click on button1 table1 will get 1 extra textbox, no matter how many times I click after that it will be still 2 textboxes and once I click button2 table1 one will appear with only one textbox again, but with listbox items everything working as I wanted (button1 add items and button2 count them)
    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim tr As New TableRow
    Dim tc As New TableCell
    
    tc.Controls.Add(New TextBox()) 
    tr.Cells.Add(tc)
    Table1.Rows.Add(tr)
    
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim tr As New TableRow
    Dim tc As New TableCell
    
    tc.Controls.Add(New TextBox()) 
    tr.Cells.Add(tc)
    Table1.Rows.Add(tr)
    ListBox1.Items.Add("makarena") 
    
    End Sub
    
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click 
    
    Label1.Text = Table1.Rows.Count
    Label2.Text = ListBox1.Items.Count
    End Sub
    HTML CODE:
    Code:
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Table ID="Table1" runat="server">
            </asp:Table><br>
            <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br>
            <asp:Button ID="Button1" runat="server" Text="Button1" /><br>
            <asp:Button ID="Button2" runat="server" Text="Button2" /><br>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
        </form>
    </body>
    Last edited by Hack; 11-13-2008 at 07:31 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Sep 2007
    Posts
    86
    Hello,

    This is a side effect of the way you are adding controls to the page. Here is a link to a good article about dynamically adding controls to an ASP.NET Table control.


    Of particular interest should be this Note that explains what you are seeing:

    Note: Controls that we add dynamically to a Web Forms page do not automatically become part of the page's view state when a page performs a round trip to the server; neither the controls nor their values are saved. Therefore we are responsible for saving the state of any dynamically generated controls whose values we want to preserve.
    John Wiese
    ISV Architect Evangelist - Microsoft
    http://blogs.msdn.com/usisvde

Similar Threads

  1. Replies: 0
    Last Post: 08-30-2007, 03:51 PM
  2. Loading Page Variables
    By hoyaabanks in forum ASP.NET
    Replies: 0
    Last Post: 02-26-2007, 03:46 PM
  3. HELP!!! redirect my page....
    By dare2design in forum Web
    Replies: 1
    Last Post: 01-09-2006, 12:49 PM
  4. Replies: 4
    Last Post: 06-24-2005, 01:40 AM
  5. Sharing events
    By Gus in forum Web
    Replies: 1
    Last Post: 07-26-2002, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center