473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: onearth | last post by:
Hello I already managed to add a dynamic DetailView under a row of Gridview if a user presses the row with the mouse... Is it possible to add under it a table(or even better load a simple...
0
by: s.gregory | last post by:
My page layout is like this: Page >DataList (databound using ObjectDataSource1 contained in page) >>Items >>>UserControl >>>>GridView (databound using another ObjectDataSource2 contained in...
1
by: JAWS | last post by:
I want to make an ASP.NET user control with only a gridview on it. My usercontrol must behave the same as a normal gridview, with some extra properties and methods (for security purposes, such...
7
by: OJ | last post by:
Hi, I have a WebUserCOntrol with a GridView and I am attempting to set the DataSource of the Gridview in the User control from the parent ASPX page. I have the following property in the user...
0
by: cheloman12 | last post by:
Hi, I’m implementing SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance avoiding sending viewstate of pages to browser. The code I...
1
by: cheloman12 | last post by:
Hi, I posted this problem some days ago. I repeat it because it was no answered. If description is confused, please make me know and i'll try to explain the case better. Thanks a lot, Marcelo...
6
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
1
by: syl | last post by:
Hello I am trying to create a "easy to use" gridview with filters (dropdownlist in headers), sorting...and much more.... In order to do that, i prefer to use a objectdatasource (ODS) beacause...
1
by: Dinu | last post by:
hi i am having a gridview in aspx page and a usercontrol in master page of that page now what i want is selected row text to be displayed in usercontrol i wrote code as follows: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.