472,969 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4707
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
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
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
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
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
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
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
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
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
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.