【发布时间】:2015-05-19 16:51:06
【问题描述】:
我正在使用 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
对不起,大家这么久了。只是想确保我提供了足够的信息。
【问题讨论】:
-
我不知道如何使用 javascript 执行此操作,因此我无法提供详细信息,但我认为您可以在文本框的 onfocuslost(或 onfocus)事件上运行一些设置该文本框的变量。然后,当单击键盘按钮时,您将使用该变量,该变量现在引用最后一个焦点文本框。
-
我想使用 onfocuslost,但据我了解,该事件处理程序不能存在于 Web 应用程序背后的代码中。因此需要javascript。我想我可能需要在 js 中使用带有 UniqueID 的东西,但不知道如何。
-
元素事件可以由浏览器用javascript处理。见w3schools.com/jsref/event_onfocus.asp。
标签: javascript asp.net vb.net gridview