0

I'm trying to select all the text inside an asp:TextBox but it keeps saying:

'TextBox' does not contain a definition for 'SelectAll' and no extension method accepting a first argument of type 'TextBox' could be found (are you missing a using directive or an assembly reference?)

I read that I need Systems.Windows.Forms so I added a reference to it but that didn't work. I don't understand what I'm doing wrong and I've tried searching for answers and nothing.

This is the Default.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
        </form>
    </body>
</html>

and this is the Default.cs file:

using System;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.SelectAll();
    }
}
RhyanO
  • 3
  • 2

1 Answers1

1

I don't think this is possible in ASP/C# as it runs on the server-side. Text-selection is a client action. Try javascript's .select() method. Ex:

var input = document.getElementById('TextBox1');

input.focus();
input.select();

Source: how to auto select an input field and the text in it on page load

Community
  • 1
  • 1
Frode F.
  • 52,376
  • 9
  • 98
  • 114