我有一个转发器控件,其中ItemTemplate
包含两个文本框。我循环遍历Repeater并将数据插入我的数据库。问题是第一个TextBox是唯一插入的。
我在PageLoad
方法中绑定List中的第一个TextBox。
<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>
我的代码背后:
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);
}
答案 0 :(得分:1)
也许是因为你没有在你的Page_Load
中附上你的转发器数据绑定If (!IsPostBack)
{
// Databind your repeater
}
此外,当您遍历转发器时,最好迭代类型项目和项目的项目。替代方案如下所示
foreach (RepeaterItem item in questionRepeater.Items)
{
if (item.ItemType == ListItemType.Item ||
item.ItemType == ListItemType.AlternatingItem)
{
}
}