GridView UserControl Problems

Discussion in 'ASP .Net' started by =?Utf-8?B?R2lvdmFubmk=?=, May 8, 2006.

  1. 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.
    =?Utf-8?B?R2lvdmFubmk=?=, May 8, 2006
    #1
    1. Advertising

  2. 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.
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, May 8, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huobazi
    Replies:
    1
    Views:
    669
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    400
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,529
  4. Phl
    Replies:
    2
    Views:
    511
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  5. Fabiano
    Replies:
    2
    Views:
    583
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
Loading...

Share This Page