How do you keep text selected in a textbox after the page refreshes?

To begin with my project is about a medium size project and I'm using alot of AJAX.

Here is the javascript function:
     function _HighlightTextBox() {
         document.form1.txtCheckInOut_BarcodeNumber.select();
     }
<br/>
..and here is the html
                                    <asp:Button ID="btnStudentInfoUpdate" runat="server"
                                       onclick = "btnStudentInfoUpdate_Click" Text="Update" 
                                        OnClientClick = "_HighlightTextBox"
                                        ToolTip="Only allows updates to the Music Teacher and the Comments fields" 
                                        Width="94px" />

Open in new window


What's going on is that it hits the javascript function first and then it goes through Page_Load function and then it finally hits

the btnStudentInfoUpdate_Click function.

When it goes through these extra steps, I am changing the properties of various controls like, visible = true, etc; So I thought

that maybe changing the properies of these controls is like clicking on another control on the form thus giving focus to the

control that you clicked on.

So I use the RegisterStartupScript function at the end of the btnStudentInfoUpdate_Click function, but that didn't work either.
Once it hits the RegisterStartupScript it does nothing. The code is below:
                this.txtCheckInOut_BarcodeNumber.Focus();
                ClientScript.RegisterStartupScript(this.GetType(), "onload", "window.onload = function() { 

document.form1.txtCheckInOut_BarcodeNumber.select(); }", false);

Open in new window


On know that the text in the textox is selectd because when I put a break point, the text in the field is selected but  when I

continue through the program the box has the focus becuase the cursor is blinking there, but the text is no longer selected..

Any ideas how to keep the text in the textbox selected when the page refreshes?


JosephNBurtonAsked:
Who is Participating?
 
khan_webguruCommented:
I also faced same kind of problem few days back but with little difference. My problem was that after refreshing the page there was a textarea on my page I wanted to set cursor on that so did lot of struggle but in the end fixed it.  

SOLUTION

If your mentioned function works fine and select the text box with no issue then idea will be same to register client side event from code behind but with little difference.

Just copy the code below and replace your code where you are registering the client side function in code behind.

 
string jScript = "<script>setTimeout('_HighlightTextBox()',3000)</script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "jScript", jScript);

Open in new window


I am assuming that your client side function "_HighlightTextBox()" will work to select I just add a timer currently it will call the function after 3 sec assuming that in this time your page will load successfully.

If you have small page and know that page can be load earlier than 3 sec then just set the milliseconds according to that.

It worked for me lets see in your case.

Thanks,

AAK
0
 
IgorNedCommented:
Joseph,
Just add Page. to your ClientScript.RegisterStartupScript...., otherwise the script is added to the TexBox.
Page.ClientScript.RegisterStartupScript(this.GetType(), "onload",  "window.onload = function() { document.form1.txtCheckInOut_BarcodeNumber.select(); };", true);

Open in new window

0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi, if you are using AJAX then use
ScriptManager.RegisterStartupScript
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
JosephNBurtonAuthor Commented:
Thanks for your response guys but unfortunately nothing is happening.

@IgorNed
I get nothing. And when I try to move the RegisterStartupScript to the Page_Load
function, the string from the RegisterStartupScript is written to web page.

@IJZ
I get nothing in the intellisense that refers to RegisterStartupScript when I use the ScriptManager;
which mine is name ScriptManager1.

 



0
 
JosephNBurtonAuthor Commented:
@khan_webguru:

Registering the script still didn't work for me.
However, it did work with the setTimeOut function but I had to implement it differently.

I had to create a client side function that calls the timeout function that called my highlight function.
So it was a good idea to have the highlight function delay for a bit, which gave the page time to finish loading; and then resume it to highlight the textbox.

I also placed an 'OnClientClick' after the onclick event on the client side.

Anyhow, this is what I implemented:

function _setTimeOut(){
  setTimeOut('_HighlightTextBox()',3000);
}

function _HighlightTextBox() {
  document.form1.txtCheckInOut_BarcodeNumber.select();
}

                                    <asp:Button ID="btnStudentInfoUpdate" runat="server"
                                       onclick = "btnStudentInfoUpdate_Click" Text="Update" 
                                        OnClientClick = "_setTimeOut()"
                                        ToolTip="Only allows updates to the Music Teacher and the Comments fields" 
                                        Width="94px" /> 

Open in new window

0
 
JosephNBurtonAuthor Commented:
Eventhough he recommended registering the script block which didn't work; he did give me the idea to use the timeout function which work beautifully.
0
 
khan_webguruCommented:
:) @JosephNBurton Thats what I was facing previously and done like that. Any way best of luck for your project.

Thanks.
0
 
IgorNedCommented:
Joseph,
I just tested and it worked OK.

Set the last parameter to "true", so it will be created with script tags:

protected void btnStudentInfoUpdate_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", "window.onload = function() { document.form1.txtCheckInOut_BarcodeNumber.select(); };", true);
    }
0
 
JosephNBurtonAuthor Commented:
@IgorNed

I did try it with the last paramter set to "true". The code above was before I changed it.

I believe it did work as you say but I encouter another problem of it getting de-selected becuase of other things happening with my code.

When I put the
        Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", "window.onload = function() { document.form1.txtCheckInOut_BarcodeNumber.select(); };", true);

in a smaller project it work just fine, but it would not in my project.

So by adding that timeout and setting the value to 3 seconds, it gives it time for everything else to finsh doing what ever its doing and then its able to concentrate on just selecting the text in  the textbox.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.