I have 10 textboxes eg:
textbox1
textbox2
textbox3
textbox4
textbox5
textbox6
textbox7
textbox8
textbox9
textbox10

How can I get the name of TextBox eg textbox3 when user clicks (Mouse) on it. The textboxes are on a asp.net web page. Its written in VB.Net.

Dani AI

Generated

The simplest approach depends on whether you need the clicked textbox only on the client or back on the server.

As hinted, detecting the click without a round‑trip needs client code; as noted, this is an ASP.Net page so you can wire client handlers from VB.Net if you prefer. A robust client‑side pattern is event delegation (no need to attach handlers to each control):

document.getElementById('container').addEventListener('click', function(e) {
  var t = e.target || e.srcElement;
  if (t && t.tagName === 'INPUT' && t.type === 'text') {
    var nameOrId = t.id || t.name;
    console.log('clicked:', nameOrId);
  }
});

If you want the click to be initiated from server code (for example to record which textbox and then post back), add a client attribute from VB.Net so each rendered input calls the same JS function:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  If Not IsPostBack Then
    For i As Integer = 1 To 10
      Dim tb = CType(FindControl("TextBox" & i), TextBox)
      If tb IsNot Nothing Then tb.Attributes("onclick") = "onTextboxClick(this);"
    Next
  End If
End Sub

and in JS:

function onTextboxClick(el) {
  document.getElementById('<%= HiddenClicked.ClientID %>').value = el.id || el.name;
  __doPostBack('<%= HiddenClicked.UniqueID %>', '');
}

Troubleshooting notes: ASP.Net may change client IDs (use the control's ClientID in script or assign a CSS class to target inputs). If you only need server-side handling on change, consider AutoPostBack="true" and handle TextChanged so the sender gives you the server ID directly.

Recommended Answers

All 2 Replies

Please post in the ASP.Net forum

Adrian - you cannot have events fire when the user clicks a control (such as a textbox) using vb.net. You would need to use javascript's OnClick event.

document.getElementById(textboxX).onfocus
{
Do stuff in here!
}

The reason for this is because vb.net only "fires" when the webpage communicates directly with the server. Javascript enables client side events - events that occur without the page needing to talk with the server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.