0

I am trying to get an application to allow a user to select something for a number rows and then do something on the server side once the user submits the form. The problem I'm having is that if I reload the table, I just get the default values back and if I don't, the table is empty. Here is the code:

    <asp:Table ID="tbl" runat="server">
        <asp:TableRow>
            <asp:TableHeaderCell>Question</asp:TableHeaderCell>
            <asp:TableHeaderCell>Answer</asp:TableHeaderCell>
        </asp:TableRow>
    </asp:Table>

c#:

    protected System.Web.UI.WebControls.Table tbl1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            init_tbl();
        }
    }
    protected void init_tbl()
    {
        tbl1.BorderWidth = 1;
        TableRow tr = new TableRow();
        tc = new TableCell();
        tc.Text = "text";
        tc.BorderWidth = 1;
        tr.Cells.Add(tc);

    ddl = new DropDownList();
    ddl.ID = "r" + index;
    ddl.Attributes["runat"] = "server";

    ListItem item;
    for (int i = 1; i <= 10; i++)
    {
         item = new ListItem();
         item.Text = i.ToString();
         item.Value = i.ToString();
         if (i.ToString().Equals(r.Trim()))
         {
             item.Selected = true;
          }
          ddl.Items.Add(item);
      }      
      list.Add(ddl);
      tc.Controls.Add(ddl);

      tc.ID = "tblr" + index;
      tr.Cells.Add(tc);

      tbl1.Rows.Add(tr);
    }
1
  • 1
    Why don't your use a GridView or DataGrid? That would be a better solution for your prblm Commented Jun 15, 2011 at 21:16

3 Answers 3

1

your problem is with the convoluted way asp.net deals with dynamic controls, you need to create the dynamic control on page init, before the view state is set so state of the controls is maintained on the post, see this article http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx.

You should definatley look at the gridview or at the very least one of the repeater controls.

Sign up to request clarification or add additional context in comments.

2 Comments

the problem is even when i try to print tbl.row.count in init i only get the values hardcoded in and not the dynamically created ones so it always returns 1.
in init the control state hasn't been updated from the viewstate, if you do the tbl.row.count in the "save button" click event the control state will be correctly set.
0

I would just make a list of questions and DataBind them to a repeater/gridview/datagrid (as @Cybernate said), then add an Event method to the OnItemDataBound of the databinder.

In the ItemDataBound event I would get a list of answers for each question DataItem and add them to a DropDownList like you were doing above.

When the user fills out all of the answers you just need to go through the Request.Form array and find all the answers that will be passed back.

Comments

0

You stored the Table in the ViewState and Use it as you want.

1 Comment

I keep getting a serialization exception when i try to add the table to the viewstate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.