asp:HiddenField的jQuery更新无法在回发中持久化 [英] jQuery update of asp:HiddenField fails to be persisted across postback

查看:92
本文介绍了asp:HiddenField的jQuery更新无法在回发中持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sitecore(ascx控件)中有一个子布局,它包含一个隐藏字段和一个图像映射。根据单击的图像映射区域,我想设置隐藏字段的值,然后启动回发到服务器。



我的标记为如下:

 < script type =text / javascript> 
函数SetRegion(regionName){
$('#<%= hdnRegion.ClientID%>')。val(regionName);
__doPostBack('<%= lnkRedirectButton.UniqueID%>','');
}
< / script>

< img src =/ Images / Interface / Map-Background.pngusemap =#regionMapViewclass =map/>
< map id =regionMapViewname =regionMapView>
< area id =xhref =javascript:SetRegion('x')shape =polycoords =,,,,/>
< area id =yhref =javascript:SetRegion('y')shape =polycoords =,,,,/>
< area id =zhref =javascript:SetRegion('z')shape =polycoords =,,,,/>
< / map>

< asp:HiddenField ID =hdnRegionrunat =server/>
< asp:LinkBut​​ton ID =lnkRedirectButtononclick =lnkRedirectButton_Clickrunat =server/>

我的代码隐藏如下:

  protected void lnkRedirectButton_Click(object sender,EventArgs e)
{
string region = hdnRegion.Value; //在这一点上,我希望hdnRegion.Value为xy或z,但它总是空回来:-(
//做东西......
}

我添加了虚拟LinkBut​​ton作为回发的钩子(创建一个代码隐藏方法来处理回发)。



我在一个独立的web应用程序中尝试过这个代码,它看起来工作正常,所以我想知道是否有什么事情发生了sitecore会在回发过程中弄乱隐藏字段的值吗?



澄清一下;回发事件在服务器上被触发并处理好了我已经使用过警报()在javascript中确认隐藏字段值设置正确。



修改:
重新提问包括jquery- ui tag。
这个问题似乎与以下事实有关:所有这一切都发生在一个div中,在jQuery UI库中使用$()。dialog()方法显示...


解决方案

正如我在编辑中所提到的,这个问题与隐藏字段(以及其他所有内容)位于使用jQuery UI对话框操作的div内部有关创建弹出窗口的方法。



我发现了这个问题(由javascript修改的HiddenField值不会被回发
这给了我答案并涉及在open中添加一行代码:callback in对话框方法构造函数。



我打开对话框的JavaScript代码修改如下:

  $('#MyDialog')。dialog({
closeOnEscape:true,
open:function(event,ui){
$(this).parent() .appendTo( 形式); //这条线排序了一切!
}
});

报价 DanaBenson 我认为根本原因是jQueryUI将对话框附加到BODY标记而不是FORM标记,其中.Net需要它。


I have a sublayout in sitecore (which is an ascx control) which contains a hidden field and an image map. Depending on the area of the image map which is clicked, I want to set the value of the hidden field, then initiate a postback to the server.

My markup is as follows:

<script type="text/javascript">
function SetRegion(regionName) {
    $('#<%=hdnRegion.ClientID%>').val(regionName);
    __doPostBack('<%=lnkRedirectButton.UniqueID%>', '');
}
</script>

<img src="/Images/Interface/Map-Background.png" usemap="#regionMapView" class="map" />
<map id="regionMapView" name="regionMapView">
    <area id="x" href="javascript:SetRegion('x')" shape="poly" coords=",,,," />
    <area id="y" href="javascript:SetRegion('y')" shape="poly" coords=",,,," />
    <area id="z" href="javascript:SetRegion('z')" shape="poly" coords=",,,," />
</map>

<asp:HiddenField ID="hdnRegion" runat="server" />
<asp:LinkButton ID="lnkRedirectButton" onclick="lnkRedirectButton_Click" runat="server" />

and my code-behind is as follows:

    protected void lnkRedirectButton_Click(object sender, EventArgs e)
    {
        string region = hdnRegion.Value; //at this point, I expect hdnRegion.Value to be "x" "y" or "z" but it's always coming back empty :-(
        //do stuff...
    }

I added the dummy LinkButton to use as a hook for the postback (to create a code-behind method to handle the postback).

I've tried this code in a stand-alone webapplication and it seems to work fine, so I'm wondering if there is something going on with sitecore which is messing up the hidden field's value during the postback process?

To clarify; the postback event is being fired and handled on the server ok and I've used alert() in javascript to confirm that the hidden field value was being set correctly.

Edit: Retagged question to include jquery-ui tag. This issue seems to be related to the fact that all of this is happening inside of a div which is displayed using $().dialog() method in the jQuery UI library...

解决方案

As mentioned in my edit, this issue is related to the fact that the hidden field (and everything else) is inside a div which is manipulated using the jQuery UI dialog method to create a popup window.

I found this question (HiddenField value modified by javascript is not posted back) which gave me the answer and involves adding a single line of code to the open: callback in the dialog methods constructor.

My JavaScript code for opening the dialog was modified as follows:

$('#MyDialog').dialog({
    closeOnEscape: true,
    open: function(event, ui) {
        $(this).parent().appendTo("form"); //this line sorted everything!
    }
}); 

To quote DanaBenson "I believe the root cause is that jQueryUI attaches dialogs to the BODY tag not the FORM tag, where .Net needs it to be."

这篇关于asp:HiddenField的jQuery更新无法在回发中持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆