开发者

How to have an ASP checkbox for each entry in a list?

I want to do something like this in my asp code:

<%
 foreach Record record in listOfRecords
{
 %>
<asp:checkbox runat="server" id="employeeIdNumber" />
<p>Employee's Name: <%= record.name %> </p>
<p>Employee's Phone Number: <%= r开发者_如何学Pythonecord.phoneNumber %></p>
<%
}
%>

The problem is that the checkbox id is a string literal. How can I give a unique id to each employee's checkbox?


Wrap this in a repeater. Then bind your listOfRecords to the repeater.

<asp:Repeater runat="server">
  <ItemTemplate>
    <asp:checkbox runat="server" id="employeeIdNumber" />
    <p>Employee's Name: <%# Eval("name") %> </p>
    <p>Employee's Phone Number: <%# Eval("phoneNumber") %></p>
  </ItemTemplate>
</asp:Repeater>

Then to get it out, you run through the RepeaterItems collection and look for the checkboxes by RepeaterItem.FindControl("employeeIdNumber") to determine if they are checked.


I think you should use Repeater instead.

<asp:Repeater ID="rptEmployees" runat="server">
    <ItemTemplate>
        <asp:CheckBox ID="employeeIDNumber" runat="server" />
    </ItemTemplate>
</asp:Repeater>

This Control will render unique ID for each checkbox for you.


CheckboxList is an option too.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx

This also has an example on how to check the checked state - just iterate through and check the Selected property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜