By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,853 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,853 IT Pros & Developers. It's quick & easy.

changing a registered client script block

P: n/a
Hi,

I have a client script block, which is registered in my Page_Load.

However a button may result in the need to change the script which I've
registered.

Obviously the OnClick event occurs after Page_Load, so calling
RegisterClientScriptBlock with the same key does nothing.

Unfortunately I have other buttons (inside custom web controls) which
cause postbacks but no changes to the script, which means I can't do
something like

if (!IsPostBack) CallMethodWhichCreatesAndRegistersTheScript();

in Page_Load

Nov 7 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
A working, albeit possibly not a very elegant, solution is this:

1. do not register the script in the web form's Page_Load if IsPostBack
is true.

2. make sure the scope of the script generation/registration method is
public

3. call the method in each button's OnClick event

3.1. for buttons in custom controls, simply cast the Page object to
your web form so you can call the method.
ie MyWebForm page = (MyWebForm)Page;
page.ScriptCreationRegistrationMethod();

Cheers

Nov 7 '06 #2

P: n/a
Of course, this merely prevents the script from being registered until
we are assured that the version that we have is the version that gets
registered.

The question of changing an already registered script (if possible)
still remains, so if anyone has a suggestion, go wild :)

Nov 7 '06 #3

P: n/a
Instead of changing the script code, pass a parameter string from server to
client and get the script code to check the parameter to determine what to
do. Use a hidden input control to pass data 2-way between server and client.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Velislav" <vg*****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Hi,

I have a client script block, which is registered in my Page_Load.

However a button may result in the need to change the script which I've
registered.

Obviously the OnClick event occurs after Page_Load, so calling
RegisterClientScriptBlock with the same key does nothing.

Unfortunately I have other buttons (inside custom web controls) which
cause postbacks but no changes to the script, which means I can't do
something like

if (!IsPostBack) CallMethodWhichCreatesAndRegistersTheScript();

in Page_Load

Nov 7 '06 #4

P: n/a
That sounds good, but I'm struggling to see how it would work in my
specific case.

I have a custom control, which (amongst other things) contains a
checkbox.

My button creates these custom controls on the fly and adds them to the
form.

The client-side script makes all the checkboxes mutually exclusive.
Which means all the checkboxes have to be created before I can do any
client-side manipulations on them, right?

How can I use a parameter string/hidden input to represent the
checkboxes the script will deal with? Just for curiosity's sake.

Eliyahu Goldin wrote:
Instead of changing the script code, pass a parameter string from server to
client and get the script code to check the parameter to determine what to
do. Use a hidden input control to pass data 2-way between server and client.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
Nov 7 '06 #5

P: n/a
One possibility is to find the checkboxes on client side based on their id.
I gave just a general idea, could be in your particular case server-side
script emitting is justified.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Velislav" <vg*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
That sounds good, but I'm struggling to see how it would work in my
specific case.

I have a custom control, which (amongst other things) contains a
checkbox.

My button creates these custom controls on the fly and adds them to the
form.

The client-side script makes all the checkboxes mutually exclusive.
Which means all the checkboxes have to be created before I can do any
client-side manipulations on them, right?

How can I use a parameter string/hidden input to represent the
checkboxes the script will deal with? Just for curiosity's sake.

Eliyahu Goldin wrote:
>Instead of changing the script code, pass a parameter string from server
to
client and get the script code to check the parameter to determine what
to
do. Use a hidden input control to pass data 2-way between server and
client.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

Nov 7 '06 #6

P: n/a
Hi,

Velislav wrote:
Hi,

I have a client script block, which is registered in my Page_Load.

However a button may result in the need to change the script which I've
registered.

Obviously the OnClick event occurs after Page_Load, so calling
RegisterClientScriptBlock with the same key does nothing.

Unfortunately I have other buttons (inside custom web controls) which
cause postbacks but no changes to the script, which means I can't do
something like

if (!IsPostBack) CallMethodWhichCreatesAndRegistersTheScript();

in Page_Load
It's easy to identify which button has been clicked according to the
Request.Form collection. The clicked button's ClientID's will be present
in the Form collection.

Ex:
If your button is bnExecuteForm, then:

(in OnInit)

if ( this.Request != null
&& this.Request.Form != null
&& this.Request.Form[ bnExecuteForm.ClientId ] != null )
{
// Register client script
}

I never quite understood why the controls' events occur so late in the
AS.NET chain of events...

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.