【问题标题】:insert data from repeater to the database将数据从转发器插入数据库
【发布时间】:2013-08-24 23:10:24
【问题描述】:

我的问题已经有很多答案了,但问题是那里有很多糟糕的答案,我必须是个天才才能弄清楚哪些答案会对我有所帮助。 让我们进入正题。 我的问题很简单,我有一个repeater,其中包括两个textboxes

txtQuestion
txtAnswer

我有一个绑定方法

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

包含我的问题以将它们绑定到txtQuestion

大约 17 个问题(Bazinga !!)。 所以我想在txtAnswer 中回答这个问题 并按下按钮将其保存到数据库中。 我使用 multi-tier 架构,所以我想要最重要的方法来保存我的 17 textbox 中的数据以将其推送到我的表中。 我的表有这个结构。

[SessionQuestionId]  [SessionId]  [Question]  [Answer].

我的代码 sn-p:

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

我在 sn-p 后面的代码:

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

}

提前致谢。

【问题讨论】:

    标签: c# asp.net sql-server repeater


    【解决方案1】:

    你可以试试这个,我刚刚展示了从中继器中获取值的方法,你可以根据需要进一步修改它。

    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
    }
    

    【讨论】:

    • 自从我问了这个问题后,我就使用了那个解决方案,我搜索了另一个重要的答案,但这似乎是现在最好的答案,谢谢大家
    【解决方案2】:

    没有很好的答案,但您可以走 ajax 路线或按照这篇文章详细说明如何响应来自中继器的命令参数:

    http://www.webcodeexpert.com/2013/04/how-to-bind-edit-update-and-delete-data.html#.Uhlq3VTD-t8

    【讨论】:

      【解决方案3】:

      您最好的选择是使用 jquery+PageMethods。 例如有一个这样的类

      public class QuestionAnswer
      {
      string txtQuestion{get;set;}
      string txtAnswer{get;set;}
      }
      

      在您的 .cs 代码上创建一个 PageMethods,如下所示:

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

      如下更改您的 .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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 2023-04-10
        • 1970-01-01
        相关资源
        最近更新 更多