在后面的代码中通过 jquery 添加列表框项目时出现问题 [英] Problem getting list box items added through jquery in code behind

查看:15
本文介绍了在后面的代码中通过 jquery 添加列表框项目时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp.net 列表框控件,我在其中使用 Jquery 填充项目,方法是使用一些代码,例如 ..$("#MylistBox").append("<option value='somevalue'>Someitem</option>

I have a asp.net list box control in which i populate items using Jquery by using some code like .. $("#MylistBox").append("<option value='somevalue'>Someitem</option>

动态地.但是在我使用的代码后面MylistBox.Items 总是显示 Count 0 无论添加多少项目.

dynamically . but in code behind when i use MylistBox.Items is always showing Count 0 no matter how much items add.

谁能帮我解决这个问题?

Can anybody help me with this?

推荐答案

在不知道实际情况的情况下...我假设您的目标实际上是通过迭代它们或其他方式来获取动态添加的项目...

Without knowing the actual scenario... I am assuming your goal is actually to get the dynamically added items either by iterating over them or something else...

任何在客户端使用 JavaScript/jQuery 动态添加的 DOM 元素都不会自动反映到服务器端.您将需要以不同的方式对它们进行序列化,并在回发期间将它们推回服务器端.一种方法是在隐藏输入中序列化 Select 元素的所有选项.如果您希望更容易访问,可以将隐藏的输入标记为 runat=server,否则使用 Request.Form["...hidden input NAME attribute here... NOT ID..."] 来获取出价值.得到它之后,你可以对这些值做任何你想做的事情.

Any dynamically added DOM element that is done on the client side using JavaScript / jQuery, will not be reflected automatically to the server side. You will need to serialize them in a different way and push them back to the server side during postback. One way you can do this is by serializing all the Options of the Select element in a hidden input. You can mark the hidden input as runat=server if you wish to make it easier for you to access, otherwise use Request.Form["...hidden input NAME attribute here... NOT ID..."] to get the value out. After you get it, you can do whatever you want with the values.

我想您的隐藏输入应该有一些值,例如:1:第一个值,2:第二个值,3:...".只需进行一些字符串操作即可将它们拆分并迭代它们.

I imagine your hidden input should have some value like: "1:First Value,2:Second Value,3:...". Just do some string manipulation to split them up and iterate over them.

这篇关于在后面的代码中通过 jquery 添加列表框项目时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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