Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic Controls - How to access postback data?

Reply
Thread Tools

Dynamic Controls - How to access postback data?

 
 
DylanSmith
Guest
Posts: n/a
 
      07-03-2008
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>

<aspropDownList 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>
</aspropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Literal ID="Literal1" runat="server"></asp:Literal>

<aspanel ID="Panel1" runat="server"></aspanel>
</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();
}
}

 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      07-03-2008
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>
>
> <aspropDownList 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>
> </aspropDownList>
> <asp:Button ID="Button1" runat="server" Text="Button" />
> <asp:Literal ID="Literal1" runat="server"></asp:Literal>
>
> <aspanel ID="Panel1" runat="server"></aspanel>
> </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


 
Reply With Quote
 
 
 
 
DylanSmith
Guest
Posts: n/a
 
      07-03-2008
"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?
 
Reply With Quote
 
kumar r
Guest
Posts: n/a
 
      08-18-2008
Hi'

Can u send me the code.because i am also having the same problem pls send me
 
Reply With Quote
 
Rory Becker
Guest
Posts: n/a
 
      08-18-2008
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" ?

--
Ror


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dynamic Controls created by Dynamic Controls Ronald ASP .Net 2 01-17-2006 12:44 AM
stop postback in postback events for server controls ?? Wael_Bakr ASP .Net Web Controls 0 11-30-2005 08:06 AM
effecting postback without auto postback controls Psych971 ASP .Net 5 12-17-2004 08:53 PM
Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls Denny Smolinski via .NET 247 ASP .Net Web Controls 1 05-04-2004 09:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57