0

連絡先に複数の連絡先がある場合、連絡先情報を収集するテーブルがあります。電話とメールが異なります。ユーザーは行を追加して、顧客に連絡先を追加できます。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
        { }

    }
4

1 に答える 1

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(); 
}

この例は完璧ではありませんが、何らかの方向性を示してくれることを願っています。

于 2012-10-16T17:24:21.223 に答える