【发布时间】:2015-12-24 09:42:30
【问题描述】:
我已经为此苦苦挣扎了好几天,试图访问 html 输入,即在运行时根据数据库结果生成的复选框或 textboxex。我作为文字控件添加到 asp 占位符的 HTML 如下所示:
<div class='cart-item'>
<div class='product-name'> <h3>gold baseball figure</h3></div>
<img src='Graphics/gold-trophy.png'></img>
<div class='inventory-short-description'> <p>A finely crafted gold plated baseball figuring to top your choice of trophey base (sold seperatly).</p></div>
<div class='clear'></div>
<div class='item-price'><p>$22.95</p></div>
<div class='cart-item-checkbox'><label><input type='checkbox' id='1' name='1' runat='server'/> Select </label>
<a href='products.aspx?viewItem=1'>view item </a></div></div
问题是,如何访问 .cs 代码隐藏页面中的此复选框?
生成此 html 并将其添加到页面的代码位于重写的 OnInit 方法中。查看回发的占位符表明该复选框位于文字控件中。
我试过了:
Page.FindControl() returns null when searching for dynamic control
http://forums.asp.net/t/1336244.aspx?finding+HTML+control
....还有无数其他人。
昨天我使用了一种骇人听闻的方式,使用 jquery 将这些复选框的值设置为 asp.net 隐藏字段。我的同事(所有 java 开发人员)说这似乎是访问这些元素的错误方法。有没有更好的办法?我开始认为我已经把自己编码到了一个我无法摆脱的地方。
感谢您的帮助。
【问题讨论】:
-
为什么不在你的aspx页面中使用asp控件?
-
不知道会从数据库返回多少结果。这些是在页面 Init 上添加的。
-
你可以使用asp:repeater控件,它会在Databound时重复模板中的控件
-
@ramhemasri 请解释一下。我从 db 返回的对象具有带有公共 get/set 方法的私有字段。我还能用这个吗?
-
runat="server" 应该足以从 .cs 文件访问!我是在 asp.net 控件中完成的