Ok I simply don't know how I am going to do this here is what I need:
I have exercises in my db (question id,question text field,close/open(bool) field,right answer field and 3 wrong answer fields which are null unless it is a closed question)
What I want to do is to load all the exercises from my database to my form I dunno how many I have and it will change so I need it to b dynamic.

How do I load them to my form so I can refer to them later on?

What I need when I load them to my form is to place the text in a label and then a textbox for an answer (open questions) or 4 possible answers (1 right and 3 wrong) in radio buttons (closed question).
Ofcourse randomized order.

My problem is that if I write a code that loads them by page_load (using html tags)
I won't b able to write a function that will relay to them.
Since if the program loads them on page_load it means they do not exist before it.

I could load the question id from the db to their id/name for using it later when I request the the answers from the client.

Can I do something like declaring an object of the kind of a textbox (not to reinvent but use the existing asp.net object) so when I place it I will b able to refer to it?
Again placing is on page_load.

Hi UtaChan, I completely dont understand your requirement but if you want you can dynamically create any asp.net control using code behind, because every asp.net control is a class and you can create an object and use them directly(like TextBox txt1=new TextBox();).

Yea this is what I need but waht if I need more of this kind like 1 for each item in my list will it b ok if all have the same name? like

foreach(     )
TextBox textbox =new TextBox()
form.controlx.add(textbox)

Is it ok as long as they have different id? since it;s gonna b inside a loop...
(please dun correct my syntax here I scribled something just for example didn;t really try to make it legid)

You can use listview. You can put other controls inside a listview like textboxes and labels.
Just assign a datasource to the listview and display database values in a label or any other control depending on your preference.

In you aspx site make a label and Name it.
For example:

<asp:Label ID="lbl_question" runat="server" Text=""></asp:Label> 
<asp:Label ID="lbl_option1" runat="server" Text=""></asp:Label> 
<asp:Label ID="lbl_option2" runat="server" Text=""></asp:Label> 

Add other stuffs like answers, etc.

Now in you code behind file enter:

protected void lbl_items_SelectedIndexChanged(object sender, EventArgs e)
       {            
            SqlConnection SqlConn = new SqlConnection();
            SqlCommand cmd;
            SqlDataReader dr;

            SqlConn.ConnectionString = ConfigurationManager.AppSettings["SqlConn"].ToString();
            SqlConn.Open();
            cmd = new SqlCommand("SELECT [Questionno],[Question],[Option1],[Option2],[Option3] FROM [tbl_exam] '" + lbl_items.SelectedItem.Value.ToString() + "'", SqlConn);
            dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    lbl_questionno.Text = dr["Questionno"].ToString();
                    lbl_question.Text = dr["Question"].ToString();
                    lbl_option1.Text = dr["Option1"].ToString();
                    ...
                    ...
                }
            }

Above code is to read data from the database i have changed little bit to give you some idea.

Help for my problem if you can Link

Example for creating controls dynamically and accessing them from server side

Code in aspx file:
<form id="form1" runat="server">
    <div>
        <asp:Panel ID="pnldyncontrols" runat="server">
        </asp:Panel>
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Get controls</asp:LinkButton>
    </div>
    </form>

    Code in aspx.cs file


     protected void Page_Load(object sender, EventArgs e)
        {
            //for adding controls dynamically on the page
            for(int i=0;i<30;i++)
            {
                TextBox tb = new TextBox();
                tb.ID = "txtbox"+i;
                tb.Text = "Text box"+i;
                RadioButton rb = new RadioButton();
                rb.ID = "Radio Button"+i;
                Label l1 = new Label();
                l1.ID = "label"+i;
                l1.Text = "Label "+i;
                pnldyncontrols.Controls.Add(tb);
                pnldyncontrols.Controls.Add(rb);
                pnldyncontrols.Controls.Add(l1);
                pnldyncontrols.Controls.Add(new LiteralControl("<br></30>"));
            }


        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            //To access the controls in a page
            foreach (Control ctrl in pnldyncontrols.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox tb = ctrl as TextBox;
                    Response.Write(tb.Text);
                    Response.Write("<br/>");
                }
            }
        }



    Replace for loop with the dataset or datareader loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.