我想知道如何从asp.net文本框中获取值,该文本框是在asp:datalist中动态创建的.
我想将这些值重新插入数据库.
我想将这些值重新插入数据库.
假设我们在datalist的网页上有以下输出. (每个问题都是一个asp:文本框)
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”时,如何获取文本框的值?
谢谢
解决方法
你可以尝试做这样的事情(代码更新):
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
这应该按你的意愿工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。