获取所有输入值并添加到db

时间:2014-03-11 08:15:36

标签: c# for-loop

我的表格约为> 100行。我需要获取所有的值并在sql中插入。是否有可能动态地更改变量并插入所有变量而无需编写太多代码。

<form runat="server" method="POST" id="passportForm" class="form-inline">
        <div class="doklist" runat="server" id="MSControl">
            <table class="table table-condensed">
                <tbody>
                    <tr>
                        <td>1</td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentName1" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentSer1" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentNumber1" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentFrom1" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control datepickclass" ID="deskDocumentDate1" /></td>
                        <td><asp:FileUpload runat="server" id="deskDocumentCopy1" /></td>
                    </tr>
                    <tr>
                        <td>2</td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentName2" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentSer2" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentNumber2" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentFrom2" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control datepickclass" ID="deskDocumentDate2" /></td>
                        <td><asp:FileUpload runat="server" id="deskDocumentCopy2" /></td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentName3" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentSer3" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentNumber3" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control" ID="deskDocumentFrom3" /></td>
                        <td><asp:TextBox runat="server" CssClass="requiredd form-control datepickclass" ID="deskDocumentDate3" /></td>
                        <td><asp:FileUpload runat="server" id="deskDocumentCopy3" /></td>
                    </tr>
                </tbody>
            </table">
        </div>  
 </form>              

例如像这样的东西

protected void test(object sender, EventArgs e)
{
    KZDBEntities db = new KZDBEntities();
    belgeListe doc = new belgeListe();

    for (int i = 1; i < 100; i++)
    {
        string docName = "deskDocumentName"+i;
        string docNo = "deskDocumentNo" + i;
        doc.belgename = docName.Text;
        doc.belgeNo = docNo.Text;
    }

    db.belgeListe.Add(doc);
    db.SaveChanges();
}

1 个答案:

答案 0 :(得分:1)

修改:只是看到您正在使用网络表单,然后它更简单,在容器控件上使用FindControl或者更正确 - 在NamingContainer < / p>

假设它们位于Page

之上
for (int i = 1; i < 100; i++)
{
    string docName = "deskDocumentName" + i;
    string docNo = "deskDocumentNo" + i;
    TextBox txtDocName = (TextBox)this.FindControl(docName);
    TextBox txtDocNo = (TextBox)this.FindControl(docNo);
    doc.belgename = txtDocName.Text;
    doc.belgeNo = txtDocNo.Text;
}

<击> 您可以使用Controls.Find

for (int i = 1; i < 100; i++)
{
    string docName = "deskDocumentName" + i;
    string docNo = "deskDocumentNo" + i;
    TextBox txtDocName = (TextBox)Controls.Find(docName, false)[0];
    TextBox txtDocNo = (TextBox)Controls.Find(docNo, false)[0];
    doc.belgename = txtDocName.Text;
    doc.belgeNo = txtDocNo.Text;
}

第二个参数是一个布尔值,表示您是否要递归搜索所有子控件。

另一种方法是使用OfType

var allTxt = Controls.OfType<TextBox>();
for (int i = 1; i < 100; i++)
{
    string docName = "deskDocumentName" + i;
    string docNo = "deskDocumentNo" + i;
    TextBox txtDocName = allTxt.First(txt => txt.Name == docName);
    TextBox txtDocNo = allTxt.First(txt => txt.Name == docNo);
    doc.belgename = txtDocName.Text;
    doc.belgeNo = txtDocNo.Text;
}

仅当所有TextBox都在同一个容器控件中时才有效。