简体   繁体   English

Asp.Net数据属性通过JS / jQuery动态更新,但通过后面的代码检索时更新的属性不会更新

[英]Asp.Net data attributes dynamically updated through JS/jQuery but the updated attributes aren't updated when retrieved through code behind

I have an aspx page of images that, when selected, a popup appears prompting for various information tid bits that I then store the information as data attributes on hidden labels through use of jQuery (ie data-id="####" , data-difficulty="###" , etc.). 我有一个aspx页面的图像,当选择时,会出现一个弹出窗口,提示各种信息tid位,然后我通过使用jQuery将信息作为数据属性存储在隐藏标签上(即data-id="####"data-difficulty="###"等)。 I acknowledge that this isn't the best way to do it necessarily but I've tried other things (see below) and nothing has worked yet. 我承认这不是最好的方法,但我已经尝试了其他的东西(见下文),但还没有任何工作。

I've been attempting, and to no avail, to retrieve the dynamically updated data attributes so the various items can be stored to my local ms sql database. 我一直在尝试,无济于事,检索动态更新的数据属性,以便各种项目可以存储到我的本地ms sql数据库。 The updating of the attributes works perfectly in that I can view the items being updated properly in Chrome's developer tools. 更新属性非常有效,因为我可以在Chrome的开发者工具中正确查看正在更新的项目。 Despite this when I try to pull the same attributes I can see as being updated I'm unable to retrieve the updated values in the code behind and keep getting back the initial values (generally an empty string ""). 尽管如此,当我尝试拉出相同的属性时,我可以看到它被更新,我无法在后面的代码中检索更新的值并继续获取初始值(通常是空字符串“”)。

Here's the implementation on the aspx page: 这是aspx页面上的实现:

<asp:Label runat="server" ID="lblSelection" data-id="" data-count="" data-price="" data-difficulty=""  CssClass="selected-items" />

and here's the relevant method being called when the "Submit" button is clicked further down on the same page: 这是在同一页面上进一步向下点击“提交”按钮时调用的相关方法:

protected void SubmitClicked(object sender, EventArgs e)
{
    var currentID = lblSelection.Attributes["data-id"];
    var currentCount = lblSelection.Attributes["data-count"];
    var currentPrice = lblSelection.Attributes["data-price"];
    var currentDifficulty = lblSelection.Attributes["data-difficulty"];

    if (currentID == null || currentID == "")
    {
              // stop and throw an informative message to the user
    }else{
             // keep processing ...
    } 
}

The trouble is that when I run the project in debug mode and inspect those elements (again, making sure that I can visually see that the attributes are actually updated in the developer tools) they're all the initial value of "". 麻烦的是,当我在调试模式下运行项目并检查这些元素时(再次确保我可以直观地看到属性在开发人员工具中实际更新),它们都是“”的初始值。 My only guess is that there's some kind of post back issue but I wouldn't think that would happen until my method had been called and fully processed. 我唯一的猜测是,有一些回发问题,但我不认为这会发生,直到我的方法被调用和完全处理。 As a note, I'm populating the images onto the page and updating their attributes already through a sql call based on the id of the item: 作为一个注释,我正在将图像填充到页面上,并根据项目的ID通过sql调用更新其属性:

<img runat="server" src="" data-id="12345" />

The initial loading all works perfectly so I can clearly set the properties from the code behind fine. 初始加载都完美无缺,因此我可以从代码中清楚地设置属性。 For whatever reason though I am unable to pick up the updated attribute values in the code behind after the jQuery updates the label's attributes (following some clicking and whatnot). 无论出于何种原因,虽然在jQuery更新标签的属性(点击一些点击等等)后,我无法在后面的代码中获取更新的属性值。 Any tips or thoughts on this would be greatly appreciated. 任何有关此的提示或想法将不胜感激。

What you are trying to do cannot work because: 你想要做的事情是行不通的,因为:

  1. The value of custom attributes is not posted back to the server (as discussed here ). 自定义属性的值不回发到服务器(如讨论这里 )。

  2. Even if you set the text of the Label in client code, it would also not be available in the Text property in code-behind. 即使您在客户端代码中设置Label的文本,它也不会在代码隐藏中的Text属性中可用。 A Label is rendered as a span element in the page, and the content of that type of element is not posted back to the server. Label在页面中呈现为span元素,并且该类型元素的内容不会发布回服务器。

A nice list of properties included in a postback is given in this topic: Which values browser collects as a postback data? 本主题中给出了回发中包含的一个很好的属性列表: 浏览器收集哪些值作为回发数据?

As suggested by mshsayem, you should use HiddenFields. 正如mshsayem所建议的那样,你应该使用HiddenFields。 If, for some reason, you want to do it differently, you could use hidden TextBoxes, set their value in client code, and retrieve it with the Text property in code-behind. 如果出于某种原因,您希望以不同方式执行此操作,则可以使用隐藏的TextBox,在客户端代码中设置它们的value ,并使用代码隐藏中的Text属性检索它。 In other words: HiddenFields are not the only controls for which a value set in client-code can be retrieved in code-behind (but they are the obvious choice if the control is not to be displayed). 换句话说:HiddenFields不是唯一可以在代码隐藏中检索客户端代码中设置的值的控件(但如果不显示控件,它们是显而易见的选择)。

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

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