populating variables from dynamically created controls

T

TB

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 valuefromtxb_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
 
C

Cor Ligthert [MVP]

TB,
Any suggestions will be higly appreciated.

A simple suggestion, brign your code back to the essentials. That gives you
much more change on help in this newsgroup.

Cor
 
T

TB

I'll do anything you say, but since I am rather ignorant in these
matters, I wouldn't know how to bring my code back to those essentials
you refer to. Please advise.

TB
 
C

Cor Ligthert [MVP]

TB,

Just try it by instance with one control in one cell in a table. Probably
you find it than yourself, however, if not, than the sample you give us is
probably much easier to read.

Just my idea

Cor
 
T

TB

Following your advice, I have now simplified the code. Now no table
rows are programmatically created and subsequently filled with various
controls, but simply buttons, which when hit are supposed to change the
content of a global variable which should be displayed via a label.
However, the problem is still the same: the buttons are created but the
the line "AddHandler btntest.Click, AddressOf updateresult" doesn´t
work.

Below is the code (hopefully sufficiently reduced to the necessary
essentials):

Content of sessiontest2.aspx

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

content of sessiontest.aspx.vb

Public Class sessiontest2
Inherits System.Web.UI.Page

Dim counter As Integer
Dim btntest As Button
Dim strResult As String
Dim plhmain As placeholder
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
strResult = "nothing at the moment"
counter = 1
plhmain = New PlaceHolder
Else
plhmain = Session("plhmain")
strResult = Session("strResult")
counter = Session("counter")
End If
End Sub
Sub page_prerender(ByVal sender As Object, ByVal e As EventArgs)
Handles MyBase.PreRender
Session("plhmain") = plhmain
Session("strResult") = strResult
Session("counter") = counter
End Sub
Sub addbutton()
btntest = New Button
AddHandler btntest.Click, AddressOf updateresult
btntest.ID = "BtnNumber" & counter
btntest.Text = "Button #" & counter
counter = counter + 1
plhmain.Controls.Add(btntest)
Dim spacer As LiteralControl = New LiteralControl("<br>")
plhmain.Controls.Add(spacer)
plhtest.Controls.Add(plhmain)
End Sub
Private Sub updateresult(ByVal sender As System.Object, ByVal e As
System.EventArgs)
strResult = strResult & btntest.ID
lblresult.Text = strResult
End Sub

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

Thanks in advance
 
C

Cor Ligthert [MVP]

TB,

The buttons that you have created on your form dynamicly, do you need to
recreate again with any postback to be able to use them. I saw that not in
your code, maybe I missed it, however. If not, thatn you will in your case
probably have to do that using the counter you have already in the session..

I hope this helps,

Cor
 
T

TB

I think it has something to do with the naming of the dynamically
created controls becuase if I simplify it even further, simply creating
a page where ONE button is created dynamically ONCE and an event
handler is attached, then the event actually fires (once of course):

Content of dynamic.aspx

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="dynamic.aspx.vb" Inherits="testsite.dynamic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>dynamic</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="plh" Runat="server" /><br>
<asp:Label ID="lblresult" Runat="server" />
</form>
</body>
</HTML>

content of dynamic.aspx.vb:

Public Class dynamic
Inherits System.Web.UI.Page

Dim btntest As Button
Dim strResult As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
btntest = New Button
AddHandler btntest.Click, AddressOf updateresult
btntest.ID = "BtnNumber"
btntest.Text = "Button #"
plh.Controls.Add(btntest)

End Sub
Sub updateresult(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim spacer As LiteralControl = New LiteralControl("Hello<br>")
plh.Controls.Add(spacer)
End Sub
End Class

So why doesn´t it work when multiple controls are dynamically created
all with the same event handler??

TB
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top