dalsandhu
asked on
RegisterStartupScript
Hi Experts
I have injected some startup javascript from my ASP.NET/VB.NET code, and this javascript runs successfully when the page loads. I've used Page.RegisterStartupScript (<name>, <script>) to do this.
In certain circumstances I want to unregister this startup code so it doesn't execute - any ideas how to do this?
Thanks
I have injected some startup javascript from my ASP.NET/VB.NET code, and this javascript runs successfully when the page loads. I've used Page.RegisterStartupScript
In certain circumstances I want to unregister this startup code so it doesn't execute - any ideas how to do this?
Thanks
ASKER
I use this method in the Page_Load sub to set the focus to the first field on my form.
The user can update values in certain fields which cause a trip to the server and the page to reload. After the user has updated these fields I want the focus to move to the next field rather than back to the first field. The problem is that the function fired by the field change event, fires after the Page_Load sub. So, I can't put a conditional in the Page_Load code as at that point there is no knowledge of the field change event.
If this isn't clear I'll provide the code.
Thanks
The user can update values in certain fields which cause a trip to the server and the page to reload. After the user has updated these fields I want the focus to move to the next field rather than back to the first field. The problem is that the function fired by the field change event, fires after the Page_Load sub. So, I can't put a conditional in the Page_Load code as at that point there is no knowledge of the field change event.
If this isn't clear I'll provide the code.
Thanks
I think you may be able to use the textchanged event to call your startup script.
Private Sub txtBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
Page.RegisterStartupScript ("MoveFocu s", "<script language>document.forms(0) ." & txtNewBox.ClientID & ".focus();</script>")
End Sub
If I'm still off, please let me know.
Thx.
Private Sub txtBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
Page.RegisterStartupScript
End Sub
If I'm still off, please let me know.
Thx.
ASKER
That's exactly what I've done, so you are not 'off' at all.
The focus does change, which is good, but the Page_Load script also fires. This has the effect that both the field I want, and the first field, are highlighted. Perhaps I'm being pernickety, but I don't like this, so I want to turn the first script off.
I've tried 'overwriting' the script, i.e. calling the method with the same ID, but this doesn't work - the original script fires.
I've just thought, I could have the movefocus script retrieve the name of the field to move to based on the value in a hidden field. The page_load and onchange procs could then set the value of this hidden field. Hopefully the hidden field value will be set before the movefocus script fires.
I'd still like to know how to unregister the script though, as this would be easier.
Thanks
The focus does change, which is good, but the Page_Load script also fires. This has the effect that both the field I want, and the first field, are highlighted. Perhaps I'm being pernickety, but I don't like this, so I want to turn the first script off.
I've tried 'overwriting' the script, i.e. calling the method with the same ID, but this doesn't work - the original script fires.
I've just thought, I could have the movefocus script retrieve the name of the field to move to based on the value in a hidden field. The page_load and onchange procs could then set the value of this hidden field. Hopefully the hidden field value will be set before the movefocus script fires.
I'd still like to know how to unregister the script though, as this would be easier.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'll go with the 'not ispostback' - thanks onskee.
I wanted to have my startup script outside the ispostback conditional, as there are cancel, save & navigation buttons that reload the page. I can repeat the startup script register in the onclick event procs for these buttons easily enough though.
Cheers,
Dal
I wanted to have my startup script outside the ispostback conditional, as there are cancel, save & navigation buttons that reload the page. I can repeat the startup script register in the onclick event procs for these buttons easily enough though.
Cheers,
Dal
If blnMyCondition = True Then
' do something here
Else
Page.RegisterStartupScript
End IF
If you can provide a code sample or some more details, we might be able to give you a more direct answer.