Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

javascript user control

Hello,

I have an ascx user control and two instances of user control on the same aspx page.

I would like to add javascript to manage the postback of a button inside the user control in order to show a waiting icon and disable the button  while execution.

The following javascript is working well ONLY if there is just one instance of the user control on the page but if there are two or more instances,  the javascritpt is executed but in the first instance of the user control created on the page. (I mean, i I click the button of the second user control on page, the button disabled is the one of the first user control and also the <div> is shown in the first user control.

The javascript is the following:

// Get the instance of PageRequestManager.
var prm = Sys.WebForms.PageRequestManager.getInstance();
// Add initializeRequest and endRequest
prm.add_initializeRequest(prm_InitializeRequest);
prm.add_endRequest(prm_EndRequest);

// Called when async postback begins
function prm_InitializeRequest(sender, args) {
// get the divImage and set it to visible
var panelProg = $get('divImage');
panelProg.style.display = '';

// Disable button that caused a postback
$get(args._postBackElement.id).disabled = true;
}

// Called when async postback ends
function prm_EndRequest(sender, args) {
// get the divImage and hide it again
var panelProg = $get('divImage');
panelProg.style.display = 'none';

// Enable button that caused a postback
$get(args._postBackElement.id).disabled = false;
}

Open in new window


I've tried to put this javascript code inside the web user control but...no way.
Also tried to put it inside the aspx page... but no way.


Thanks
0
Roberta Franchi
Asked:
Roberta Franchi
  • 3
  • 2
1 Solution
 
Julian HansenCommented:
Have you considered using JQuery for this?
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
   $('.buttonClass').click(function() {
       var control = $(this);
       $.ajax({
            url: urltosendto,
            data: datatosend,
            type: 'POST',
            success: function(response) {
                 // interact with control or other elements here
            }
       });
   });
});
</script>

Open in new window

0
 
Roberta FranchiAuthor Commented:
where do I have to put it? in the ascx or aspx?
Which is the reason why this JQuery will work instead of mine?
thanks
0
 
Roberta FranchiAuthor Commented:
solution is not working
0
 
Roberta FranchiAuthor Commented:
not working.....not working not working not working
0
 
Julian HansenCommented:
My previous post was based on a misunderstanding on my part regarding your question.

With respect to your question - the multiple instances solution requires that you give each of your target elements a class and that you then target the class rather than the ID - which is what you are doing.

To do this in JQuery is dead simple. If you have an element with class = "target" then all that is required to target all elements of class 'target' is

$('.target')

However with the MS javascript approach it does not seem as simple - I am not that familiar with the MS javascript libs - I did do some research and did not find anything that specifically targets classes. What you can do is this
Give your target elements a class - in the example below 'YourClass' and then make the code modificaitons as shown.
function prm_InitializeRequest(sender, args) 
{
	// get the divImage and set it to visible
	var panelProg = document.getElementsByClassName('YourClass');
	for (key in panelProg) {
		panelProg[key].style.display = '';
	}

	// Disable button that caused a postback
	$get(args._postBackElement.id).disabled = true;
}

function prm_EndRequest(sender, args) 
{
	// get the divImage and hide it again
	var panelProg = document.getElementsByClassName('YourClass');
	for (key in panelProg) {
		panelProg[key].style.display = 'none';
	}

	// Enable button that caused a postback
	$get(args._postBackElement.id).disabled = false;
}

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now