Solved

javascript user control

Posted on 2014-10-23
8
136 Views
Last Modified: 2015-01-02
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
Comment
Question by:Roberta Franchi
  • 3
  • 2
8 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40401100
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
 

Author Comment

by:Roberta Franchi
ID: 40402037
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
 

Author Comment

by:Roberta Franchi
ID: 40405859
solution is not working
0
 

Author Comment

by:Roberta Franchi
ID: 40406683
not working.....not working not working not working
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40407897
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video discusses moving either the default database or any database to a new volume.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now