简体   繁体   English

Asp.NET没有获得由javascript设置的自定义属性的更新值

[英]Asp.NET not getting updated values of custom attributes set by javascript

This problem has been bugging me and any explanation of why this is would help. 这个问题一直困扰着我,并解释为什么这会有所帮助。

Given the following DOM: 鉴于以下DOM:

<body>
<form id="form1" runat="server">

    <div id="myDiv" runat="server" data-type="Monkey">
        Choose your inner animal!
        <ul>
            <li class="selected">Monkey</li>
            <li>Tiger</li>
            <li>Dog</li>
            <li>Bunny</li>
        </ul>
    </div>

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

</form>
</body>
<script src="Assets/js/jquery-1.9.0.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {

    $('li').bind('click', function () {
        $('li.selected').removeClass('selected');
        var animal = $(this).addClass('selected').text();

        //$('#myDiv').data('type', animal);
        $('#myDiv').attr('data-type', animal);
    });

});
</script>

Code Behind: 代码背后:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
    Dim type As String = myDiv.Attributes("data-type")

    MsgBox(type)
End Sub
End Class

The MsgBox always comes back with the default value of 'Monkey'. MsgBox 总是返回默认值'Monkey'。 Now i did implement a workaround where I stored the value in a Hidden field, then recalled the value on button click and that works. 现在我确实实现了一个解决方法,我将值存储在隐藏字段中,然后调用按钮单击的值,这是有效的。 I did try an update panel but no luck, but that doesn't really matter because I hate update panels and never use them anyways. 我确实尝试过更新面板但没有运气,但这并不重要,因为我讨厌更新面板而且从不使用它们。

This is a limit - if you want to call it that - of HTML and the way things post back. 这是一个限制 - 如果你想称之为 - HTML和事情回发的方式。 It's not even an ASP.NET limit, as far as I am aware: it goes beyond that. 据我所知,它甚至不是ASP.NET的限制:它超越了它。

Only certain tags have their values posted back to the server. 只有某些标签将其值发布回服务器。 I'm thinking it's just INPUT and SELECT tags, although there may be one I'm not thinking of. 我认为它只是INPUT和SELECT标签,虽然可能有一个我没想到的。

Attributes in tags don't even get posted back: just values. 标签中的属性甚至不会被回发:只是值。

So the server doesn't know anything about what you've changed on the client, unless it's what's selected in a SELECT or the value of an INPUT. 因此服务器不知道您在客户端上更改了什么,除非它是在SELECT中选择的内容或INPUT的值。 Everything in ASP.NET, everything that gets reconstructed from ViewState and postback data, is based on those tags. ASP.NET中的所有内容,从ViewState和回发数据重建的所有内容都基于这些标记。

So your hidden field is the only way what you want can happen. 所以你隐藏的领域是你想要的唯一方式。

In case of SELECT, if the "disabled" attribute is set to true in JQuery then in ASP .Net code-behind, you will not get the correct values but instead, you will get the first item in the list as selecteditem and it's corresponding value as selectedvalue. 在SELECT的情况下,如果在JQuery中将“disabled”属性设置为true,那么在ASP .Net代码隐藏中,您将无法获得正确的值,而是将获得列表中的第一个项目作为selecteditem并且它是相应的值为选定值。 Same is the case for INPUT as well. INPUT的情况也是如此。

I think the simplest option is to use the hidden fields, populating them in JQuery and using their values in code-behind. 我认为最简单的选择是使用隐藏字段,在JQuery中填充它们并在代码隐藏中使用它们的值。

Another common reason is if you rebuild the dropdown list in code behind on the page_load function without checking that it's not IsPostBack first. 另一个常见原因是,如果您在page_load函数中重建代码后面的下拉列表,而不检查它是否首先不是IsPostBack This would cause you to reset the value each time to the default/first item. 这将导致您每次都将值重置为默认/第一项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM