0

Hello all I'm working on a little project where I'm adding controls to a page based on a SQL table of questions, this table will grow overtime. I just wanted to share the code and see if there was any better way or if any of the experts could chime in and give me any insight on future problems. Here is the code:

       protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            SqlParameter[] paramz = new SqlParameter[1];
            paramz[0] = new SqlParameter("@c_id", 1);

            dt = SqlHelper.ExecuteDataTable(ConfigurationManager.ConnectionStrings["sql"].ToString(), CommandType.StoredProcedure, "get_Questions", paramz);
            clinicName.Text = "<b>" + dt.Rows[0]["Clinic Name"].ToString();

            for(int row = 0; row <= dt.Rows.Count; row++)
            {
                if (row == dt.Rows.Count) //if we're on the last question put a break for spacing(this could be fixed with styling)
                {
                    Literal alit = new Literal();
                    alit.Text = "<br/>";
                    questionsPanel.Controls.Add(alit);
                }
                else
                {
                    addQuestion(dt.Rows[row], row);
                }
            }

        }
        catch (Exception err)
        {
            Response.Write(err.Message);
        }

    }

    private void addQuestion(DataRow row, int i)
    {
        Label lbl = new Label();
        lbl.Text = row["question"].ToString();
        questionsPanel.Controls.Add(lbl);

        Literal lit = new Literal();
        lit.Text = "<br/>";
        questionsPanel.Controls.Add(lit);

        TextBox txt = new TextBox();
        txt.ID = "txt" + i.ToString();
        questionsPanel.Controls.Add(txt);

        Literal lit2 = new Literal();
        lit2.Text = "<br/>";
        questionsPanel.Controls.Add(lit2);

    }

2 Answers 2

3

Use a Repeater control:

ASPX Code:

<asp:Repeater id="repData" runat="server">
    <ItemTemplate>
        <asp:Label id="lblQuestion" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "question") %>' />
        <br />
        <asp:TextBox id="lblAnswer" runat="server" />
    </ItemTemplate>
    <FooterTemplate>
        <br />
    </FooterTemplate>
</asp:Repeater>

Code behind:

// Populate repeater
SqlParameter[] paramz = new SqlParameter[1];
paramz[0] = new SqlParameter("@c_id", 1);
dt = SqlHelper.ExecuteDataTable(ConfigurationManager.ConnectionStrings["sql"].ToString(), CommandType.StoredProcedure, "get_Questions", paramz);

repData.DataSource = dt;
repData.DataBind();
Sign up to request clarification or add additional context in comments.

Comments

1

If the controls either use or contribute to ViewState, then you must ensure that the same controls are added to ViewState in the same order, on every post back. The order in which objects are added to ViewState depends on the order of the controls in the control tree.

Comments

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.