使用C#从jquery向gridview添加文本框值

时间:2017-01-08 14:49:21

标签: javascript jquery

我有网格视图没有任何记录,我想使用jQuery在不使用c#的情况下将文本框值添加到网格视图中。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果Id属性是静态的(如果没有,请将CssClass属性添加到GridView),那么您可以使用jQuery附加文本框



function addRecord() {
  var record = $('#newRecord').val();
  addRow(record);
  $('#newRecord').val('');
}

function addRow(value) {
  $('#GridView1 tbody').append('<tr><td>' + value + '</td></tr>');  
}
&#13;
table, td, th {
  border: 1px solid;
  border-spacing: 0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Let's say that the gridview rendrer as table-->
<table id="GridView1">
  <thead>
  <tr>
    <th>Col 1</th>
  </tr>
  </thead>
  <tbody>
    <!-- It's empty because of no records-->
  </tbody>
</table>

<input id="newRecord" type="text" placeholder="Record to add" />
<!-- button with OnClientClick will render as onclick html attribute -->
<button onclick="addRecord()">Add a record</button>
&#13;
&#13;
&#13;