I am new to ASP.NET and I was creating a simple form and I wanted to set an OnClick event for a text field so when the user clicked in it, it would clear the text if any was in there. I created the text field and when I click on the Events button in the
properties window, it only had the "TextChange" event. Is it not possible to do OnClick events on text fields using C# in ASP.NET?
indeed TextBox doesn't have Click event at server-side. One reason is that it wouldn't necessarily be reasonable to cause a postback to clear a TextBox (server-side event requires server-side action to occur e.g also a postback).
But, textBox is a web control which supports expando attributes, that is, any attribute added to the control tag which doesn't match any member of the control, is arendered out as is to the markup. So you could in fact use this to create a javascript click
action to clear the TextBox. Either set this on aspx
Actually, you can use the onkeypress event to achieve your purpose, the below code is to be added on page_load (just change the TextBox1 to your textbox id)
When I try and add the OnClick="this.value='' I receive the following two errors from the IDE:
If this attribute value is enclosed in quotation marks, the quotation marks must match.
Warning 2 Validation (ASP.Net): Attribute 'OnClick' is not a valid attribute of element 'TextBox'.
Yes you do, but as its also said on the message itself it's just a warning. It works just fine with it (just use two double-quotes and two single quotes as I demonstrated in my example). In case that disturbs you, use the programmatic approach.
if you like effect you can nested a textbox into linkbutton like this <asp:LinkButton runat="server" OnClick="btnSeleccion_Click" > <asp:TextBox ID="txtProducto" runat="server" Enabled="false" /> </asp:LinkButton>
Absolute_Zer...
Member
38 Points
49 Posts
OnClick Event for Text Box in ASP.NET with C#
Mar 09, 2007 03:25 PM|LINK
I am new to ASP.NET and I was creating a simple form and I wanted to set an OnClick event for a text field so when the user clicked in it, it would clear the text if any was in there. I created the text field and when I click on the Events button in the properties window, it only had the "TextChange" event. Is it not possible to do OnClick events on text fields using C# in ASP.NET?
Thanks,
~zero
chrisoftoday
Member
263 Points
66 Posts
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 09, 2007 03:36 PM|LINK
You'll have to use javascript to clear the text and set the event handler in the code behind
For example, on page load... TextBox1.Attributes["onclick"] = "clearTextBox(this.id)";
Then you would need to create a javascript function called clearTextBox...
function clearTextBox(textBoxID)
{
document.getElementById(textBoxID).value = "";
}
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 09, 2007 03:37 PM|LINK
Hi,
indeed TextBox doesn't have Click event at server-side. One reason is that it wouldn't necessarily be reasonable to cause a postback to clear a TextBox (server-side event requires server-side action to occur e.g also a postback).
But, textBox is a web control which supports expando attributes, that is, any attribute added to the control tag which doesn't match any member of the control, is arendered out as is to the markup. So you could in fact use this to create a javascript click action to clear the TextBox. Either set this on aspx
<asp:TextBox ID="TextBox1" runat="server" OnClick="this.value=''" />
or this in code
Me.TextBox1.Attributes("onclick") = "this.value=''"
Teemu Keiski
Finland, EU
Haissam
All-Star
37421 Points
5632 Posts
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 09, 2007 04:50 PM|LINK
Actually, you can use the onkeypress event to achieve your purpose, the below code is to be added on page_load (just change the TextBox1 to your textbox id)
TextBox1.Attributes.Add("onkeypress","document.getElementById('"+TextBox1.ClientID+"').blur();");
TextBox1.Attributes.Add("onblur","document.getElementById('"+TextBox1.ClientID+"').innerText = ''");
HC
MCAD.NET
| Blog |
Absolute_Zer...
Member
38 Points
49 Posts
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 09, 2007 08:10 PM|LINK
Joteke,
When I try and add the OnClick="this.value='' I receive the following two errors from the IDE:
If this attribute value is enclosed in quotation marks, the quotation marks must match.
Warning 2 Validation (ASP.Net): Attribute 'OnClick' is not a valid attribute of element 'TextBox'.
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 10, 2007 06:19 AM|LINK
Yes you do, but as its also said on the message itself it's just a warning. It works just fine with it (just use two double-quotes and two single quotes as I demonstrated in my example). In case that disturbs you, use the programmatic approach.
Teemu Keiski
Finland, EU
Mahbobo Alam
Member
2 Points
2 Posts
ROnClick Event for Text Box in ASP.NET with C#
Jun 08, 2010 06:25 PM|LINK
Thanks dude these 2 statement realy works no need to write in textbox field just write these two state ment
1st one is code behind in PageLoad
TextBox1.Attributes["onclick"] = "clearTextBox(this.id)";
and 2nd is in aspx in header
function clearTextBox(textBoxID)
{
document.getElementById(textBoxID).value = "";
}
and all work done Thank u very much
Pablogrind
Member
54 Points
100 Posts
Re: OnClick Event for Text Box in ASP.NET with C#
Mar 03, 2011 05:00 AM|LINK