3

How can I add mouse click event to Web TextBox in c#

1
  • Please elaborate, is this in a web application, or a page displayed with the webbrowser control? Commented Nov 20, 2009 at 6:14

7 Answers 7

1

If you're talking about a client side onclick event you can use the OnClick property in the designer, or you can manually add the "onclick" event in code.

For the onclick method try the following:

//asp will allow the onclick event to pass through to the webpage
<asp:textbox onclick="myJavaScriptFunction()" runat="server" id="myTextBox" ... >

To add the attribute manually try this:

myTextBox.Attributes.Add("onclick", "myJavaScriptFunction()");
Sign up to request clarification or add additional context in comments.

1 Comment

Don't think System.Web.UI.WebControls.TextBox supports OnClientClick
1

If you are looking for server side click event. Try this.

public class TextBox : System.Web.UI.WebControls.TextBox, System.Web.UI.IPostBackEventHandler
{

    private static readonly object _clickEvent = new object();

    [System.ComponentModel.Category("Action")]
    public event EventHandler Click
    {
        add { base.Events.AddHandler(_clickEvent, value); }
        remove { base.Events.RemoveHandler(_clickEvent, value); }
    }

    protected virtual void OnClick(EventArgs e)
    {
        EventHandler handler = (EventHandler)base.Events[_clickEvent];
        if (handler != null) handler(this, e);
    }

    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);

        writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onclick, base.Page.ClientScript.GetPostBackEventReference(this, null));
    }

    #region IPostBackEventHandler Members

    void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        this.OnClick(EventArgs.Empty);
    }

    #endregion

}

<pages>
  <tagMapping>
    <add tagType="System.Web.UI.WebControls.TextBox"
         mappedTagType="{namespace}.TextBox"/>
  </tagMapping>
</pages>

Comments

0
<asp:TextBox runat="server" ID="MyTextbox" onclick="alert('click')" />

Comments

0

Highlight the text box Go to "Properties" in the lower right corner Click on the lightning bolt (events)

and in there you will see a list of events, textchanged, onload etc. In there you put in your method name and start programming!

Comments

0

You means like this?

txtBox1.Attributes.Add("onClick","javascript:doSomething()");

Comments

0

You should register js event

textBox1.Attributes.Add("onclick","javascript:alert('ALERT ALERT!!!')")

Comments

0

You Can use JS for the Concern.
Define a JS Method for OnClick Event, Say doSomething() between Script Tag in the ASP Page And Call the Method from the TextBox

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.