K C
K C

Reputation: 219

Dynamic controls(Textbox) in asp.net

I want to create dynamic text boxes during run time. Suppose im gettng a text from a database as "# is the capital of India" now i want to replace that "#" by text box while it is rendered to user as below

<asp:TextBox runat="server" id = "someid"></asp:TextBox> is the capital of India

Im able to get the textbox and text as combination. However I cannot access the textboxes with the given id and when any event occurs on the page the textboxes are lost as they logically does not exist untill their state is stored.

I also went through the concepts of place holder and Viewstate to store the dynamically created controls and make their id's available for the methods, but as far as I tried I could not meet the textbox and text combination requirement.

Im looping over the entire text recieved from database and checking if there is any"#". Is yes then i want to replace it with a textbox on which i can call methods to take back the value entered in the text box to database and store it.

eg: text is "#" is the capital of India

for (int i = 0; i < que.Length; j++)  //que holds the text
        {
            if (que[i] == '#')
            {
                  //Textbox should be created
            }
            else
            {
                    //the texts should be appended before or after the textbox/textboxes as text demands
            }
        }

On button click I'm passing request to database, which will send me necessary details i.e. question text, options and also saves the current checked value etc.

 protected void BtnLast_Click(object sender, EventArgs e)
{
    CheckBox1.Checked = false;
    CheckBox2.Checked = false;
    CheckBox3.Checked = false;
    CheckBox4.Checked = false;

    QuestionSet q = new QuestionSet();
    StudentB b = new StudentB();

    q = b.GetQuestion(1, 1, qid, 'L', 0, Checked, date);

    qid = Convert.ToInt32(q.Question_Id);
    Checked = q.Checked;

    if (q.Question_Type == 11) //indicates its objective Question
    {
        //ill bind data to checkboxes
    }
    else if (q.Question_Type == 12) // indicate its fill in the blanks question
    {
        for (int j = 0; j < que.Length; j++)
        {
            if (que[j] == '#')
            {
                  count++;
                  string res = "<input type = 'text' runat = 'server' id ='TxtBoxFillUp" + count + "'/>";

                    htm = htm.Append(res);

            }
            else
            {
                    htm = htm.Append(que[j]);
            }
        }

    }

}

Any help will be greatly appreciated, thanks in advance.

Upvotes: 2

Views: 444

Answers (2)

R.C
R.C

Reputation: 10565

Seeing the requirements you have:

1.) You need to use JavaScript. Since the ASP.NET will not recreate controls which are dynamically added. Dynamically added controls need to be recreated after every postback. This is the reason why your TextBoxes are Lost after every postback.

2.) You can write JavaScript code to Hide and show the textboxes for blank texts since at every button click you can call Client side functions using: OnClientClick() property of buttons.

3.) Also to Get the TextBoxes using ID property, add them in Markup( .aspx ) portion itself.

Upvotes: 1

Johnny_D
Johnny_D

Reputation: 4652

Adding control in the way you do it won't create control as asp.net creates it. You do have to create controls as usual .net object.

TextBox myNewTextBox = new TextBox() {};
// Set all initial values to it

And add this cotrol to placeholder or panel, whatever you use. Keep in mind that asp.net page events fire even if you use update panel, so in order to maintain state and events of newly created controls you have take care of creating such controls long before page's Load event fires. Here is my answer to another simialiar question.

Upvotes: 2

Related Questions