使用C#从html表插入多行数据?

时间:2012-10-16 16:49:47

标签: c# javascript sql-server bulkinsert

如果联系人有多个联系人不同的手机和电子邮件,我有收集联系人信息的表。用户可以添加更多行以向客户添加更多联系人。 我使用JavaScript添加新的行和html控件 我的问题是当用户有多行时。我不知道如何在某个时间插入多个联系人以及如何从Javascript添加的行中的html控件中获取数据

HTML标记

<asp:Panel ID="p_contacts" runat="server" GroupingText="Contacts">
    <table id="tableContact">
        <thead>
            <tr>
                <th scope="col">
                    Contact
                </th>
                <th scope="col">
                    Contact Type
                </th>
                <th scope="col">
                    Status
                </th>
                <th scope="col">
                    Note
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input name="contact1" id="contact1" runat="server" onclick="return contact1_onclick()">
                </td>
                <td>
                    <select name="contactType1" id="contactType1" runat="server">
                        <option value="">Please select</option>
                        <option value="1">Email</option>
                        <option value="2">Phone</option>
                    </select>
                </td>
                <td>
                    <select name="status1" id="status1" runat="server">
                        <option value="">Please select</option>
                        <option value="1">Active</option>
                        <option value="2">Not Active</option>
                    </select>
                </td>
                <td>
                    <input name="noteContact1" id="noteContact1" runat="server">
                </td>
            </tr>
        </tbody>
    </table>
    <button id="AddContact">
        <img src="images/button-add_blue.png" alt="btnAdd" height="26" width="26" /></button>
</asp:Panel>

Javascript

$("#AddContact").click(function () {
    // add new row to table using addTableRow function
    addTableRow($("#tableContact"));

    // prevent button redirecting to new page
    return false;
});

// function to add a new row to a table by cloning the last row and 
// incrementing the name and id values by 1 to make them unique
function addTableRow(table) {
    // clone the last row in the table
    var $tr = $(table).find("tbody tr:last").clone();
    // get the name attribute for the input and select fields
    $tr.find("input,select").attr("name", function () {
        // break the field name and it's number into two parts
        var parts = this.id.match(/(\D+)(\d+)$/);
        // create a unique name for the new field by incrementing
        // the number for the previous field by 1
        return parts[1] + ++parts[2];
        // repeat for id attributes
    }).attr("id", function () {
        var parts = this.id.match(/(\D+)(\d+)$/);
        return parts[1] + ++parts[2];
    });
    // append the new row to the table
    $(".date").datepicker();
    $(table).find("tbody tr:last").after($tr);

};

C#

protected void btn_add_Click(object sender, EventArgs e)
    {

        try
        {
            //customer contact inforation
            Contacts contact = new Contacts();
            contact.CustomerID  = 1;
            contact.ContactDetail=  contact1.Value.ToString();
            contact.LabelContactTypeID = (int)contactType1.SelectedIndex;
            contact.Status = Convert.ToBoolean(status1.SelectedIndex);
            contact.Note = noteContact1.Value.ToString();
            //bool successedAddCustomer = Customer.AddNewCustoemr(cust);
            bool successedAddCustomer_Contacts = Contacts.AddNewCustomer_Contact(contact);
            if (!successedAddCustomer_Contacts)
            {

                Response.Write("Customer add");
            }

            else
                Response.Write("Can't add new customer");

        }

        catch
        { }

    }

1 个答案:

答案 0 :(得分:0)

添加一个新的隐藏字段,其中包含已添加项目的计数。在您的代码中,使用Request.Form集合来查找循环中的项目,例如

for (int i = 0, i < Request.form["HiddenFieldHere"], i++)
{
        Contacts contact = new Contacts(); 
        contact.CustomerID  = i; 
        contact.ContactDetail=  Request.Form[string.format("contact{0},i)].ToString(); 
}

这个例子并不完美,但我希望它能给你一些指导。