Noro
Noro

Reputation: 1663

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

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

Upvotes: 3

Views: 17433

Answers (7)

Arun Selva Kumar
Arun Selva Kumar

Reputation: 2732

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

Upvotes: 0

m3kh
m3kh

Reputation: 7941

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>

Upvotes: 1

Joe Basirico
Joe Basirico

Reputation: 1985

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()");

Upvotes: 1

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

You should register js event

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

Upvotes: 0

Anuraj
Anuraj

Reputation: 19598

You means like this?

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

Upvotes: 0

Jeremy Morgan
Jeremy Morgan

Reputation: 3372

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!

Upvotes: 0

Chris Fulstow
Chris Fulstow

Reputation: 41872

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

Upvotes: 0

Related Questions