简体   繁体   English

将数据从中继器插入数据库

[英]insert data from repeater to the database

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 我的问题很简单,我有一个包含两个textboxesrepeater

txtQuestion
txtAnswer

i have a bind method with 我有一个绑定方法

   List<SessionQuestion> questions = new List<SessionQuestion>();

that containing my questions to bind them to the txtQuestion . 包含我的问题以将其绑定到txtQuestion

about 17 question (Bazinga!!). 大约17个问题(Bazinga!)。 so i want to answer the question in the txtAnswer and press the button to save it to the database . 所以我想在txtAnswer回答问题,然后按一下按钮将其保存到数据库中。 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. 我使用multi-tier体系结构,所以我想要最重要的方法来保存17个textbox的数据,以将其推入表中。 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: 没有答案很漂亮,但是您可以走ajax路线,也可以按照本文详细介绍如何响应来自中继器的命令参数:

http://www.webcodeexpert.com/2013/04/how-to-bind-edit-update-and-delete-data.html#.Uhlq3VTD-t8 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. 最好的选择是使用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: 在您的.cs代码上,创建如下的PageMethods:

[WebMethod]
public static void SaveAnswers(QuestionAnswer[] answers)
{
    ....
}

Change your .aspx as follows: 更改您的.aspx,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM