Possible Error Using __doPostBack and JQuery
-
16-06-2021 - |
Pergunta
I'm trying to use __doPostBack to pass some data from client to server side. I'm not very expierenced with JS, but when debugging in Firebug something wierd is happening. I'll show my code first.
<script type="text/javascript">
function BeginDownload() {
var temp = $("#waterwheel-carousel-horizon").CurrentSelectedImg().toString()
__doPostBack("ImgDownload", temp);
}
</script>
Here's the relevant jquery I'm attempting to call.
// Relevant Waterwheel jquery code.
$.fn.CurrentSelectedImg = function () {
return data.currentCenterItem.data().index; };
$.fn.waterwheelCarousel = function (options) {
// override the default options with user defined options
options = $.extend({}, $.fn.waterwheelCarousel.defaults, options || {});
return $(this).each(function () {
/* These are univeral values that are used throughout the plugin. Do not modify them
* unless you know what you're doing. Most of them feed off the options
* so most customization can be achieved by modifying the options values */
var data = {
itemsContainer: $(this).find(".carousel-images"),
totalItems: $(this).find(".carousel-images img").length,
containerWidth: $(this).width(),
containerHeight: $(this).height(),
currentCenterItem: null,
items: [],
itemDistances: [],
waveDistances: [],
itemWidths: [],
itemHeights: [],
itemOpacities: [],
carouselRotationsLeft: 0,
currentlyMoving: false,
itemsAnimating: 0,
currentSpeed: options.speed,
intervalTimer: null
};
// Setup the carousel
beforeLoaded();
// Preload the images. Once they are preloaded, the passed in function
// will be called and the carousel will be setup
preload(function () {
setupDistanceArrays();
setupCarousel();
setupStarterRotation();
});
And the code behind the scenes. It's not being reached at the moment.
// This code is currently not reached, but I put it here for completion.
#region EventTarget
private string EventTarget
{
get{
return Request.Form["__EVENTTARGET"].ToString().ToUpper();
}
}
#endregion EventTarget
#region HandlePostBack
void HandlePostBack()
{
switch (EventTarget)
{
case "ImgDownload":
DownloadImage(EventArgs);
break;
}
}
#endregion HandlePostBack
private void DownloadImage(string index)
{
string test = index;
}
Ultimately what happens is that I click the linkbutton and firebug jumps to the first line of code, but before it even finishes that function it suddenly jumps to __doPostBack function's code before I even pass in the variables. Why is this happening?
I should also mention I'm doing this in DNN if that makes a difference. I think the problem might be the way I formed that jquery function or how I'm calling it.
Solução
Found the problem. I just had to move where my jquery was placed in the code. I think it just wasn't inside some bracket so it couldn't see data. Lack of intellisense and disorganized code strikes again! Now to write that download function.