转发器控制中的文本框 [英] textbox inside repeater control
问题描述
你好,我的问题很简单。
它的问题和答案页面大约17个问题的答案,
有两个文本框一个问题和答案。
i绑定来自List<>的PageLoad的问题方法。
i有一个转发器控件,其中一个itemtemplate包含两个文本框。
i循环通过转发器并将数据插入我的数据库,但问题是第一个文本框是唯一插入的!
当我按下保存按钮时,它似乎需要为问题文本框提供绑定数据,因为当我写入文本而不绑定它时,它的行为就像它是一个空的就像在答案文本框中发生的那样。
我的代码:
hello , my problem is simple .
it's Questions and answers page about 17 question with it's answer ,
with two text boxes one for question and on for answer.
i bind the questions at the PageLoad from List<> method .
i have a repeater control with one itemtemplate containing the two textboxes.
i loop through the repeater and insert the data in my database , but the problem is the first textbox is the only one inserted !
when i press save button it seems that it takes the binded data for question textbox because when i write into the text without binding it , it behave like it's an empty just like what happening with the Answer text box .
this my code :
<asp:Repeater ID="questionRepeater" ViewStateMode="Enabled" runat="server">
<ItemTemplate>
<tr class="">
<td>
<div class="control-group">
<label class="control-label">Queston : </label>
<div class="controls">
<asp:TextBox runat="server" ID="txtQ" Text='<%#Eval("Question") %>' ReadOnly="true" CssClass="span8">
</asp:TextBox>
</div>
</div>
</td>
</tr>
<tr class="info">
<td>
<div class="control-group">
<label class="control-label">Answer : </label>
<div class="controls">
<asp:TextBox runat="server" ID="txtAns"
Height="150" TextMode="MultiLine" CssClass="span8"></asp:TextBox>
</div>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
private void BindRepeater()
{
List<SessionQuestion> questions = new List<SessionQuestion>();
questions.Add(new SessionQuestion { Question = " Q1" });
questions.Add(new SessionQuestion { Question = " Q2" });
.
.
.
questionRepeater.DataSource = questions;
questionRepeater.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
Sessions session = new Sessions();
SessionQuestion sessionQuestions = new SessionQuestion();
session.ClientId = id;
session.DateTime = DateTime.Now;
session.Report = txtReport.Text;
session.Notes = string.Empty;
session.IsActive = IsActive.Active;
int sessionId = SessionBLL.Insert(session);
foreach (Control item in questionRepeater.Items)
{
sessionQuestions.SessionId = sessionId;
TextBox txtQ = (TextBox)item.FindControl("txtQ");
sessionQuestions.Answer = "";
sessionQuestions.Question = txtQ.Text;
var txtAns = (TextBox)item.FindControl("txtAns") as TextBox;
if (txtAns != null)
{
sessionQuestions.Answer = "";
sessionQuestions.Answer = txtAns.Text;
}
Thread.Sleep(150);
if (txtAns != null && txtQ.Text != null)
{
SessionQuestionBLL.Insert(sessionQuestions);
}
}
string message = "";
Response.Redirect("/Sessions/Sessions.aspx?message=" + message);
}
推荐答案
这是开发人员忘了放
and this the end of the developer who forget to put the
!IsPostBack
页面加载中的
:D
in the pageload :D
这篇关于转发器控制中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!