按钮单击可更改动态生成的聚焦文本框中的文本

Button click to change text in focused textbox that was dynamically generated

本文关键字:文本 聚焦 单击 可更改 动态 按钮      更新时间:2023-09-26

使用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>