469,621 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

GridView UserControl Problems

Dear Friends/Gurus,

I have exhausted myself and have yet no solution to the following:

I have an ASP.NET 2.0 Survey type application. On a page, I have placed
a GridView which is bound to an SQLDataSource. The GridView contains 3
columns: QuestionNumber, QuestionText, and a TemplateField.

Each row in the GridView represents a question in a table (Ex.: "18.
Satisfaction Level").

BACKGROUND:

On a per question (row) basis, a user may choose a response using either
a DropDownList, a TextBox, RadioButtonList, etc. For example, a Yes/No type
question displays a DropDownList while a comment type question would displays
a TextBox. I have designed a UserControl for each different type of input.
For example: a DropDownList, an SQLDataSource, a RequiredValidator, Labels,
and formatting are encapsulated in a UserControl called DDLUC. I have also
added a ResponseChosen property which returns the SelectedItem.Text value of
the DropDownList control. The situation is such that each question may be of
a different type and thus requires a different input control. Some of these
controls are they themselves bound to an SQLDataSource which retrieves their
possible values. For example, my DDLUC control must be populated with
possible answers (Excellent, Good, Bad, Poor).

In the main page, I use the GridView's RowDataBound event, (fired once
for each row in my datasource). In this event, I obtain a QuestionTypeID
field. Based on this value (Integer), I use the ".LoadControl" method to
dynamically add one of my custom UserControls to the TemplateField column;
which up until now has been empty. Each Question may be of a different type.
So far, all this works well. Each question is displayed along with its
correct input type UserControl (Ex.: Question 1 has a DropDownList
UserControl, while Question 2 has a Textbox UserControl for comments, etc...)
The problem is as follows:

Once I click on a "Save Responses" button, I cannot seem to iterate
through the Rows in the GridView and get the values of the UserControl stored
in the TemplateField (which now contains the response the user

has chosen/typed). The problem (I am assuming) is that all controls that
are dynamically added using the .LoadControl method must be recreated
everytime. Unfortunately, the RowDataBound event only fires once

when the Page_Load or Page_PreRender event fires which is not the case when
I click on the "Save Responses" button. I have tried to use the .FindControl
of the Row, and the Cells, but nothing works. I can see that the
TemplateField contains 0 controls. If I try to force a manual .DataBind on
the GridView, I lose all the chosen/typed user responses as the UserControls
are recreated as their viewstate is not preserved. I also need to be able to
pass in values to the UserControls as they will be used for Select
parameters. Again, the only place I can do this (I assume), on a per row
basis, is the GridView's RowDataBound event. I am confused as to which
events to use both in the base and UserControls to do all the databinding,
etc....

SYNOPSIS:

Can anyone please help me on this? In essence, what I am trying to do
is retrieve values from a UserControl which is dynamically added to a
TemplateField in a GridView in the RowDataBound event. Any and all of your
help is greatly appreciated.
Regards,

Giovanni P.
May 8 '06 #1
1 3025
If the final GridView structure is decided upon based on the data that is
bound to the control then you should databind the GridView during or before
the page initialization (page.init)
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Giovanni" wrote:
Dear Friends/Gurus,

I have exhausted myself and have yet no solution to the following:

I have an ASP.NET 2.0 Survey type application. On a page, I have placed
a GridView which is bound to an SQLDataSource. The GridView contains 3
columns: QuestionNumber, QuestionText, and a TemplateField.

Each row in the GridView represents a question in a table (Ex.: "18.
Satisfaction Level").

BACKGROUND:

On a per question (row) basis, a user may choose a response using either
a DropDownList, a TextBox, RadioButtonList, etc. For example, a Yes/No type
question displays a DropDownList while a comment type question would displays
a TextBox. I have designed a UserControl for each different type of input.
For example: a DropDownList, an SQLDataSource, a RequiredValidator, Labels,
and formatting are encapsulated in a UserControl called DDLUC. I have also
added a ResponseChosen property which returns the SelectedItem.Text value of
the DropDownList control. The situation is such that each question may be of
a different type and thus requires a different input control. Some of these
controls are they themselves bound to an SQLDataSource which retrieves their
possible values. For example, my DDLUC control must be populated with
possible answers (Excellent, Good, Bad, Poor).

In the main page, I use the GridView's RowDataBound event, (fired once
for each row in my datasource). In this event, I obtain a QuestionTypeID
field. Based on this value (Integer), I use the ".LoadControl" method to
dynamically add one of my custom UserControls to the TemplateField column;
which up until now has been empty. Each Question may be of a different type.
So far, all this works well. Each question is displayed along with its
correct input type UserControl (Ex.: Question 1 has a DropDownList
UserControl, while Question 2 has a Textbox UserControl for comments, etc...)
The problem is as follows:

Once I click on a "Save Responses" button, I cannot seem to iterate
through the Rows in the GridView and get the values of the UserControl stored
in the TemplateField (which now contains the response the user

has chosen/typed). The problem (I am assuming) is that all controls that
are dynamically added using the .LoadControl method must be recreated
everytime. Unfortunately, the RowDataBound event only fires once

when the Page_Load or Page_PreRender event fires which is not the case when
I click on the "Save Responses" button. I have tried to use the .FindControl
of the Row, and the Cells, but nothing works. I can see that the
TemplateField contains 0 controls. If I try to force a manual .DataBind on
the GridView, I lose all the chosen/typed user responses as the UserControls
are recreated as their viewstate is not preserved. I also need to be able to
pass in values to the UserControls as they will be used for Select
parameters. Again, the only place I can do this (I assume), on a per row
basis, is the GridView's RowDataBound event. I am confused as to which
events to use both in the base and UserControls to do all the databinding,
etc....

SYNOPSIS:

Can anyone please help me on this? In essence, what I am trying to do
is retrieve values from a UserControl which is dynamically added to a
TemplateField in a GridView in the RowDataBound event. Any and all of your
help is greatly appreciated.
Regards,

Giovanni P.

May 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by JAWS | last post: by
reply views Thread by cheloman12 | last post: by
1 post views Thread by cheloman12 | last post: by
6 posts views Thread by Kevin Attard | last post: by
1 post views Thread by Dinu | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.