passing values from programmatically created controls

Discussion in 'ASP .Net' started by TB, Apr 5, 2006.

  1. TB

    TB Guest

    Hi All:

    I have this page where a rows / cells are programmatically added to to table
    by pushing a button.

    The rows contain a textbox and a associated button. What I want to is to be
    able to add the content of any textbox to a global variable (and a related
    session variable) when pushing the associate button.

    However whenever I push the button(s) apparently the session variable is not
    correctly updated and therefore nothing is updated. I have a feeling that it
    has something to do with the "handles" argument in the click event of the
    programmatically created buttons and / or the dynamically assigned textbox
    and button names, but I am not sure.

    Below is simplied version of the page

    Content of sessiontest.aspx:

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="sessiontest.aspx.vb" Inherits="testsite.sessiontest"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>sessiontest</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:placeholder id="plhtable" Runat="server"></asp:placeholder><br>

    <asp:Button ID="btnAddrow" Text="Add Row" Runat="server" />
    </form>
    </body>
    </HTML>

    Contant of sessiontest.aspx.vb

    Public Class sessiontest
    Inherits System.Web.UI.Page

    Dim td As TableCell
    Dim tb As Table
    Dim tr As TableRow
    Dim btntest As Button
    Dim txbtest As TextBox
    DIM strResult as string ' This is where I want to store everything

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    tb = New Table
    addrows()

    Else
    tb = Session("table")
    strResult = Session("strResult")
    End If
    plhtable.Controls.Add(tb)
    End Sub
    Sub page_prerender(ByVal sender As Object, ByVal e As EventArgs) Handles
    MyBase.PreRender
    Session("Table") = tb
    Session("strResult") = strResult
    End Sub
    Sub addrows()

    tr = New TableRow
    txbtest = New TextBox

    td = New TableCell
    td.Text = "Enter text:"
    tr.Cells.Add(td)

    td = New TableCell
    txbtest.ID = "txbNumber" & tb.Rows.Count()
    td.Controls.Add(txbtest)
    tr.Cells.Add(td)

    td = New TableCell
    btntest = New Button
    AddHandler btntest.Click, AddressOf valuefromtxb_click
    btntest.ID = "btnNumber" & tb.Rows.Count()
    btntest.Text = "Button #" & tb.Rows.Count()

    td.Controls.Add(btntest)
    tr.Cells.Add(td)

    tb.Rows.Add(tr)
    End Sub

    Private Sub fromtxb_click(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    counter = counter + 1
    strResult = strResult % & txbtest.Text

    End Sub
    Private Sub btnAddrow_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnAddrow.Click
    addrows()
    End Sub
    End Class


    Any suggestions will be higly appreciated.

    Thanks.

    TB
    TB, Apr 5, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    9
    Views:
    566
    Rob Meade
    Aug 10, 2004
  2. Replies:
    0
    Views:
    405
  3. Ramesh
    Replies:
    1
    Views:
    461
    Ramesh
    Aug 17, 2006
  4. Replies:
    3
    Views:
    182
  5. Access Programmatically created controls

    , Sep 20, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    173
Loading...

Share This Page