4

In an ASP.NET page I have a user control and I want to perform some action within it using javascript. When that action is complete I want to raise an event (again in javascript) to be picked up by the containing ASP.NET page (again in javascript).

The reason I want to do this is because I have more than one user control on the page and I need the action performed in one user control to instantiate an action in another user control without doing a postback.

Does anyone know how to do this?

Many thanks.


Hi and thanks for the response. What I'm trying to do is create some form of encapsulation. So if the javascript code does something in one user control that user control shouldn't know of the impact else where. It would raise an event in javascript which can be picked up by asp.net page rendered javascript which can in turn call a javascript method in another user control if it needs to. The idea here is also to eliminate any need for a postback.

Hope this expains it better.

2 Answers 2

6

I think the simple way to implement what you are describing, is to forget about JavaScript events, and just supply a "callback" function to the user-controls.

The way i would implement it, would be to expose a public property on the user control, which takes the name of this JavaScript "callback-function". The user-control is then responsible for calling this function after it has finished its client-side job.

So the page that uses the user-controls will have to implement this JavaScript "callback-function", and then supply the name in properties to the user controls.

Makes sense?

Sign up to request clarification or add additional context in comments.

1 Comment

That looks good - think that could be what I'm after - will do some research into it. Thanks.
0

You can simply run the javascript you need from the rendered HTML of your user control.

<a href="javascript:doSomething();">Click Me</a>

By the sounds of things you want to create some form of controller in JavaScript. When the page loads each of your controls register with the controller. Then an action in one of your controls runs a function on the controller, which does something with the controls registered with it.

Your JavaScript could be as simple as:

var oControls = new Array();

doSomething = function() { for(var i=0;i<oControls.length;i++) { var oControl = document.getElementById(oControls[i]); oControl...... } }

So you need to register your control by using ScriptManager in your user controls render method.

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Startup", String.Format("oControls.push('{0}');", ClientID), True);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.