Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to change value of textbox on focus

I'm writing an application in C#. I would like to replace the value for the TEXT property after the user clicks (focuses) on a textbox. I would like to set the TEXT value to be blank instead of the words "ENTER NAME HERE" when they click to edit the textbox.

Front-end:

<asp:TextBox Text="ENTER NAME HERE" OnClick="MyTextboxID_OnClick" ID="MyTextboxID" runat="server"></asp:TextBox>

Code-behind:

protected void MyTextboxID_OnClick(object sender, EventArgs e) 
{
    MyTextboxID.Text = "";
}

I tried to find the answer to this question but the answers didn't quite match what I wanted to do.

I was hoping C# had something similar to Javascript's "OnClick" or "OnFocus" events. I added the OnClick event to the textbox for illustration purposes. This OnClick event doesn't work.

Thank you in advance for your help!

like image 347
Gigi Avatar asked Feb 12 '23 08:02

Gigi


1 Answers

Remember that ASP.NET is primarly server-side. Actions that run in C# require a post-back to the server. The impact of this on a page can be mitigated somewhat by using AJAX, but this is why you don't see an "OnClick" event off the ASP control.

However, you can still use the Javascript "OnClick" event. Since Javascript runs on the client, and the interaction in this instance is entirely handled on the client, you should just use that.

like image 161
BradleyDotNET Avatar answered Feb 14 '23 14:02

BradleyDotNET