my question have a lot of answers already but The problem is there is “sooo” much bad answers out there that i have to be a genius to figure out what answers is going to help me . lets get to the point. my problem is simple,i have a repeater
that include two textboxes
txtQuestion
txtAnswer
i have a bind method with
List<SessionQuestion> questions = new List<SessionQuestion>();
that containing my questions to bind them to the txtQuestion
.
about 17 question (Bazinga!!). so i want to answer the question in the txtAnswer
and press the button to save it to the database . i use multi-tier
architecture so i want the most significant way to save the data from my 17 textbox
to push it into my table. my table have this structure .
[SessionQuestionId] [SessionId] [Question] [Answer].
my code snippet :
<table class="table responsive">
<tbody>
<asp:Repeater ID="questionRepeater" runat="server">
<ItemTemplate>
<tr class="">
<td>
<div class="control-group">
<label class="control-label">Queston <asp:Literal id="liteQuestionNum" runat="server" ></asp:Literal> : </label>
<div class="controls">
<asp:TextBox runat="server" ID="txtQ" Text='<%#Eval("Question") %>' ReadOnly="true" CssClass="span8">
</asp:TextBox>
</div>
</div>
<hr />
<div class="control-group">
<label class="control-label">Answer <asp:Literal id="liteAnserNum" runat="server" ></asp:Literal> : </label>
<div class="controls">
<asp:TextBox ID="txtAns" runat="server" CssClass="span8" TextMode="MultiLine">
</asp:TextBox>
</div>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
my code behind snippet:
private void BindRepeater()
{
List<SessionQuestion> questions = new List<SessionQuestion>();
questions.Add(new SessionQuestion { Question = "Question 1 etc.."});
questions.Add(new SessionQuestion { Question = "Question 2 etc.."});
.
.
.
.
questionRepeater.DataSource = questions;
questionRepeater.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
}
Thanks in advance.
You can try this, I've just shown the way of taking the value from the repeater, you can modify it further for your needs.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in questionRepeater.Items)
{
TextBox Qbox = item.FindControl("txtQ") as TextBox;
TextBox Ansbox = item.FindControl("txtAns") as TextBox;
string Question = Qbox.Text;
string Answer = Ansbox.Text;
if (!string.IsNullOrEmpty(Answer))
{
//Perform your insert operation.
}
}
//Bind Repeater again if required
}
No answer is pretty but you can go the ajax route or follow this article detailing responding to a command arguments from your repeater:
http://www.webcodeexpert.com/2013/04/how-to-bind-edit-update-and-delete-data.html#.Uhlq3VTD-t8
Your best bet would be to use jquery+PageMethods. For example have a class like this
public class QuestionAnswer
{
string txtQuestion{get;set;}
string txtAnswer{get;set;}
}
On your .cs code create a PageMethods like follows:
[WebMethod]
public static void SaveAnswers(QuestionAnswer[] answers)
{
....
}
Change your .aspx as follows:
<table class="table responsive">
<tbody>
<asp:Repeater ID="questionRepeater" runat="server">
<ItemTemplate>
<tr class="">
<td>
<div class="control-group">
<label class="control-label">Queston <asp:Literal id="liteQuestionNum" runat="server" ></asp:Literal> : </label>
<div class="controls">
<asp:TextBox runat="server" ID="txtQ" Text='<%#Eval("Question") %>' ReadOnly="true" CssClass="span8 question">
</asp:TextBox>
</div>
</div>
<hr />
<div class="control-group">
<label class="control-label">Answer <asp:Literal id="liteAnserNum" runat="server" ></asp:Literal> : </label>
<div class="controls">
<asp:TextBox ID="txtAns" runat="server" CssClass="span8 answer" TextMode="MultiLine">
</asp:TextBox>
</div>
</div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
.....
don't forget to reference jquery
...
<script>
$('#<%=btnSave.ClientId%>').click(function(){
var answers = [];
$('.responsive tbody tr').each(function(){
answers.push({
txtQuestion:$('td .question').val(),
txtAnswer:$('td .answer').val()
});
});
PageMethods.SaveAnswers(answers);
return false;
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.