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

Recommended Answers

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
Jump to Post

Try out in some other event which are available for control

Jump to Post

>MouseClick cannot be found error is generated.

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

Jump to Post

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 learning and sharing knowledge.