将信息从JS传递到ASP服务器端

时间:2021-11-10 16:23:28

I've been trying to somehow get my javascript function (which I have in the head section of the aspx page) to set a value in a hidden item, then some how have the server-side (trusty 'ol ASP) read that data so I can do some work depending on what it is. I have my entire page wrapped in an update panel, and my drag 'n drop woes have mostly been solved. The ideal situation would be to have it so once an object gets dropped in the drop zone, the javascript chunk of code (I suck at JS) will assign a value to a hidden field, then make the update panel do one of its asynchronous postback and refresh itself (which will pull data from a function that is ran based on what the hidden value is). This is what I have for my current (broken) javascript section:

我一直试图以某种方式获取我的javascript函数(我在aspx页面的head部分中)在隐藏项中设置一个值,然后一些如何让服务器端(可靠的'ol ASP)读取该数据所以我可以做一些工作取决于它是什么。我的整个页面都包含在一个更新面板中,我的拖拽问题大多已经解决了。理想的情况是拥有它所以一旦一个对象被丢弃在drop区域中,javascript代码块(我吮吸JS)将为隐藏字段赋值,然后让更新面板执行其异步回发之一并刷新自身(它将从基于隐藏值的内容运行的函数中提取数据)。这就是我目前(破碎)的javascript部分:

<script type="text/javascript">  
  $(document).ready(function() {  
doReady();  

var prm = Sys.WebForms.PageRequestManager.getInstance();  
prm.add_endRequest(function(s, e) {  
    doReady();  
});  
}); 

All the above stuff makes it so JQuery doesn't bork itself after a postback

所有上述内容都使得JQuery不会在回发后自行解决问题

function doReady() {

$('.drag').draggable({ revert: true, helper: 'clone' });
$('.drop').droppable({
    tolerance: "touch", // Here should be a string
    drop: function() {

        $('#myHidden').val('Testy test test');
        __doPostBack('<%= HiddenButton.UniqueID  %>', '');
        alert(#myHidden.val);

    }


});

} // End of do ready
</script>

And here is the relevant ASPX part that has that mysterious "myHidden" thingy.

这是相关的ASPX部分,具有神秘的“myHidden”东西。

<input type="hidden" id="myHidden" />
<asp:Button ID="HiddenButton" runat="server" Text="Button" />   

Am I doing something wrong in my javascript section, or is the whole concept I have of this fubar?

我在我的javascript部分做错了什么,或者是我对这个fubar的整个概念?

Thanks! Bill

1 个解决方案

#1


If you want the hidden input to be posted back you need to give a name as well as an id. Only inputs with names are sent with the posted form. You may want to make it an asp:HiddenField so that you can easily retrieve its value on the server side, though you could get it from the Request.Form collection, too.

如果您想要回发隐藏的输入,您需要提供名称和ID。只有带有名称的输入才会与发布的表单一起发送。您可能希望将其设置为asp:HiddenField,以便您可以轻松地在服务器端检索其值,尽管您也可以从Request.Form集合中获取它。

<asp:HiddenField runat="server" id="myHidden" />

Change your javascript to be:

将您的javascript更改为:

drop: function() {

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

}

#1


If you want the hidden input to be posted back you need to give a name as well as an id. Only inputs with names are sent with the posted form. You may want to make it an asp:HiddenField so that you can easily retrieve its value on the server side, though you could get it from the Request.Form collection, too.

如果您想要回发隐藏的输入,您需要提供名称和ID。只有带有名称的输入才会与发布的表单一起发送。您可能希望将其设置为asp:HiddenField,以便您可以轻松地在服务器端检索其值,尽管您也可以从Request.Form集合中获取它。

<asp:HiddenField runat="server" id="myHidden" />

Change your javascript to be:

将您的javascript更改为:

drop: function() {

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

}