I have ten TextBoxes:
TextBox1
TextBox2
TextBox3
TextBox4
TextBox5
TextBox6
TextBox7
TextBox8
TextBox9
TextBox10

How can I get the name of the TextBox the user clicks on (mouse) during runtime? It is a asp.net web page and the program is written in vb.net.

Cheers

Dani AI

Generated

Clarification and a practical approach

The short reason for the "MouseClick cannot be found" error: those event handlers () apply to Windows Forms, not ASP.NET WebForms. TextBox on an ASP.NET page has no server-side MouseClick — you must capture user interaction in the browser and then pass the info to the server if needed (as , and suggested). Below is a compact, robust pattern that avoids adding inline handlers for every control and works well with keyboard users.

Client-side (unobtrusive, event-delegation; captures keyboard focus too)

document.addEventListener('focusin', function (e) {
  var t = e.target;
  if (t && t.tagName === 'INPUT' && (t.type === 'text' || t.type === 'search')) {
    var hf = document.getElementById('lastClicked'); // a hidden field in the page
    if (hf) hf.value = t.id || t.name;
  }
});

Server-side helper in VB to avoid manual markup changes (recursively adds an attribute)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then AddHandlerToTextboxes(Page)
End Sub

Private Sub AddHandlerToTextboxes(container As Control)
    For Each c As Control In container.Controls
        If TypeOf c Is TextBox Then
            Dim tb As TextBox = CType(c, TextBox)
            tb.Attributes("onfocus") = String.Format("document.getElementById('lastClicked').value = '{0}';", tb.ClientID)
        ElseIf c.HasControls() Then
            AddHandlerToTextboxes(c)
        End If
    Next
End Sub

Notes and tips

  • Use a hidden input or an <asp:HiddenField runat="server"> and read its value on postback (Request.Form("lastClicked") or HiddenField.Value).
  • For predictable client IDs use ClientIDMode="Static" or use tb.ClientID as shown.
  • Prefer focus/focusin (accessibility) and avoid deprecated patterns like window.event or arguments.callee.
  • Do not add System.Windows.Forms references in a WebForms project — they are for desktop apps.

Recommended Answers

All 9 Replies

In mouse click event of textbox1... till textbox10 do

Dim strName as string=string.emty
strName=textbox1.name

You can do like this also

Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick, TextBox2.MouseClick, TextBox3.MouseClick, TextBox4.MouseClick...till textbox10
        Dim strName As String = ""
        Dim text As TextBox = sender
        strName = text.Name
        MsgBox(strName)

    End Sub

MouseClick cannot be found error is generated.

Try out in some other event which are available for control

Thanks for your help. The onClick and Mouse Click events are not available for textboxs. I have imported several references like system.windows.forms, etc. These may work well on a form, however generates issues with web page development.

I have changed to LinkButton. When I click on it, I store an indicator in an invisible label for use later. However n x number of textboxes = alot of coding.

Regards
Adrian

>MouseClick cannot be found error is generated.

You have to use JavaScript code to handle those events (Keys, Mouse etc).

javaScript Code:

<script language="javascript" type="text/javascript">
        function getTextBoxID() {
            var srcid = window.event || arguments.callee.caller.arguments[0];
            var target = srcid.target || srcid.srcElement;
            document.getElementById('textBoxId').value = target.id;
        }
</script>

page.aspx:

<asp:TextBox ID="TextBox1" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox3" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox4" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox5" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox6" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox7" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox8" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox9" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:TextBox ID="TextBox10" runat="server" OnClick="javascript:getTextBoxID();"></asp:TextBox><br />
        <asp:Button ID="btnGetTxtID" runat="server" Text="Button" 
            onclick="btnGetTxtID_Click" />
        <input id="textBoxId" type="hidden" runat="server"  />

Code Behide:

protected void btnGetTxtID_Click(object sender, EventArgs e)
        {
            string id = textBoxId.Value.ToString();
        }

Use Java script like Mouse Click, on mouse etc

Thanks. I will attempt and let you know.

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.