List Controls on Datagrid will not show Selection after Postback

Discussion in 'ASP .Net' started by Joanne, Jul 7, 2005.

  1. Joanne

    Joanne Guest

    Hi,

    I have an "interesting" problem with UserControls in a datagrid and I'm
    desperate for your help as I work alone and have no-one else to ask. It
    is quite complicated but I'll try to keep it to a simple example ...

    FormQuestionAnswer.ascx
    -----------------------
    I have a user Control that displays a question (from a questionnaire)
    and its answer (as previously selected by a user). The question has a
    list of possible answers in a CheckBoxList control. There is also an
    "Other" option allowing the user to specify an answer as text. Note:
    this is supposed to be a read-only control simply for display purposes,
    I am not requiring any user input here.

    QuestionnaireAnswerList.ascx
    ----------------------------
    The parent UserControl contains a datagrid based on a particular
    questionnaire with answers given by a particular user. Each row in the
    datagrid displays an instance of the FormQuestionAnswer.ascx
    UserControl, one for each question in the questionnaire.

    When I first load the page the answers are all selected correctly. Any
    text answers are displayed in labels.

    However if, for any reason, the page is posted back (e.g. the
    questionnaire is changed or the user is changed) then the list controls
    no longer display any selections. Any text answers do still come
    through as labels. Please note: this is NOT a ViewState issue as I am
    loading up new controls afresh with new answers each time and the
    selections are made according to data from my database.

    I have been through the code a hundred times and traced the stack. Each
    time the followng happens: the datagrid is bound, the
    FormQuestionAnswer controls are created, the list controls are
    databound and then last of all the selections are made. Nothing happens
    to the controls after the selections are made (I was looking for a
    subsequent DataBind() that would account for the selections being lost
    but it doesn't happen).

    I cannot find any significant difference in the stack trace when the
    page is posted back which would account for this problem.

    Does anyone have any ideas as to why this might be happening? Any
    insight much appreciated.

    Thanks in advance.

    Joanne
     
    Joanne, Jul 7, 2005
    #1
    1. Advertising

  2. Hi Joanne, sorry i'm a bit distracted by the news... but.. make sure all your
    controls are loaded before the page loads viewstate and post data. This is a
    particular issue if you are dynamically loading controls into the page. e.g
    create your controls in page.init event HTH jd

    "Joanne" wrote:

    > Hi,
    >
    > I have an "interesting" problem with UserControls in a datagrid and I'm
    > desperate for your help as I work alone and have no-one else to ask. It
    > is quite complicated but I'll try to keep it to a simple example ...
    >
    > FormQuestionAnswer.ascx
    > -----------------------
    > I have a user Control that displays a question (from a questionnaire)
    > and its answer (as previously selected by a user). The question has a
    > list of possible answers in a CheckBoxList control. There is also an
    > "Other" option allowing the user to specify an answer as text. Note:
    > this is supposed to be a read-only control simply for display purposes,
    > I am not requiring any user input here.
    >
    > QuestionnaireAnswerList.ascx
    > ----------------------------
    > The parent UserControl contains a datagrid based on a particular
    > questionnaire with answers given by a particular user. Each row in the
    > datagrid displays an instance of the FormQuestionAnswer.ascx
    > UserControl, one for each question in the questionnaire.
    >
    > When I first load the page the answers are all selected correctly. Any
    > text answers are displayed in labels.
    >
    > However if, for any reason, the page is posted back (e.g. the
    > questionnaire is changed or the user is changed) then the list controls
    > no longer display any selections. Any text answers do still come
    > through as labels. Please note: this is NOT a ViewState issue as I am
    > loading up new controls afresh with new answers each time and the
    > selections are made according to data from my database.
    >
    > I have been through the code a hundred times and traced the stack. Each
    > time the followng happens: the datagrid is bound, the
    > FormQuestionAnswer controls are created, the list controls are
    > databound and then last of all the selections are made. Nothing happens
    > to the controls after the selections are made (I was looking for a
    > subsequent DataBind() that would account for the selections being lost
    > but it doesn't happen).
    >
    > I cannot find any significant difference in the stack trace when the
    > page is posted back which would account for this problem.
    >
    > Does anyone have any ideas as to why this might be happening? Any
    > insight much appreciated.
    >
    > Thanks in advance.
    >
    > Joanne
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 7, 2005
    #2
    1. Advertising

  3. Joanne

    Joanne Guest

    Hi,

    Thanks for the reply. I'm not quite sure if you are saying it is good
    or bad to create controls in the page.init event. In any case that is
    not what I've been doing so far.

    In the FormQuestionAnswer.ascx usercontrol, all the standard controls I
    could possibly need are already declared and visible on the page. At
    page_load I hide them all, then find out what sort of answer is
    required and make visible the appropriate answer control (e.g. the
    CheckBoxList), then select the appropriate answers ( e.g. select some
    of the checkboxes). I have set public properties QuestionID and UserID
    for the UserControl to determine the question and answer required.

    The UserControl gets created when the datagrid in the parent page is
    databound. The html for the datagrid looks something like this ...

    QuestionnaireAnswerList.ascx
    ----------------------------
    <asp:datagrid id="dg" runat="server" >
    <Columns>
    <asp:TemplateColumn HeaderText="">
    <ItemTemplate>
    <uc1:FormQuestionAnswer id="ctlFormQuestionAnswer1" runat="server"
    UserID='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem,
    "UserID")) %>' QuestionID='<%#
    Convert.ToInt32(DataBinder.Eval(Container.DataItem, "QuestionID")) %>'>
    </uc1:FormQuestionAnswer>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    In the FormQuestionAnswer.ascx usercontrol when the public property,
    QuestionID, is set, the call to make the appropriate selections is
    made. The c# code looks something like this ...

    FormQuestionAnswer.ascx
    -----------------------
    //passed in
    public int QuestionID
    {
    get
    {
    return mintQuestionID;
    }
    set
    {
    mintQuestionID = value;
    //Whenever QuestionID changed, set up new question
    SetUpNewQuestion();
    }
    }

    public void SetUpNewQuestion()
    {
    //First set all the controls to hide
    HideQuestionControls();

    //What Answer type do we want?
    switch(CurrentFormQuestion.AnswerTypeID)
    {
    case lkAnswerType.gintCurrencyID:
    {
    SetUpCurrencyAnswer(map);
    break;
    }
    // more cases here ...
    case lkAnswerType.gintListID:
    {
    SetUpCheckBoxListAnswer();
    break;
    }
    }

    ---***---
    SetUpCheckBoxListAnswer() binds the possible answers to the
    checkBoxList and makes it visible. The answers given by the user are
    then selected.

    Note: The Page_Load() method of FormQuestionAnswer.ascx also calls
    SetUpNewQuestion() the first time it is run.

    FormQuestionAnswer.ascx
    -----------------------
    private void Page_Load(object sender, System.EventArgs e)
    {
    //Clear the SessionObject vars
    if(!IsPostBack)
    {
    //Initially, set up the question
    //next time, wait for event to be fired
    SetUpNewQuestion();
    }
    }

    ---***---
    This means that SetUpNewQuestion() gets called at least twice the first
    time, but when I took it away, it didn't work. My investigations also
    showed that if I removed the if(!IsPostBack) condition and allowed the
    SetUpNewQuestion() event to be called again on postback, it still
    resulted in the same problem - list items not being selected.

    Driving me mad, but thanks very much for any help!
    Joanne
     
    Joanne, Jul 7, 2005
    #3
    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. thegame
    Replies:
    1
    Views:
    960
    =?Utf-8?B?ZGlyazQx?=
    Feb 28, 2005
  2. Carlos
    Replies:
    10
    Views:
    706
  3. BizEd
    Replies:
    1
    Views:
    872
    Aamir Ghanchi
    Oct 2, 2007
  4. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    291
    curiousity
    Nov 21, 2003
  5. Hide/Show Selection List

    , May 11, 2006, in forum: Javascript
    Replies:
    4
    Views:
    172
    Evertjan.
    May 12, 2006
Loading...

Share This Page