We help IT Professionals succeed at work.
Get Started

Get Value From Dynamic Text Box ASP.NET

idny99
idny99 asked
on
571 Views
Last Modified: 2012-06-27
Hi.

I am wondering how I can get the value from an asp.net textbox that is dynamically created inside an asp:datalist.
I would like to then insert these values back into the database.

Let say we have the following output on the web page. (Each Question is a asp:textbox)
Question 1
    Answer 1
    Answer 2
Update Button

Question 2
    Answer 1
    Answer 2
Update Button

Question 3
    Answer 1
    Answer 2
Update Button

For example I would like to get the value from Question 2 and then parse the value to my database insert method.
How can this be done as the text boxes are generate dynamically in the data list?

I have written the following
<asp:DataList runat="server" id="dgQuestionnaire" DataKeyField="QuestionID" CssClass="confirm">
                    <ItemTemplate>
                        <h3>Question <asp:Label ID="lblOrder" runat="server" Text='<%# Container.ItemIndex  + 1 %>'></asp:Label></h3>
                        <asp:TextBox runat="server" ID="QuestionName" Text='<%# Eval("QuestionText") %>' CssClass="form"></asp:TextBox>
                        <asp:DataList ID="nestedDataList" runat="server">
                            <ItemTemplate> 
                                 <asp:TextBox ID="AnswerBox" runat="server" CssClass="form" Text='<%# Eval("AnswerTitle") %>' Width="300px"></asp:TextBox>
                            </ItemTemplate>
                        </asp:DataList>
                        <asp:Button runat="server" ID="updateName" CssClass="button_update" style="border: 0px;" onClick="UpdateQuestionName_Click" />    
                    </ItemTemplate> 
                </asp:DataList>

Open in new window


Here is the code behind
protected void UpdateQuestionName_Click(object sender, EventArgs e)
        {
            int QuestionnaireId = (int)Session["qID"];
            GetData = new OsqarSQL();
            // Update question name
            GetData.InsertQuestions(QuestionName.Text, QuestionnaireId);

        } // End NewQNRButton_Click

        public void BindParentDataList(int QuestionnaireID)
        {
            _productConn = new SqlConnection();
            _productConnectionString += "data source=mssql.myurl.com; Initial Catalog=database_2;User ID=userid;Password=aba123";
            _productConn.ConnectionString = _productConnectionString;
            SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
            myCommand.Parameters[0].Value = QuestionnaireID;
            // check the connection state and open it accordingly.
            _productConn.Open();
            // Sql datareader object to read the stream of rows from SQL Server Database
            SqlDataReader myDataReader = myCommand.ExecuteReader();
            // Pass the Sql DataReader object to the DataSource property
            // of DataList control to render the list of items.
            dgQuestionnaire.DataSource = myDataReader;
            dgQuestionnaire.DataBind();
            // close the Sql DataReader object
            myDataReader.Close();
            // check the connection state and close it accordingly.
            if (_productConn.State == ConnectionState.Open)
                _productConn.Close();
            // foreach loop over each item of DataList control
            foreach (DataListItem Item in dgQuestionnaire.Items)
            {
                BindNestedDataList(Item.ItemIndex);
            }
        }

        public void BindNestedDataList(int ItemIndex)
        {
            int QuestionID = Convert.ToInt32(dgQuestionnaire.DataKeys[ItemIndex]);
            SqlCommand myCommand = new SqlCommand("GetAnswer", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add("@QUESTION_ID", SqlDbType.Int).Value = QuestionID;
            // check the connection state and open it accordingly.
            if (_productConn.State == ConnectionState.Closed)
                _productConn.Open();
            // Sql datareader object to read the stream of rows from SQL Server Database
            SqlDataReader myDataReader = myCommand.ExecuteReader();
            // findControl function to get the nested datalist control
            DataList nestedDataList = (DataList)dgQuestionnaire.Items[ItemIndex].FindControl("nestedDataList");
            nestedDataList.DataSource = myDataReader;
            nestedDataList.DataBind();
            // close the Sql DataReader object
            myDataReader.Close();
            // check the connection state and close it accordingly.
            if (_productConn.State == ConnectionState.Open)
                _productConn.Close();
        }

Open in new window


How can I get the value of a textbox when its corresponding button "updateName" is pressed?

Thanks
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE