0

I have form where will be approximately > 100 rows. i need to take values of all and insert in sql. is it possible to change variable dynamicly and insert all of them without writing much code.

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

for example something like this

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();
}
DSI
  • 279
  • 1
  • 6
  • 16

1 Answers1

1

Edit: Only just seen that you are using webforms, then it's even simpler, use FindControl on their container-control or - more correct - on their NamingContainer

Presuming they are on top of the 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;
}

You could use 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;
}

The second parameter is a boolean which indicates if you want to search recursively all child controls.

Another approach is to use 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;
}

which works only if all TextBoxes are in the same container-control.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thanks. i think i understand the logic. but it doesn't work with my code. i thing problem is namecontainer. could you please help to rewrite the code. my form structure is actually this. UPDATED TOP – DSI Mar 11 '14 at 09:21
  • Are you using a MasterPage? – Tim Schmelter Mar 11 '14 at 09:31
  • Yes. <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeFile="passport.aspx.cs" Inherits="Services_svraccess_passport" %> – DSI Mar 11 '14 at 09:35
  • 1
    @BarsDS: if you are using a `MasterPage` and the controls are sitting on top of the content-page (so they are not nested in child controls like a `GridView`, the `ContentPlaceHolder` of the `MasterPage` is the `NamingContainer` not the `Page`. So you need to find it first. Have a look: http://stackoverflow.com/a/8163964/284240 Once you've the reference to that `ContentPlaceHolder` you can use my code above with `FindControl` on it to find your `TextBoxes`. Another approach would be to put your controls in a container-control like `Panel`(rendered as div) and use `FindControl` on that. – Tim Schmelter Mar 11 '14 at 09:47