最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Raising javascript event from an ASP.NET user control and handling in ASP.NET page - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Aug 16, 2011 at 0:11 user50049 asked Dec 31, 2008 at 14:17 PeanutPeanut 19.4k20 gold badges74 silver badges78 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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?

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);

发布评论

评论列表(0)

  1. 暂无评论