簡體   English   中英

使用C#從html表中插入多行數據?

[英]insert more than one row of data from html table using C#?

如果聯系人有多個聯系人,我有表格來收集聯系人信息。不同的電話和電子郵件。用戶可以添加更多行以向客戶添加更多聯系人。 我使用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>

Java腳本

$("#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
        { }

    }

添加一個新的隱藏字段,其中將包含已添加項目的數量。 在后面的代碼中,使用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(); 
}

這個例子並不完美,但我希望它能給您一些指導。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM