0

I hope this question hasn't been asked before in this manner, if it has, I sadly can't find it!

I'm currently trying to create a User Control in which I want a client to select a questionnaire from a dropdownmenu which is dynamically populated on page_load like this:

      DiscoverConnectData(); //gets the items from a database
      var formItems = 0;
      using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
        {
          var forms = (from f in osContext.CreateQuery("form") select f);

          foreach (var form in forms)
          {
              var name = formulier.GetAttributeValue<string>("name");
              var formId = formulier.GetAttributeValue<Guid>("formid");
              DropDownList_RP.Items.Insert(formItems, new ListItem(naam, formId.ToString()));
              formItems++;
          }
      }

I've also tried putting the above code inside an if (!Page.IsPostBack), however this didn't fix my problem... Okay, moving on. When the client has selected a questionnaire and hit a button, I have a piece of code that runs through all the questions and answers related to that specific questionnaire in a for each loop and creates Labels and RadioButtonLists for each question. First it loads the questions and answers related to the selected formId:

        var curQuestionName = string.Empty;
        var lblId = 0;
        _formGuid = new Guid(DropDownList_RP.SelectedValue);

        DiscoverConnectData();

        using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
        {
            _answerList =
                (from a in osContext.CreateQuery("answer")
                 join v in osContext.CreateQuery("question")
                 on a["question"] equals v["questionid"]
                 where ((EntityReference)v["form"]).Id == _formGuid
                 select new AnswerClass
                 {
                     AnswerQuestionId = v.GetAttributeValue<Guid>("questionid"),
                     QuestionName = v.GetAttributeValue<string>("name"),
                     Name = a.GetAttributeValue<string>("ansname"),
                     Score = a.GetAttributeValue<int>("score")
                 }).ToList();
        }

After picking up this data and adding it to a list, I create the Questionnaire items:

    foreach (var ant in _answerList)
        {
            if (ant.QuestionName != curQuestionName)
            {
                var vLabel = new Label();
                var qid = "Q" + lblId;
                vLabel.ID = qid;
                vLabel.Text = ant.QuestionName;
                Questionnaire.Controls.Add(vLabel);

                _aRadioList = new RadioButtonList();
                var rblId = "list" + lblId;
                _aRadioList.ID = rblId;
                Questionnaire.Controls.Add(_aRadioList);
                Questionnaire.Controls.Add(new LiteralControl("<br />"));

                curQuestionName = ant.QuestionName;
                lblId++;
            }
            _aRadioList.Items.Add(new ListItem((" " + ant.Name), ant.Score.ToString()));
        }

This all works fine and I'm getting a list with questions and answers neatly put underneath each other. So far so good!

However, the big problem I'm having is the losing of data on page postback. Whenever the user selects certain answers and I then want to show them a score, or do anything else - the page reloads and all my dynamically added controls are lost. Now I have fixed that issue by putting the relevant functionality above into a function and calling that again when I press the button to show a score, and this recreates the questions and answers, but I lose all the selectedvalues. I don't specifically even need to keep the controls on the page or anything, I just need to keep the selectedvalues in order to do stuff with them!

I can't create the controls on page_load or page_init because I need user input to know exactly which questionnaire to pull from the database. I can't use "hard-coded" values for the dropdownlist items either, because that's not the functionality we're aiming for. We need to pull it all from the database - which I can't access directly, I need to use the DiscoverConnectData() function.

Please, if anyone has any ideas in order to get the functionality I want (also if it means exploring a whole new 'avenue'), please let me know!

Brookabum
  • 1
  • 1
  • You need to use a session. http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net – J... Nov 13 '15 at 11:30
  • http://stackoverflow.com/q/1058568/327083 – J... Nov 13 '15 at 11:31
  • http://stackoverflow.com/q/1051305/327083 – J... Nov 13 '15 at 11:31
  • http://stackoverflow.com/q/1106776/327083 – J... Nov 13 '15 at 11:32
  • Oh I forgot to add, I have tried using an Ajax UpdatePanel but of course this doesn't negate the page_load event as far as I know. It's all in the retaining of the selected values! – Brookabum Nov 13 '15 at 11:42
  • Hey there, thanks for your answer! I have looked at using a session, but I can't figure out how that will retain my selected values. Any event handlers (.SelectedIndexChanged for example) doesn't fire until I hit a button, which causes a page reload. – Brookabum Nov 13 '15 at 11:43
  • It's not automagic - *you* have to store the information in a session variable and then retrieve it later. As for how it works, that's another topic altogether. Also read : https://msdn.microsoft.com/en-us/library/ms178581.aspx – J... Nov 13 '15 at 12:03
  • Alternatively, consider a redesign to something more modern like REST, if possible. http://stackoverflow.com/q/17967369/327083 – J... Nov 13 '15 at 12:41
  • Hey J, I know what you meant when you said I needed to store a session variable. And that works fine when I create them, for example I can save a list of the radiobuttons when I create them and retrieve them from the session later. The problem is that I need to save the selected values, which I can't do with a session because what the user selects is wiped as soon as they hit the submit button. – Brookabum Nov 13 '15 at 12:41
  • http://stackoverflow.com/q/23419011/327083 – J... Nov 13 '15 at 12:41
  • That's why you would *store* the selected values in a session variable - before they are wiped - so you can retrieve them later. There's a lot of reading here. I suggest you give some serious effort to reading it. The answers are in there. – J... Nov 13 '15 at 12:45
  • Hey J, thanks for helping me. I think we're misunderstanding each other, so if you could answer one more thing for me that'd be great. Where do you suggest I save the selected values? At what point? I can't save them as the client clicks them. I can't save them on a button click event either. I can't use any event whatsoever, because this reloads the page and thus wipes the selected values. If I'm being an idiot I'm sorry, I just really don't see it! – Brookabum Nov 13 '15 at 13:08
  • 1
    Hi Brookabum, if I may suggest just use a webmethod and jquery ajax , create a webmethod that returns stuff like question/answer etc then append it to your DOM. No pageload, values are retained. In my case I've created a webmethod that returns arraylist then in my frontend I loop and append those items in dropdownbox and table.. Again this is just giving you an idea in another way around. You're actually done with your codebehind. – Francis Saul Nov 13 '15 at 13:48

0 Answers0