This article demonstrates C# and VB.NET codes to execute server-side click and double-click events on ASP.NET controls like TextBox, ListBox, DropDownList etc.


Adding Click event to TextBox

Please go through the following instruction:

1. Add a TextBox control to the aspx page. You don't need to add any special code to it. The following code shows a sample TextBox added to aspx page:

        
        <asp:Label ID="lblTitle" runat="server" 
            Text="Click on following Text Box:"></asp:Label>
        <br />
        <br />
        <asp:TextBox ID="txtText" runat="server"></asp:TextBox>

2. In the codebehind file of the aspx page, enter the following code:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        //Add Click event to Textbox
        if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txtOnClick")
        {
            txtText_Click();
        }
        txtText.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txtText, "txtOnClick"));

    }

    //Click Event Function
    private void txtText_Click()
    {
        //Write down any code here that you want to execute while Click even on TextBox fires
        txtText.Text = "You clicked me";
    }

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Add Click event to Textbox
        If (Not IsDBNull(Request("__EVENTARGUMENT"))) And Request("__EVENTARGUMENT") = "txtOnClick" Then

            txtText_Click()
        End If


        txtText.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(txtText, "txtOnClick"))
    End Sub


    'Click Event Function
    Private Sub txtText_Click()
        'Write down any code here that you want to execute while Click even on TextBox fires
        txtText.Text = "You clicked me"
    End Sub