Domanda

I m facing problem in getting data from asp.net Table. It displays table correctly, but when I try to access the table from code, I can't find the table. Here is my code. Tell me if any other way of doing same thing.

private void PopulateSurvey()
    {
      btnSubmit.Enabled = true;
        List<squestion> questions = (from p in context.squestions
                                     where p.surveyid == surveyid
                                     select p).ToList();
        Table tbl = new Table();
        tbl.Attributes.Add("id", "datatable");
        tbl.Width = Unit.Percentage(100);
        TableRow tr;
        TableCell tc;
        TableCell counter;
        TextBox txt;
        CheckBoxList cbk;
        DropDownList ddl;
        RadioButtonList ddl1;






        int i = 1;

        foreach (squestion q in questions)
        {
            tr = new TableRow();

            counter = new TableCell();

            counter.Text = i.ToString() + " ";

            tc = new TableCell();
            tc.Width = Unit.Percentage(25);
            tc.Text = q.qtext;
            tc.Attributes.Add("id", q.qid.ToString());

            i++;
           tr.Cells.Add(counter);
            tr.Cells.Add(tc);

            tc = new TableCell();



            if (q.qtype.ToLower() == "radiobutton")
            {

                ddl1 = new RadioButtonList();
                ddl1.ID = "ddl_" + q.surveyid;
                ddl1.Width = Unit.Percentage(41);

                List<sanswer> sanswers = (from z in context.sanswers
                                          where z.qid == q.qid
                                          select z).ToList();
                foreach (sanswer ans in sanswers)
                {
                    ddl1.Items.Add(ans.anstext);
                }


                tc.Controls.Add(ddl1);
            }
            else if (q.qtype.ToLower() == "checkbox")
            {

                cbk = new CheckBoxList();
                cbk.ID = "ddl_" + q.surveyid;
                cbk.Width = Unit.Percentage(41);

                List<sanswer> sanswers = (from z in context.sanswers
                                          where z.qid == q.qid
                                          select z).ToList();
                foreach (sanswer ans in sanswers)
                {
                    cbk.Items.Add(ans.anstext);
                }


                tc.Controls.Add(cbk);
            }
            else if (q.qtype.ToLower() == "singlelinetextbox")
            {
                txt = new TextBox();
                txt.ID = "txt_" + q.surveyid;
                txt.Width = Unit.Percentage(40);
                tc.Controls.Add(txt);
            }
            else if (q.qtype.ToLower() == "multilinetextbox")
            {
                txt = new TextBox();
                txt.ID = "txt_" + q.surveyid;
                txt.TextMode = TextBoxMode.MultiLine;
                txt.Width = Unit.Percentage(40);
                tc.Controls.Add(txt);
            }

            else if (q.qtype.ToLower() == "singleselect")
            {
                ddl = new DropDownList();
                ddl.ID = "ddl_" + q.surveyid;
                ddl.Width = Unit.Percentage(41);

                List<sanswer> sanswers = (from z in context.sanswers
                                          where z.qid == q.qid
                                          select z).ToList();



                foreach (sanswer ans in sanswers)
                {
                    ddl.Items.Add(ans.anstext);
                }


                tc.Controls.Add(ddl);
            }

            tc.Width = Unit.Percentage(80);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
        }
        Panel1.Controls.Add(tbl);

    }

calling the the function to get response

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        List<suseranswer> surveyanswers = GetSurveyReponse();
        foreach (suseranswer sres in surveyanswers)
        {
           context.AddTosuseranswers(sres);
           Response.Write(sres.answer);

        }


         context.SaveChanges();
        }

Here is the function which is getting data from table. it cant find parent control (Panel1) but cant find table.

   private List<suseranswer> GetSurveyReponse()
    {
        List<suseranswer> response = new List<suseranswer>();

        foreach (Control ctr in Panel1.Controls)
        {

        if (ctr is Table)
            {
                Table tbl = ctr as Table;

                foreach (TableRow tr in tbl.Rows)
                {
                    suseranswer sres = new suseranswer();
                    sres.suserid = "anyone";
                    sres.qid = tr.Cells[0].Attributes["ID"];

                    TableCell tc = tr.Cells[1];
                    foreach (Control ctrc in tc.Controls)
                    {
                        if (ctrc is TextBox)
                        {
                            sres.answer = (ctrc as TextBox).Text.Trim();
                        }
                        else if (ctrc is DropDownList)
                        {
                            sres.answer = (ctrc as DropDownList).SelectedValue;
                        }
                        else if (ctrc is RadioButtonList)
                        {
                            sres.answer = (ctrc as DropDownList).SelectedValue;
                        }
                        else if (ctrc is CheckBoxList)
                        {

                            foreach (ListItem item in (ctrc as CheckBoxList).Items)
                            {
                                if (item.Selected)
                                {
                                    string selectedValue = item.Value.ToString();
                                    sres.answer = selectedValue + "%";
                                }
                            }

                        }
                    }
                    response.Add(sres);

                }

            }
        }

        return response;
    }

here is code of my aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="survey.aspx.cs" Inherits="csdatabaseModel.survey" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
  <title></title>
 </head>
 <body>
  <form id="form1" runat="server">
   <div style="vertical-align: middle; line-height: 15px; text-align: left; text-indent: 5px; background-color: #CCFFFF; border: 1px dashed #CCCCCC; padding: 10px; margin: 10px; table-layout: auto">

    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    <asp:Button ID="btnSubmit" runat="server" Text="save" OnClick="btnSubmit_Click" />
</div>
</form>
 </body>
 </html>

Again data is being displayed as i want.

È stato utile?

Soluzione

When you create the table on code behind, this data are not saved anywhere.

You may see them on the page, but on post back, that data never move back on server and on code behind.

So possible solutions.

  • You always create the survey and on Init().
  • You add inside the survey input controls that make post the results and use that posted data
  • You fully save what you render, on viewstate, and one other functions of yours is take care to re-render the saved viewstate data.
  • You do not make the survey dynamically on code behind, but direct on page.

Altri suggerimenti

Save the table in Session Var This is the solution for this case

private void PopulateSurvey()
{
  btnSubmit.Enabled = true;
    List<squestion> questions = (from p in context.squestions
                                 where p.surveyid == surveyid
                                 select p).ToList();
    Table tbl = new Table();
    tbl.Attributes.Add("id", "datatable");
    tbl.Width = Unit.Percentage(100);
    TableRow tr;
    TableCell tc;
    TableCell counter;
    TextBox txt;
    CheckBoxList cbk;
    DropDownList ddl;
    RadioButtonList ddl1;
    int i = 1;
    foreach (squestion q in questions)
    {
        tr = new TableRow();
        counter = new TableCell();
        counter.Text = i.ToString() + " ";
        tc = new TableCell();
        tc.Width = Unit.Percentage(25);
        tc.Text = q.qtext;
        tc.Attributes.Add("id", q.qid.ToString());
        i++;
       tr.Cells.Add(counter);
        tr.Cells.Add(tc);
        tc = new TableCell();
        if (q.qtype.ToLower() == "radiobutton")
        {

            ddl1 = new RadioButtonList();
            ddl1.ID = "ddl_" + q.surveyid;
            ddl1.Width = Unit.Percentage(41);

            List<sanswer> sanswers = (from z in context.sanswers
                                      where z.qid == q.qid
                                      select z).ToList();
            foreach (sanswer ans in sanswers)
            {
                ddl1.Items.Add(ans.anstext);
            }
            tc.Controls.Add(ddl1);
        }
        else if (q.qtype.ToLower() == "checkbox")
        {

            cbk = new CheckBoxList();
            cbk.ID = "ddl_" + q.surveyid;
            cbk.Width = Unit.Percentage(41);

            List<sanswer> sanswers = (from z in context.sanswers
                                      where z.qid == q.qid
                                      select z).ToList();
            foreach (sanswer ans in sanswers)
            {
                cbk.Items.Add(ans.anstext);
            }
            tc.Controls.Add(cbk);
        }
        else if (q.qtype.ToLower() == "singlelinetextbox")
        {
            txt = new TextBox();
            txt.ID = "txt_" + q.surveyid;
            txt.Width = Unit.Percentage(40);
            tc.Controls.Add(txt);
        }
        else if (q.qtype.ToLower() == "multilinetextbox")
        {
            txt = new TextBox();
            txt.ID = "txt_" + q.surveyid;
            txt.TextMode = TextBoxMode.MultiLine;
            txt.Width = Unit.Percentage(40);
            tc.Controls.Add(txt);
        }

        else if (q.qtype.ToLower() == "singleselect")
        {
            ddl = new DropDownList();
            ddl.ID = "ddl_" + q.surveyid;
            ddl.Width = Unit.Percentage(41);

            List<sanswer> sanswers = (from z in context.sanswers
                                      where z.qid == q.qid
                                      select z).ToList();
            foreach (sanswer ans in sanswers)
            {
                ddl.Items.Add(ans.anstext);
            }
            tc.Controls.Add(ddl);
        }

        tc.Width = Unit.Percentage(80);
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);
    }
    Session["Tabla"] = tbl;
    Panel1.Controls.Add(tbl);
}

Part 2

private List<suseranswer> GetSurveyReponse()
{
    List<suseranswer> response = new List<suseranswer>();

    foreach (Control ctr in Panel1.Controls)
    {

    if (Session["Tabla"] is Table)
        {
            Table tbl = Session["Tabla"] as Table;

            foreach (TableRow tr in tbl.Rows)
            {
                suseranswer sres = new suseranswer();
                sres.suserid = "anyone";
                sres.qid = tr.Cells[0].Attributes["ID"];

                TableCell tc = tr.Cells[1];
                foreach (Control ctrc in tc.Controls)
                {
                    if (ctrc is TextBox)
                    {
                        sres.answer = (ctrc as TextBox).Text.Trim();
                    }
                    else if (ctrc is DropDownList)
                    {
                        sres.answer = (ctrc as DropDownList).SelectedValue;
                    }
                    else if (ctrc is RadioButtonList)
                    {
                        sres.answer = (ctrc as DropDownList).SelectedValue;
                    }
                    else if (ctrc is CheckBoxList)
                    {

                        foreach (ListItem item in (ctrc as CheckBoxList).Items)
                        {
                            if (item.Selected)
                            {
                                string selectedValue = item.Value.ToString();
                                sres.answer = selectedValue + "%";
                            }
                        }

                    }
                }
                response.Add(sres);

            }

        }
    }

    return response;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top