Search code examples
c#asp.netfocusonfocus

Add to OnFocus codebehind without deleting current OnFocus


I'm developing an ASP.NET page with c# on the backend.

I have some javascript to keep track of where the last focus was before postback, and setting it after postback. However, if the field getting focus is a password field, I need to clear the input when the control gets focus.

I have added this like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (TextBox.Text.Trim().Length > 0)
    {
    TextBox.Attributes.Add("onfocus", "this.value=''");
    }
}

but now it overrides my JavaScript, so when I click the next textbox the focus is lost.

Can I add the lines after each other in some way? Like this:

<asp:TextBox ID="TextBoxPassword" runat="server" TextMode="password" onfocus="try{document.getElementById(&#39;__LASTFOCUS&#39;).value=this.id} catch(e) {} + this.value=''" ></asp:TextBox>

I know it cannot be done with a +, but is there some way to do this?


Solution

  • Worked it out by calling it by the id of my textbox, generated by asp.net.

    if(document.getElementById('REQUEST_LASTFOCUS') == document.getElementById('MainContent_TextBoxAdgangskode')) 
                        {
                            document.getElementById('MainContent_TextBoxAdgangskode').value = '';
                        }
    

    I dont know if this is a "good pratice" to call the id directly, but i could not get the other to work.