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

Javascript 按钮单击以更改动态生成的聚焦文本框中的文本,javascript,asp.net,vb.net,gridview,Javascript,Asp.net,Vb.net,Gridview,我正在使用VS2013创建一个网站,用IE在平板电脑上收集一些数据。我不想使用平板电脑键盘,所以我用asp按钮创建了一个数字广告。我需要知道哪个文本框有焦点,以便将文本更改为单击的按钮的值 这是我的问题-文本框是在gridview模板字段中动态创建的,当每一行数据绑定时,该字段也是动态创建的。因此,该字段和控件在aspx页面中不存在 在单击按钮之前,我不知道如何确定选择了哪个文本框。 我相信这需要在客户端使用js或jquery来完成,但我以前从未使用过这些 我需要一个脚本,将点击的数字插入最近选

我正在使用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 LASTBOX;在加载页面时定义全局变量,因此它不在函数中

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

我不知道如何使用javascript实现这一点,因此我无法提供详细信息,但我认为您可以在文本框的onfocuslost或onfocusforthat事件上运行一些设置该文本框变量的内容。然后单击键盘按钮时,使用该变量,该变量现在引用最后一个焦点文本框。我想使用onfocuslost,但据我所知,该事件处理程序不能存在于web应用程序的代码中。因此,javascript需要。我想我可能需要在js中使用UniqueID,但不知道浏览器如何使用javascript处理.Element事件。看,修改这段代码正是我所需要的。下面是我最后使用的:函数按钮ickint{lastTbox.text=int;}在vb代码中加上:`tbxCount.Attributes.Addonfocus,focusfunction很高兴我能提供帮助!