• 0

Best way to do a survey page in ASP.NET


Question

Hey guys.

I'm currently at a project that includes giving the chance to create a survey online, w/ the questions, answers and its types.

What currently happens is the following: I coded it so that the user is able to add a question, pressing a button, and in that event ,it would normally generate a Label, a Textbox, and a Dropdownlist (or a Listbox, deppending on whether is a simple or multiple answer).

He can generate the first one, but that's about it. When I pess the button again, he just gives me one of each control, and doesn't update the table count at all (a control of type Table is what i'm using currently to store and show these controls).

I've already seen the Postback possibility (in the controls, and put the Page.isNotPostback in the Page_Load event of the page), but i can't put it to work. Can you help me?

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Can you post your code so I can see what you're trying to do? It doesn't sound very difficult to do at all, and should be pretty simple as long as everything is saved in a database somewhere. If you're adding the controls to a panel, you can do a Panel.Controls.Clear() on them to clear out all of the controls first, before the new ones are added (or trying to be added).

Link to comment
Share on other sites

  • 0

Here it is:

        for (int i = 1; i <= numcontrolos; i += 1)
        {
// Criar os controlos,                
            TextBox textBox = new TextBox();
            LinkButton linkButton = new LinkButton();
            Label lblQuestao = new Label();

// Caracterizar os controlos,                
            textBox.ID = "txtQuestao" + i.ToString();
            textBox.Width = System.Web.UI.WebControls.Unit.Pixel(260);
            textBox.Height = System.Web.UI.WebControls.Unit.Pixel(38);
            textBox.TextMode = TextBoxMode.MultiLine;


            linkButton.ID = "linkButton" + i.ToString();
            linkButton.Text = "?";

            lblQuestao.ID = "lblQuestao" + i.ToString();
            lblQuestao.ForeColor = Color.FromName("#CC0000");
            lblQuestao.Text = "Introduza uma resposta";            


// Atribuir aos controlos a c?lula respectiva e adicionar as c?lulas ? linha,
            celula1.Controls.Add(textBox);
            linha.Cells.Add(celula1);

            celula2.Controls.Add(linkButton);
            linha.Cells.Add(celula2);

            celula3.Controls.Add(lblQuestao);
            linha.Cells.Add(celula3);

            // Adicionar a linha,
            tabelaInserirQuestionarios.Rows.Add(linha);

// Limpar a linha e os controlos associados ?s celulas.
            celula1.Controls.Clear();
            celula2.Controls.Clear();
            celula3.Controls.Clear();
            linha.Controls.Clear();
        }

Link to comment
Share on other sites

  • 0

Hmmm, having a hard time reading your comments, but the code is fairly clear. Have you set any break points on the button_click event to see what's happening? Compare what's happening to what you "expect" to happen. You might be running into a viewstate issue.

Link to comment
Share on other sites

  • 0

Sorry about the comments, but they just describe what happens. From 1st to 5th:

-Create the controls

-Featuring the controls

-Put the controls in their respective cell and add those cells to the line

-Add the line

-"Cleaning" the line ands the controls associated with its cells

About the breakpoint and that event, he does all the above, but restarts as soon as i press the button again. I've already set the ViewState property to true. I've tried to do do it also w/ UpdatePanel (AJAX).

The code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            ViewState["aTabela"] = tabelaInserirQuestionarios;
        else tabelaInserirQuestionarios = (Table)ViewState["aTabela"];
    }

    protected void [Serializable()] criacaoComum (object sender, EventArgs e)
    {
             int i = tabelaInserirQuestionarios.Rows.Count;

            TableRow linha = new TableRow();

            TableCell celula1 = new TableCell();
            // Criar e caracterizar o 1º controlo (e a sua célula respectiva),
            Label _label = new Label();
            _label.ID = "label" + i.ToString();
            _label.Text = "Questão " + i.ToString() + ":";
            celula1.Controls.Add(_label);

            TableCell celula2 = new TableCell();
            // Criar e caracterizar o 2º controlo (e a sua célula respectiva),
            TextBox _textBox = new TextBox();
            _textBox.ID = "txtQuestao" + i.ToString();
            celula2.Controls.Add(_textBox);

            TableCell celula3 = new TableCell();

            if (((Button)sender).ID == "bCriarSimples")
            {
                // Criar e caracterizar o 3º controlo (e a sua célula respectiva),
                DropDownList _dropdown = new DropDownList();
                _dropdown.ID = "dropdown" + i.ToString();
                _dropdown.DataSource = ODSTipos;
                _dropdown.DataTextField = "descricao";
                _dropdown.DataValueField = "id_tipo";
                _dropdown.AutoPostBack = true;
                _dropdown.DataBind();
                celula3.Controls.Add(_dropdown);
            }
            else
                if (((Button)sender).ID == "bCriarMatriz")
                {

                    // Criar e caracterizar o 4º controlo (e a sua célula respectiva),
                    ListBox _listbox = new ListBox();
                    _listbox.ID = "listbox" + i.ToString();
                    celula3.Controls.Add(_listbox);
                }

            //Adicionar as células à linha,
            linha.Cells.Add(celula1);
            linha.Cells.Add(celula2);
            linha.Cells.Add(celula3);

            // Adicionar a linha.
            tabelaInserirQuestionarios.Rows.Add(linha);
            ViewState["aTabela"] = tabelaInserirQuestionarios;
    }

The comments remain the same, the onClick event of the two buttons that i have in the page call this last function called criacaoComum.

Is there other way to do this?

Link to comment
Share on other sites

  • 0

I've done something similar on another project at a previous job, but it was not something that was controlled by the user directly, the controls were created based on what was in the database. So if i'm understanding what you're trying to do, you need a way to keep track of the information that the user is adding to the form. You could try creating an object for what's being added, then add that object to a collection and put the collection in the session. This way when the page posts back, you can check to see if the user added a new control, or if it's the same one, loop through the objects in the collection (this would also allow you to keep track of what was selected in the other controls as well), and still be able to create new controls in the next row in the table.

Link to comment
Share on other sites

  • 0

So, if i'm understanding, something like an object that could hold all the controls added, and when the user finishes, read all the controls ion the object, and that way read all its values and insert them in the db?

Link to comment
Share on other sites

  • 0

Yes, that's correct. You have a lot of choices on how to do this, and my approach may not be the "best", just an alternative. So for this, say you have an object named SurveyQuestion, and in that object, you have the following properties and members: Question (string), Answer (string), Choices (List<string> or Dictionary<string, string>), ChoiceType (whether it's a drop down list or list box). Then you have another object named something like SurveyQuestionCollection which basically holds every survey question that has been added. You should be able to store that collection in the session, or cache it in the database if necessary. This way you could just loop through each survey question in the collection when you want to display it (something like foreach(SurveyQuestion sq in SurveyQuestionCollection) { /* Do Something */ }).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.