从动态文本框 ASP.NET 获取值

发布于 2024-12-26 06:50:16 字数 4734 浏览 0 评论 0原文

我想知道如何从在 asp:datalist 内动态创建的 asp.net 文本框获取值。 然后我想将这些值插入数据库中。

假设我们在网页上有以下数据列表的输出。 (每个问题都是一个 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

例如:我想从问题 2 文本框中获取值,然后将该值解析到我的数据库插入方法。 由于文本框是在数据列表中动态生成的,如何做到这一点?

我编写了以下内容:

<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>

这里是后面的代码(抱歉长度)

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();
        }

当按下相应的按钮“updateName”时,如何获取文本框的值?

谢谢

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 from the datalist. (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 textbox and then parse the value to my database insert method.
How can this be done as the text boxes are generated 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>

And here the code behind (sorry about the length)

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();
        }

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-02 06:50:16

您可以尝试执行以下操作(代码已更新):

    protected void UpdateQuestionName_Click(object sender, EventArgs e)
    {
        int QuestionnaireId = (int)Session["qID"];
        GetData = new OsqarSQL();

        //get the button that caused the event
        Button btn = (sender as Button);
        if (btn != null)
        {
                //here's you question text box if you need it
                TextBox questionTextBox = (btn.Parent.FindControl("QuestionName") as TextBox);

                // Update question name
                GetData.InsertQuestions(questionTextBox.Text, QuestionnaireId);



                //and in case you want more of the associated controls
                //here's your data list with text boxes
                DataList answersDataList = (btn.Parent.FindControl("nestedDataList") as DataList);
                //and if answersDataList != null, you can use answersDataList.Controls to access the child controls, where answer text boxes are
        }

    } // End NewQNRButton_Click

这应该可以按您的意愿工作。

You can try doing something like this (code updated):

    protected void UpdateQuestionName_Click(object sender, EventArgs e)
    {
        int QuestionnaireId = (int)Session["qID"];
        GetData = new OsqarSQL();

        //get the button that caused the event
        Button btn = (sender as Button);
        if (btn != null)
        {
                //here's you question text box if you need it
                TextBox questionTextBox = (btn.Parent.FindControl("QuestionName") as TextBox);

                // Update question name
                GetData.InsertQuestions(questionTextBox.Text, QuestionnaireId);



                //and in case you want more of the associated controls
                //here's your data list with text boxes
                DataList answersDataList = (btn.Parent.FindControl("nestedDataList") as DataList);
                //and if answersDataList != null, you can use answersDataList.Controls to access the child controls, where answer text boxes are
        }

    } // End NewQNRButton_Click

This should work as you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文