Home All Groups Group Topic Archive Search About
Author
6 Dec 2006 2:49 PM
usamaalam
Hello everybody,

I am generating a dynamic web form based on the options in database that are
set by the admin (sort of a survey form). On the page load event, it loop
through the database values and generate different types of controls like
Label, RadioButton, TextBox etc. in a Repeater control like
rptQuestionsHTML.Controls.Add(SomeControl).

On the click of a button, I am trying to read the value in a
programmatically generated control but unable to find the control.

controlID = rptQuestionsHTML.ID.ToString() + "_" + "optAnswer_" +
dtAnswerChoice.Rows[i]["ID"].ToString().Trim();           
answerRadioButton = (RadioButton) Page.FindControl(controlID);
isChecked = answerRadioButton.Checked;

The value the controlID has is present in the HTML source of the page but
the FindControl method returns nothing to the radiobutton object
answerRadioButton.

Any ideas??

Thanks.

Author
6 Dec 2006 7:45 PM
Robert Bernhardt
usamaalam schrieb:
Show quoteHide quote
> Hello everybody,
>
> I am generating a dynamic web form based on the options in database that are
> set by the admin (sort of a survey form). On the page load event, it loop
> through the database values and generate different types of controls like
> Label, RadioButton, TextBox etc. in a Repeater control like
> rptQuestionsHTML.Controls.Add(SomeControl).
>
> On the click of a button, I am trying to read the value in a
> programmatically generated control but unable to find the control.
>
> controlID = rptQuestionsHTML.ID.ToString() + "_" + "optAnswer_" +
> dtAnswerChoice.Rows[i]["ID"].ToString().Trim();           
> answerRadioButton = (RadioButton) Page.FindControl(controlID);
> isChecked = answerRadioButton.Checked;
>
> The value the controlID has is present in the HTML source of the page but
> the FindControl method returns nothing to the radiobutton object
> answerRadioButton.

Hi.

try to use "rptQuestionsHTML.UniqueId" instead of "rptQuestionsHTML.ID".

rb
Author
7 Dec 2006 8:25 AM
usamaalam
It doesn't work, basically whether I use rptQuestionsHTML.ID or
rptQuestionsHTML.UniqueId, the control Id generated is present on the page(I
can see the control by viewing the page source) but it doesn't get the
control programmatically.  The reason I think is on the click of the button,
form posts back and the controls that were generated programmatically are now
gone because on every post back, we have a set of different controls on the
page.

I am searching the solution to this problem.

Thanks.

Show quoteHide quote
"Robert Bernhardt" wrote:

> usamaalam schrieb:
> > Hello everybody,
> >
> > I am generating a dynamic web form based on the options in database that are
> > set by the admin (sort of a survey form). On the page load event, it loop
> > through the database values and generate different types of controls like
> > Label, RadioButton, TextBox etc. in a Repeater control like
> > rptQuestionsHTML.Controls.Add(SomeControl).
> >
> > On the click of a button, I am trying to read the value in a
> > programmatically generated control but unable to find the control.
> >
> > controlID = rptQuestionsHTML.ID.ToString() + "_" + "optAnswer_" +
> > dtAnswerChoice.Rows[i]["ID"].ToString().Trim();           
> > answerRadioButton = (RadioButton) Page.FindControl(controlID);
> > isChecked = answerRadioButton.Checked;
> >
> > The value the controlID has is present in the HTML source of the page but
> > the FindControl method returns nothing to the radiobutton object
> > answerRadioButton.
>
> Hi.
>
> try to use "rptQuestionsHTML.UniqueId" instead of "rptQuestionsHTML.ID".
>
> rb
>
Author
7 Dec 2006 8:23 PM
Robert Bernhardt
usamaalam schrieb:
> It doesn't work, basically whether I use rptQuestionsHTML.ID or
> rptQuestionsHTML.UniqueId, the control Id generated is present on the page(I
> can see the control by viewing the page source) but it doesn't get the
> control programmatically.  The reason I think is on the click of the button,
> form posts back and the controls that were generated programmatically are now
> gone because on every post back, we have a set of different controls on the
> page.
hi,

you must also append the controls on postback and than you can iterate
the repeater items to get all included controls.

rb
Author
8 Dec 2006 7:37 AM
usamaalam
How can I append controls on postback?  Can you give an example?

Let me explain you the scenario.  I need to generate a multi-page survey
form on a single page.  Why I need to use single page because there is no
specific page size.  Survey creator specified that I want to expand the
survey on say 5 pages.

Now what is happending right now is that when the survey page is called, it
first fetches data from the database on what questions need to be displayed
on the first page and what possible answer choices for each page (i.e.
textbox for text type questions, checkboxes for multiple choice questions
etc.) and generates the form for the first page.  This whole process goes on
Page_Load event and it works fine.  Now there's a button on the page which we
call Next button.  On the click of Next button, old form is saved and the
form for page 2 is generated and so on.  When the button is clicked,
Page_Load event is fired before Button Click event.  Page_Load event
explicitly fires button click event before generating the page 2 form and
sets a flag variable so that the Click event will not fire again.  When the
Click event is fired by Page_Load event, we cannot get the controls even the
controls collection has 0 controls in it so it gives Object reference not set
to an instance of an object exception.

What I need to do is when the click event is fired from Page_Load event, I
want to get the values of the previous dynamic controls and save them in
database before generating the next page.

Thanks a lot.

Show quoteHide quote
"Robert Bernhardt" wrote:

> usamaalam schrieb:
> > It doesn't work, basically whether I use rptQuestionsHTML.ID or
> > rptQuestionsHTML.UniqueId, the control Id generated is present on the page(I
> > can see the control by viewing the page source) but it doesn't get the
> > control programmatically.  The reason I think is on the click of the button,
> > form posts back and the controls that were generated programmatically are now
> > gone because on every post back, we have a set of different controls on the
> > page.
> hi,
>
> you must also append the controls on postback and than you can iterate
> the repeater items to get all included controls.
>
> rb
>
Author
8 Dec 2006 5:15 PM
gerry
2 issues :

1)  to get the data out of dynamic controls on post back , in the Init
method ( usually ) , you have to rebuild the page's controls exactly the
same way they were built when the page was served.
Of course you could always forget about the controls on postback and parse
out the request variables yourself to get at the posted data.

2) the problem you are having is, IMO, a problems with where you are doing
things, the page dynamic controls should be built in the PreRender not
OnLoad.
OnLoad should be used to deal with the page being posted whereas PreRender
should be used to build the page being served.
    OnInit - postback - rebuild page controls for page being posted
    OnLoad - postback - save posted data etc
    OnButtonClick etc ... handle posted page events
    PreRender - build controls for page being served.


Gerry




Show quoteHide quote
"usamaalam" <usamaa***@discussions.microsoft.com> wrote in message
news:DDC9B9FB-A90E-42CC-9D94-C0C7B3700564@microsoft.com...
> How can I append controls on postback?  Can you give an example?
>
> Let me explain you the scenario.  I need to generate a multi-page survey
> form on a single page.  Why I need to use single page because there is no
> specific page size.  Survey creator specified that I want to expand the
> survey on say 5 pages.
>
> Now what is happending right now is that when the survey page is called,
> it
> first fetches data from the database on what questions need to be
> displayed
> on the first page and what possible answer choices for each page (i.e.
> textbox for text type questions, checkboxes for multiple choice questions
> etc.) and generates the form for the first page.  This whole process goes
> on
> Page_Load event and it works fine.  Now there's a button on the page which
> we
> call Next button.  On the click of Next button, old form is saved and the
> form for page 2 is generated and so on.  When the button is clicked,
> Page_Load event is fired before Button Click event.  Page_Load event
> explicitly fires button click event before generating the page 2 form and
> sets a flag variable so that the Click event will not fire again.  When
> the
> Click event is fired by Page_Load event, we cannot get the controls even
> the
> controls collection has 0 controls in it so it gives Object reference not
> set
> to an instance of an object exception.
>
> What I need to do is when the click event is fired from Page_Load event, I
> want to get the values of the previous dynamic controls and save them in
> database before generating the next page.
>
> Thanks a lot.
>
> "Robert Bernhardt" wrote:
>
>> usamaalam schrieb:
>> > It doesn't work, basically whether I use rptQuestionsHTML.ID or
>> > rptQuestionsHTML.UniqueId, the control Id generated is present on the
>> > page(I
>> > can see the control by viewing the page source) but it doesn't get the
>> > control programmatically.  The reason I think is on the click of the
>> > button,
>> > form posts back and the controls that were generated programmatically
>> > are now
>> > gone because on every post back, we have a set of different controls on
>> > the
>> > page.
>> hi,
>>
>> you must also append the controls on postback and than you can iterate
>> the repeater items to get all included controls.
>>
>> rb
>>
Author
10 Dec 2006 8:19 AM
usamaalam
I have got it working using Request.Form[].

Thanks a lot.

Show quoteHide quote
"gerry" wrote:

> 2 issues :
>
> 1)  to get the data out of dynamic controls on post back , in the Init
> method ( usually ) , you have to rebuild the page's controls exactly the
> same way they were built when the page was served.
> Of course you could always forget about the controls on postback and parse
> out the request variables yourself to get at the posted data.
>
> 2) the problem you are having is, IMO, a problems with where you are doing
> things, the page dynamic controls should be built in the PreRender not
> OnLoad.
> OnLoad should be used to deal with the page being posted whereas PreRender
> should be used to build the page being served.
>     OnInit - postback - rebuild page controls for page being posted
>     OnLoad - postback - save posted data etc
>     OnButtonClick etc ... handle posted page events
>     PreRender - build controls for page being served.
>
>
> Gerry
>
>
>
>
> "usamaalam" <usamaa***@discussions.microsoft.com> wrote in message
> news:DDC9B9FB-A90E-42CC-9D94-C0C7B3700564@microsoft.com...
> > How can I append controls on postback?  Can you give an example?
> >
> > Let me explain you the scenario.  I need to generate a multi-page survey
> > form on a single page.  Why I need to use single page because there is no
> > specific page size.  Survey creator specified that I want to expand the
> > survey on say 5 pages.
> >
> > Now what is happending right now is that when the survey page is called,
> > it
> > first fetches data from the database on what questions need to be
> > displayed
> > on the first page and what possible answer choices for each page (i.e.
> > textbox for text type questions, checkboxes for multiple choice questions
> > etc.) and generates the form for the first page.  This whole process goes
> > on
> > Page_Load event and it works fine.  Now there's a button on the page which
> > we
> > call Next button.  On the click of Next button, old form is saved and the
> > form for page 2 is generated and so on.  When the button is clicked,
> > Page_Load event is fired before Button Click event.  Page_Load event
> > explicitly fires button click event before generating the page 2 form and
> > sets a flag variable so that the Click event will not fire again.  When
> > the
> > Click event is fired by Page_Load event, we cannot get the controls even
> > the
> > controls collection has 0 controls in it so it gives Object reference not
> > set
> > to an instance of an object exception.
> >
> > What I need to do is when the click event is fired from Page_Load event, I
> > want to get the values of the previous dynamic controls and save them in
> > database before generating the next page.
> >
> > Thanks a lot.
> >
> > "Robert Bernhardt" wrote:
> >
> >> usamaalam schrieb:
> >> > It doesn't work, basically whether I use rptQuestionsHTML.ID or
> >> > rptQuestionsHTML.UniqueId, the control Id generated is present on the
> >> > page(I
> >> > can see the control by viewing the page source) but it doesn't get the
> >> > control programmatically.  The reason I think is on the click of the
> >> > button,
> >> > form posts back and the controls that were generated programmatically
> >> > are now
> >> > gone because on every post back, we have a set of different controls on
> >> > the
> >> > page.
> >> hi,
> >>
> >> you must also append the controls on postback and than you can iterate
> >> the repeater items to get all included controls.
> >>
> >> rb
> >>
>
>
>