views:

401

answers:

1

Hello all,

With the help of this community, I've been able to pass a static chunk of text from javascript (clientside) to ASP 3.5 (server side)! This piece of code works beautifully:

$('.drop').droppable({
    tolerance: "touch", // Here should be a string
    drop: function() {

    $('[id$="myHidden"]').val('Testy test test');
    __doPostBack('<%= HiddenButton.UniqueID  %>', '');
    alert($('[id$="myHidden"]').val());

    }


});

What the above does, is when an element with the class of .drop is dropped, it will assign the string "Testy test test" to my hidden field on my aspx page. Then it does a postback to an invisible button I have that is within an updatepanel. The hidden button's click event fires, which throws up a msgbox displaying the contents of the hidden element, which invariably are "Testy test test".

Now here is where I'm having some problems. Of whatever I drop into the dropzone, instead of setting the hidden field's value to a static string, I want to set it to the ID of whatever it was I dropped into the dropzone. This is what I changed:

 $('[id$="myHidden"]').val($(ui.draggable).attr("id"));
    __doPostBack('<%= HiddenButton.UniqueID  %>', '');
    alert($('[id$="myHidden"]').val());

Now all of a sudden, when I drop something it stays glued to the dropzone, and no drop event fires. Did I break something? Or am I trying to get the ID of the dropped element the wrong way?

In case it matters, here is the relevant aspx page stuff:

<asp:Button ID="HiddenButton" runat="server" Text="Button" />

That button is typically hidden via css, I just left it unhidden for now for testing purposes.

EDIT!!!!:

I tried this as well, and it does not work (so I can rule out the 'var' thing?)

var foo = $(ui.draggable).attr("id")
    $('[id$="myHidden"]').val(foo);
    __doPostBack('<%= HiddenButton.UniqueID  %>', '');
    alert($('[id$="myHidden"]').val());
+1  A: 

In your drop function, you are not providing any formal parameters:

drop: function() {

should be

drop: function(ev, ui) {

I'm guessing this expression in your drop callback:

$(ui.draggable).attr("id")

is failing because the ui object cannot be referenced within the scope of the function.

karim79
Yikes, as soon as I put the (ev, ui) in there then the whole thing breaks. The ui.draggable.attr thing I jacked off some other StackO thread, it may not be the right way to get a dropped element's ID...
Bill Sambrone
How do I reference the dropped ui object from within the function? Changing to function(ev,ui) prevents dragging from happening, much less dropping.
Bill Sambrone
Ah, just figured it out. I forgot to add a semicolon, your suggested fix is spot on!
Bill Sambrone