views:

290

answers:

2

I have a Web Forms textbox in a gridview and I want to higlight the text on a button click. textbox.select(start,end) doesn't work. Here is the code:

    Dim row As GridViewRow = TryCast(DirectCast(sender, ImageButton).Parent.Parent, GridViewRow)
    Dim txtdays As TextBox = row.Cells(2).FindControl("txtDays")
    Dim lbldays As Label = row.Cells(2).FindControl("lblDays")
    Dim btndel As ImageButton = row.Cells(2).FindControl("btndel")
    Dim imgbttnadd As ImageButton = row.Cells(2).FindControl("imgbttnadd")

    //Show textbox and set its text.
    txtdays.Visible = True
    txtdays.Text = lbldays.Text
    txtdays.Focus()

    //Here is where I want to select the text.
    txtdays.Select() //????????  Doesn't work.

How do you do this?

.Select works on Windows Forms textboxes but not Web Forms textboxes.

A: 

Based on your comments, you might want to specify in your question that you're dealing with an ASP.NET control. The answer Chris gave does work on a WinForms textbox.

I'm not sure how you'd do it in ASP.NET, but I can bet you can't do it in server-side code. You'll probably have to use JavaScript to pull it off.

Sam Erwin
+2  A: 

You need to understand that a client side script will be needed to execute the effect you desire. However, server-side code would be required to accurately identify the element. This is why you need the "rendered ID" of the txtDays TextBox (since it lies in a GridView, the rendered ID will be different)

Using JavaScript, you can do it as follows:

The following code would be in your ASPX page, for instance (you may want to wrap it in a function). I'm assuming that you have basic familiarity with JavaScript.

var txtDays = document.getElementById(<%= txtDays.ClientID %>);
if (txtDays != null)
{
    txtDays.focus();
    txtDays.select();
}

The Select function you are looking for in VB does not exist. The Focus() function only exists (.NET 2.0+) because of the implementation of the Focus API, which renders a JavaScript function similar to the above sample in client side code.

Edit: (after comments)

In that case, I guess you can use something like the following instead of the line that does not work (in your code sample).

txtDays.Attributes.Add("onfocus", "this.select();")

This should ensure that when the onfocus event is raised, the TextBox will be selected as well.

Cerebrus
Thanks for your extra help (+1). But I've tried a function much like the one you requested but I get a javascript error saying that txtdays is not declared. My txtdays is in a template field, but it should find it right? Should i post some markup?
Eric
Hmmm... Rather than markup, I think we need much more info. on exactly what is going on. It seems that you click the ImageButton and the page posts back to the server and then the provided code runs. So, does this page need to reappear in the browser but only with the textbox selected ? If that is the only functionality desired, you do not need to post back to the server at all.
Cerebrus
The JS is not able to find the textbox because the rendered ID of child controls of a GridView is quite different. You should take a look at the HTML source of rendered page and it will turn out to be something like : "MyGridView_ctl01_txtDays". If you pass that ID to Javascript, it will be able to focus and select your textbox.
Cerebrus
What happens is a button is clicked to edit a label. In my _click event I delete the data in the database that appeared in the label and I show that label's text in the textbox. I want to highlight the text to give the idea that they are editing or updating the record. The postback is needed because I hit the server for the delete command.
Eric
That's a great concept. However, a javascript error occurs saying 'This' is not defined.
Eric