473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

populating variables from dynamically created controls

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

Apr 5 '06 #1
6 1765
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
Apr 6 '06 #2
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

Apr 6 '06 #3
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
Apr 6 '06 #4
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

Apr 6 '06 #5
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
Apr 6 '06 #6
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

Apr 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
6
by: eureka | last post by:
Hi friends, I am developing a web application using Jsp and JS. I have a main Jsp page(Jsp1).Inside it I have an iframe having an Html- table which is created dynamically and contains all...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.