简体   繁体   English

ASP.NET从Repeater获取所有复选框

[英]ASP.NET Get all checkboxs from Repeater

I used repeater to generate checkbox in a Content page, 我使用中继器在“内容”页面中生成复选框,

both checkbox has a unique ID, 两个复选框都有唯一的ID,

However, the checkboxs were generated by looping the string 但是,复选框是通过循环字符串生成的

<input id=\"Checkbox" + i+ "-" + j + "\" type=\"checkbox\" name=\"CollapseID" + i + "\" />

It can properly show in the webpage, with correct ID, and its name. 它可以正确显示在网页中,并带有正确的ID及其名称。 Moreover, I can control it via javascript. 而且,我可以通过javascript控制它。

I would like to ask, how can I know those checkbox is checked or not in server side? 我想问一下,我怎么知道那些复选框是否在服务器端被选中?

I have tried using the below code, but return null. 我尝试使用下面的代码,但返回null。

foreach (Control control in this.Page.Form.Controls)  
{  
    if (control is CheckBox)  
    {  
        string checkboxValue = ((CheckBox)control).Text;  
    }
}

Also, I tried: 另外,我尝试了:

Control chk = FindControl("Checkbox1-1"); // where "Checkbox1-1" is the checkboxID

and

HtmlInputCheckBox chk = (HtmlInputCheckBox)FindControl("Checkbox1-1");

but also return null... 但也返回null ...

Thanks for help! 感谢帮助!

you should write attribute as runat="server" in checkbox. 您应该在复选框中将属性写为runat="server"

then 然后

CheckBox cx= repeaterName.FindControl("checkboxId") as CheckBox;

I think it will work for you. 我认为它将为您工作。

if you are creating these checkboxes using string and then dumping them as HTML. 如果要使用字符串创建这些复选框,然后将其转储为HTML。 they will be treated as html check boxes and will not be accessible at server side. 它们将被视为html复选框,并且无法在服务器端访问。 you can get selected checkboxes in javascript only.. you can assign those values to any hidden server control and then use that in server side. 您只能在javascript中获得选中的复选框。您可以将这些值分配给任何隐藏的服务器控件,然后在服务器端使用它们。

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

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