473,511 Members | 15,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Controls - How to access postback data?

I have a WebForm where I'm dynamically creating some controls and I'm having
difficulty understanding how the state is being persisted and how to work
with it.

I've created a simplified example to demonstrate my issues. Lets say I have
a WebForm with a DropDownList where the user selects a number from 1 to 10
(the DropDownList is not dynamically created). I also have a button on there
that I use to trigger a PostBack.

Based on the user's selection in the drop-down I want to create that many
textboxes programatically. I got this part working easily enough by putting
some code in the Page_Load event. The new textboxes even maintain their
values correctly across post-backs.

But what if I want to put a LiteralControl on my page that will display the
sum of the values entered in all the dynamic textboxes. The problem is where
do I put this code so that it can access the data in the dynamic textboxes
after their values have been restored from the postback data?

Based on my understanding of the page lifecycle (from this article:
http://msdn.microsoft.com/en-us/library/ms972976.aspx) I was assuming that
when I added the TextBoxes to the Panel it was triggering some processing
behind the scenes that caused the "LoadPostbackData" logic to be applied to
the textboxes. This would explain how the textbox values were being
maintained across postbacks. If this were true I should be able to put my
adding code immediately after adding the textboxes to the panels and their
properties should be set to the appropriate values.

This turns out not to work, and the Sum always displays as 0.

If you're going to suggest I use the Init event to create the dynamic
textboxes then I have the problem of retrieving the value from the
DropDownList that tells me how many I need to create, because the
DropDownList's SelectedItem hasn't been updated yet in the Init event because
the PostBack data hasn't been applied (that's why I was using the Load event).

Here is the code I have:

AddingMachine.aspx
==================
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

<asp:Panel ID="Panel1" runat="server"></asp:Panel>
</div>
</form>
</body>
AddingMachine.aspx.cs
=====================
public partial class AddingMachine : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
for (int i = 0; i < Int32.Parse(DropDownList1.SelectedItem.Value);
i++) {
TextBox NewTextBox = new TextBox();
NewTextBox.Text = "0";
Panel1.Controls.Add(NewTextBox);
}

int Sum = 0;

foreach(Control CurControl in Panel1.Controls) {
if(CurControl.GetType() == typeof(TextBox)) {
TextBox TargetTextBox = (TextBox)CurControl;
Sum += Int32.Parse(TargetTextBox.Text);
}
}
Literal1.Text = "Sum = " + Sum.ToString();
}
}

Jul 3 '08 #1
4 4740
DylanSmith presented the following explanation :
I have a WebForm where I'm dynamically creating some controls and I'm having
difficulty understanding how the state is being persisted and how to work
with it.

I've created a simplified example to demonstrate my issues. Lets say I have
a WebForm with a DropDownList where the user selects a number from 1 to 10
(the DropDownList is not dynamically created). I also have a button on there
that I use to trigger a PostBack.

Based on the user's selection in the drop-down I want to create that many
textboxes programatically. I got this part working easily enough by putting
some code in the Page_Load event. The new textboxes even maintain their
values correctly across post-backs.

But what if I want to put a LiteralControl on my page that will display the
sum of the values entered in all the dynamic textboxes. The problem is where
do I put this code so that it can access the data in the dynamic textboxes
after their values have been restored from the postback data?

Based on my understanding of the page lifecycle (from this article:
http://msdn.microsoft.com/en-us/library/ms972976.aspx) I was assuming that
when I added the TextBoxes to the Panel it was triggering some processing
behind the scenes that caused the "LoadPostbackData" logic to be applied to
the textboxes. This would explain how the textbox values were being
maintained across postbacks. If this were true I should be able to put my
adding code immediately after adding the textboxes to the panels and their
properties should be set to the appropriate values.

This turns out not to work, and the Sum always displays as 0.

If you're going to suggest I use the Init event to create the dynamic
textboxes then I have the problem of retrieving the value from the
DropDownList that tells me how many I need to create, because the
DropDownList's SelectedItem hasn't been updated yet in the Init event because
the PostBack data hasn't been applied (that's why I was using the Load
event).

Here is the code I have:

AddingMachine.aspx
==================
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

<asp:Panel ID="Panel1" runat="server"></asp:Panel>
</div>
</form>
</body>
AddingMachine.aspx.cs
=====================
public partial class AddingMachine : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
for (int i = 0; i < Int32.Parse(DropDownList1.SelectedItem.Value);
i++) {
TextBox NewTextBox = new TextBox();
NewTextBox.Text = "0";
Panel1.Controls.Add(NewTextBox);
}

int Sum = 0;

foreach(Control CurControl in Panel1.Controls) {
if(CurControl.GetType() == typeof(TextBox)) {
TextBox TargetTextBox = (TextBox)CurControl;
Sum += Int32.Parse(TargetTextBox.Text);
}
}
Literal1.Text = "Sum = " + Sum.ToString();
}
}
Try moving the sum-code to the PreRender event.
I think that "LoadPostbackData" is executed immediately *after* the
"Load", so too late for your code but in time for the PreRender.

Hans Kesting
Jul 3 '08 #2
"Hans Kesting" wrote:
Try moving the sum-code to the PreRender event.
I think that "LoadPostbackData" is executed immediately *after* the
"Load", so too late for your code but in time for the PreRender.

Hans Kesting

Thanks Hans, that works great and should solve both my simplifiied scenario
and my actual real-world scenario.

But just for curiosities sake, what if I had a more involved scenario where
I needed to read the value of my drop-down and use that to create some
dynamic controls, then read the values in my dynamic controls and use that to
create some more and then read the values of those and perform some logic,
and so on and so on.

Is there a way to explicitly force the "LoadPostbackData" processing to
occur without having to wait for the PreRender event?
Jul 3 '08 #3
Hi'

Can u send me the code.because i am also having the same problem pls send me
Aug 18 '08 #4
Hello kumar,
Can u send me the code.because i am also having the same problem pls
send me
Kumar this seems to ba a bit vague. I cannot tell who you are replying to.

Perhaps choose to "Reply" to the original post instead of creating a "new
post" ?

--
Rory
Aug 18 '08 #5

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

Similar topics

1
2661
by: Scott Schluer | last post by:
Hello, I've got myself a small problem and I'm hoping someone can help. I have a DataList called dlProducts (displays products from a database). Within the <ItemTemplate> container of the...
13
3648
by: Chris Thunell | last post by:
I have created several grids dynamically and have added them to different HTML placeholders on a vb.net web form. The grids and controls within them come up and view beautifully when the web page...
2
2541
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired...
3
1798
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a...
1
2344
by: Gummy | last post by:
Hello, I am loading several user controls dynamically in OnInit() like this: ucListBoxSelections ucLocation = (ucListBoxSelections)LoadControl("UserControls/ucListBoxSelections.ascx");
9
3608
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
3
3090
by: Andreas Wöckl | last post by:
Hi Group! I have a web form that is created dynamically - so I create Textboxes, RadioButtonLists, CheckBoxLists and so on - I have found some articles that there may be some problems with...
2
5005
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
3
4348
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.