【问题标题】:insert more than one row of data from html table using C#?使用C#从html表中插入多行数据?
【发布时间】:2012-10-16 16:49:47
【问题描述】:

如果联系人有多个联系人不同的电话和电子邮件,我有表格来收集联系人信息。用户可以添加更多行以向客户添加更多联系人。 我使用 JavaScript 添加新的行和 html 控件 我的问题是当用户有多于一行时。我不知道如何在某个时候插入超过 1 个联系人以及如何从 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
        { }

    }

【问题讨论】:

  • 您可以在新添加的行上设置一些“新”属性并过滤这些行。
  • 你将如何添加contact.CustomerID = 1的其他联系人;重复的 CustomerID 怎么样,您应该想出一种方法让该 ID 自动生成或创建您自己的密钥序列
  • 我只将该行用于开发目的

标签: c# javascript sql-server bulkinsert


【解决方案1】:

添加一个新的隐藏字段,该字段将包含添加项目的计数。在后面的代码中,使用 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(); 
}

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

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 1970-01-01
    • 2017-06-07
    • 2014-11-13
    • 1970-01-01
    • 2022-11-27
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多