Reputation: 1663
How can I add mouse click event to Web TextBox in c#
Upvotes: 3
Views: 17433
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
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
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
Reputation: 50712
You should register js event
textBox1.Attributes.Add("onclick","javascript:alert('ALERT ALERT!!!')")
Upvotes: 0
Reputation: 19598
You means like this?
txtBox1.Attributes.Add("onClick","javascript:doSomething()");
Upvotes: 0
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
Reputation: 41872
<asp:TextBox runat="server" ID="MyTextbox" onclick="alert('click')" />
Upvotes: 0