简体   繁体   English

asp:从codebehind访问repeateritem itemtemplate内部的html元素

[英]asp: access html element inside repeateritem itemtemplate from codebehind

How to access HTML "input type="checkbox" data-item="myItems" id="id123"" in ascx.cs file when the input is in asp:repeater ? 当输入在asp:repeater中时,如何在ascx.cs文件中访问HTML“ input type =” checkbox“ data-item =” myItems“ id =” id123“”? This does not work, cuz findcontrol only works with system.web.ui.control: 这不起作用,因为cuz findcontrol仅适用于system.web.ui.control:

var checkBox = (System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Item.FindControl("id123");



This: 这个:

var checkBox = (Checkbox)e.Item.FindControl("id123");

will work if I make asp:checkbox instead html input. 如果我使asp:checkbox而不是html输入将可以工作。
But then another question: What is the equivalent for html input "data-" attribute in asp:checkbox ? 但是,还有另一个问题:asp:checkbox中html输入的“ data-”属性的等效项是什么?

Answer to first part : 回答第一部分

You need to set runat="server" on the html input. 您需要在html输入上设置runat="server"

<input type="checkbox" id="id123" data-item="myItems" runat="server".....

Answer to second part : 第二部分的答案

You can specify any attribute on server controls and they will be spewed as is in the generated html. 您可以在服务器控件上指定任何属性,它们会像生成的html一样喷出。 So you can have: 因此,您可以:

<asp:checkbox id="id123" data-item="myItems" runat="server"....

您需要在要从背后的代码访问的任何元素中包含runat="server"属性。

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

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