MultiPage Expert Please (Dynamic MultiPage controls)

R

Russ

I'm getting a little frazzled here. I have been trying to figure out
how to extract the data from my dynamically created Multipage controls
for about a week now!

Depending on data from a web service, I am adding one or more Tabs and
PageViews to a design time created TabStrip and MultiPage control.
Each PageView then gets a number of TextBoxes. Here is how I am
creating the controls:

for (int i=0; i < Ed.nRates; ++i)
{
// Ed is the data from the web service.
// Ed.Rd is an array of data, one element for each rate
CEmpRateData ERate = Ed.Rd ;

if (i != 0)
{
TabSeparator ts = new TabSeparator ();
TabStrip1.Items.Add (ts);
}

Tab t = new Tab ();
t.Text = "Rate " + (ERate.m_Rate + 1);
TabStrip1.Items.Add (t);

PageView pv = new PageView ();
MultiPage1.Controls.Add (pv);

AddField (pv, "Regular Hours", 115,20, 40, 10);
AddField (pv, "Overtime Hours", 115,50, 40, 10);
 
R

Russ

Scott, thanks very much for the prompt response. Did I mention that I
am new to about all aspects of this? I guess not but maybe it was
obvious by my question. Anyway I'm not sure I understand what
postback has to do with it. Earlier I tried to find a definition of
'postback' and failed, everything I could find seemed circular:
"Postback is when the client posts back".

But I think that postback is when the web page is recreated after
passing control to the client and the client puts the same page up
again. If that is correct, then I am not doing a postback because
immediately after the submit operation is completed I am doing a
Server.Transfer to a different web page. In other words I am not
expecting the page to be recreated at all.

So, I have to conclude that postback means more than I think it does,
or else people are using it to mean more than it does. Either way, I
will research what you have sent me and see if I can learn to make it
work.

Thanks again, Russ
 
S

Scott G.

A postback is kind-a the same as "Submit" (in the simple old world of CGI). The first time a page is generated is "not posted back"; after that the controls on the page (buttons + other javascript events) all do a "postback" (i.e. postback comes from the HTTP POST). I'm simplifying here....

In your example, you said the page was generated, but when a "SubmitButton_Click" event happens the page was missing the controls -- the "SubmitButton_Click" is the result of a postback --- thus, to actually get at the controls in the SubmitButton_Click you have to recreate the controls (and in my code I posted the CreateChildControls is called during the postback and the controls will be populated with the "data" from the user).

Scott

Scott, thanks very much for the prompt response. Did I mention that I
am new to about all aspects of this? I guess not but maybe it was
obvious by my question. Anyway I'm not sure I understand what
postback has to do with it. Earlier I tried to find a definition of
'postback' and failed, everything I could find seemed circular:
"Postback is when the client posts back".

But I think that postback is when the web page is recreated after
passing control to the client and the client puts the same page up
again. If that is correct, then I am not doing a postback because
immediately after the submit operation is completed I am doing a
Server.Transfer to a different web page. In other words I am not
expecting the page to be recreated at all.

So, I have to conclude that postback means more than I think it does,
or else people are using it to mean more than it does. Either way, I
will research what you have sent me and see if I can learn to make it
work.

Thanks again, Russ
 
R

Russ

Hi again Scott. Ok, I have tried what you suggested as well as I can
comprehend and have failed again. The new control class appears to do
nothing - not even display. I created a composite control class along
the lines you suggested, after doing the reading you suggested. Then
I created an instance of the new class, setting it's data members to
contain the information needed to create all the pages and controls.

When it did not work, I tried placing a breakpoint in the
CreateChildControls routine, and it never gets there. When is that
routine supposed to be called?

Below is the code I used, with some settings omitted for brevity. (I
didn't assign any id's to the TextBoxes yet because I just want to see
it display properly before starting to worry about getting the data
out.)

Thanks for any additional help you can provide.

Russ

In my class definition:

MultiTabCompositeControl TabCtrl;


In Page_Load (not within IsPostBack):

TabCtrl = new MultiTabCompositeControl ();
TabCtrl.nRates = (int) Ed.nRates;
TabCtrl.Er = Ed.Rd; // The array of rate data

// The composite class
public class MultiTabCompositeControl : Control, INamingContainer
{
public override ControlCollection Controls
{
get { this.EnsureChildControls(); return base.Controls; }
}

public int nRates; // number of pages needed
public CEmpRateData [] Er;

TabStrip FormTabs;
MultiPage FormPages;

protected override void CreateChildControls ()
{
this.Controls.Clear ();
FormTabs = new TabStrip (); // important that is
created...
FormTabs.ID = "TS";
FormTabs.Style.Add ("POSITION", "absolute");
FormTabs.Style.Add ("LEFT", "10px");
FormTabs.Style.Add ("TOP", "280px");
 
R

Russ

Ahh, I see. I figured there was more to it than I was thinking about.
Now if I could only get it to work. (See my last posting for details
of current try).

Thanks, Russ
 
R

Russ

Ok Scott. I've got it working:) Part of my problem was the transition
from regular windows programming to web programming. I have now
deduced that I cannot create controls in the code behind and have them
appear in the browser - I have to declare them in HTML. I thought the
system would automatically generate the HTML for me if they were
instantiated in code.

So, I removed: TabCtrl = new MultiTabCompositeControl (), from my code
and added the following to my aspx file:

<%@ Register TagPrefix="mtcc" Namespace="MultiTabCompositeControls"
Assembly="PayrollEntryClient" %>

and in the Form:

<mtcc:MultiTabCompositeControl id="TabCtrl1"
runat="server"></mtcc:MultiTabCompositeControl>

(All Styles and other settings are done in code).

This now starts to work. The tabs and pages come up and display just
the same as they did before. And when the submit event fires, the
CreateChildControls() routine is executed first. However there were
still problems. I saw three significant differences between what you
last showed and what I am doing/need.

1. Your code is all in script while mine is all in code-behind. I am
don't think this makes any difference..

2. Your example does not depend upon any values external to the
composite control class in order to generate the internal controls.

3. You are not pre-setting the text boxes to zero like I do. I am
setting the initial values in the create routines, so when they are
re-created they would all be zero again.

With regard to #2, I am handling the external values by loading them
into the composite control class variables at page load time. But the
problem was that when CreateChildControls() is called at submit time,
the variables are no longer loaded. So, the pages and text boxes
could not be recreated. I solved that problem by modifying the
composite control class to store/retrieve the necessary data in
ViewState.

For item number 3 I simply used an if (! Page.IsPostBack) around the
point where I set the box text to zero. This worked, but later I
tested and determined it is not necessary. Obviously the boxes are
loaded from the ViewState afterwards.

Now I can use the LocateControl routine you showed to get the values
at submit time! Fantastic! Thanks so much for your help.

A note and a question:

1. The above is sufficient for my purposes, but if I allow the page to
be updated (IOW if I don't transfer to another page after the submit),
the page redraws without the tabs. This was fixed by removing the if
(! Page.IsPostBack) from the tab creations. Apparently this is needed
too.

2. My question is if there is now a way I can data bind the text boxes
to some class variables and eliminate the need to extract the value
from each text box individually? Will data binding work
automatically, or will I still have to write code to extract the
values? And is it possible to bind an array to a group of text boxes,
or would I have to bind each element of the array separately.

Scott, thank you again for your help. Now that I have it working, I
can see very well what the issues are, but without help it was
impossible for me to figure it out.

Regards, Russ
 

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

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top