从HTML输入框捕获数据

时间:2018-02-01 15:23:47

标签: c# html asp.net datatable webforms

我正在生成一行带有标题和单行的html表。我有一个按钮,当按下时会激活一些JQuery并添加第二行。

如何捕获文本框中的输入并将值插入数据表?映射将是数据表中列名称的映射,并且需要将tr作为数据表的行插入。

这是当前语法

<table id="tab">
<thead>
  <tr>  
    <th id="Header1" runat="server" >Header 1</th>
    <th id="Header2" runat="server" class="hidetablerows" >Header 2</th>
    <th id="Header3" runat="server" class="hidetablerows" >Header 3</th>
</tr>
</thead>
<tbody>
  <tr>
    <td id="lblHeader1" runat="server" ><input type="text" name="txtHeader1"></td>
    <td id="lblHeader2" runat="server" class="hidetablerows" ><input type="text" name="txtHeader2"></td>
    <td id="lblHeader3" runat="server" class="hidetablerows" ><input type="text" name="txtHeader3"></td>
</tr>
    </tbody>
    <tfoot>
        <tr>
            <td>
                <button id="add" type="button">Add</button>
            </td>
        </tr>
    </tfoot>
</table>

<script>
    $(function () {
        $("#add").on("click", function () {
            var $row = $("#tab tbody tr").first().clone();
            $row.find("input").val(""); 
            $("#tab tbody").append($row);
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

从客户端将数据导入服务器的Web Forms方法是使用控件。

First Name: <asp:TextBox runat="server" id="FirstNameTextBox" />
<br />
LastName: <asp:TextBox runat="server" id="LastNameTextBox" />
<br /> <asp:Button runat="server" onclick="SubmitButton_Click" Text="Submit" />
<asp:Label runat="server" id="ResultsLabel" />

然后在服务器上处理SubmitButton_Click事件。

protected void SubmitButton_Click(object sender, EventArgs e)
{
    string firstName = FirstNameTextBox.Text;
    string lastName = LastNameTextBox.Text;

    // Now you can do whatever you want with the submitted values.
    // Let's display them on the page.
    ResultsLabel.Text = "Hello " + firstname + " " + lastName + "!";

    // Rather than having loose properties,
    // it's often a good idea to group your properties into a structure, 
    // such as a class that represents some concept.
    Person submittedPerson = new Person();
    submittedPerson.FirstName = firstName;
    submittedPerson.LastName = lastName;
}

// this class should be defined in its own file
public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
}