按钮单击可更改动态生成的聚焦文本框中的文本
Button click to change text in focused textbox that was dynamically generated
使用VS2013,我正在创建一个网站来收集平板电脑上的一些数据(使用IE)。我不想使用平板电脑键盘,所以我用asp按钮创建了一个数字键盘。我需要知道哪个文本框有焦点,以便将文本更改为单击按钮的值。
这是我的问题-文本框是在gridview模板字段内动态创建的,该字段在每行绑定时也是动态创建的。所以字段和控件在aspx页面中不存在。
我不知道在单击按钮之前如何确定选中了哪个文本框。我相信这需要在客户端使用js或jquery完成,但我以前从未使用过这些。
我需要一个脚本,插入点击数字到最近选择的文本框。
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If (Not IsPostBack) Then
tfield = New TemplateField()
tfield.HeaderText = "Count"
GridView1.Columns.Add(tfield)
Dim GhostTable As New DataTable
GhostTable.Columns.Add(New DataColumn("Count"))
'Initialize table with 1 blank row
Dim blankRow As DataRow = GhostTable.NewRow()
GhostTable.Rows.Add(blankRow)
'Makes the gridview reflect the GhostTable (currently 1 blank row)
GridView1.DataSource = GhostTable
GridView1.DataBind()
Else
'Occurs on every postback
GridView1.DataSource = Session("GhostTable")
GridView1.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Handles databinding of each gridview1 row and adds the textboxes to the template fields
If e.Row.RowType = DataControlRowType.DataRow Then
Dim tbxCount As New TextBox()
tbxCount.ID = "tbxCount"
tbxCount.Text = TryCast(e.Row.DataItem, DataRowView).Row.Item("Count").ToString()
tbxCount.Width = 100
tbxCount.Height = 61
tbxCount.Font.Size = 36
e.Row.Cells(5).Controls.Add(tbxCount)
End if
End sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Determine which of the gridview row's "tbxCount" was focused and insert the number 1
'preferably this would happen on the client side?
End Sub
对不起,大家都这么久了。我只是想确保我提供了足够的信息。
这是一个独立的示例,说明如何处理文本框的onfocus事件,在事件发生时将文本框分配给变量,以及在单击按钮时操作最后一个焦点文本框的内容。
var lastTbox;
在加载页面时定义了一个全局变量,因此它不在函数中。
<html><body>
<script>
var lastTbox;
function focusFunction(val){
lastTbox = val;
}
function buttonClick(){
lastTbox.value = "text";
}
</script>
<input type="text" id="tbox1" onfocus="focusFunction(this)">
<input type="text" id="tbox2" onfocus="focusFunction(this)">
<input type="button" onclick="buttonClick()">
</body></html>
相关文章:
- 当没有文本输入聚焦时检测空格键按下
- 无法使用 $('input[name=“rate”]').focus() 聚焦文本框;方法
- 不允许输入或聚焦的输入类型文本
- 想要清除文本框+聚焦文本框+阻止asp.net在同一按钮上回发
- 文本框聚焦事件并在IE9中输入问题
- jQuery聚焦文本框并转到末尾,但也可以设置光标的位置,以便用户可以看到它
- 当文本字段或文本区域使用纯JavaScript聚焦时,禁用键盘快捷键
- 在聚焦后检测到文本输入上的非聚焦
- 在聚焦文本输入的同时使用箭头键滚动(Javascript)
- 在聚焦文本输入时触发文件选择
- JQuery - 将光标放在文本区域的末尾,当光标聚焦时
- 如何使用 Reactjs 选择输入中的所有文本,当它聚焦时
- 是否可以将光标锁定/聚焦到文本区域的最后一行
- 在文本字段中插入文本后聚焦光标
- 如何聚焦文本区域单击主类
- NG-重复和文本区域聚焦
- 通过检测 JavaScript 中的 CTRL+A 键来聚焦文本框
- 当用户使用 jquery 将焦点聚焦到文本区域时显示按钮
- 聚焦文本输入时如何单击行?(反应原生)
- 实时文本聚焦