changing a registered client script block

V

Velislav

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
 
V

Velislav

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
 
V

Velislav

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 :)
 
E

Eliyahu Goldin

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.
 
V

Velislav

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.
 
E

Eliyahu Goldin

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.
 
L

Laurent Bugnion

Hi,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top