ASP.NET MVC - checkbox text attribute

I am drawing some checkboxes in a loop, and I want to set a text attribute based on the objects that I am iterating with the loop.

I have something like this:

<asp:CheckBox ID=

"CheckBox1 " runat=

" server " Text=

" <%= Html.Encode(item.nome) %>

" Checked = "true" / ">

The problem is that Html.Encode (item.nome) appears as plain text and if I don't use quotes I get an error.

0


a source to share


3 answers


Alternatively, use a helper Html.CheckBox

.



<%= Html.CheckBox( "CheckBox1", true ) %> <%= Html.Encode(Item.none) %>

      

+8


a source


Don't use a control <asp:CheckBox>

- create a standard html checkbox:



<input type="checkbox" name="cb" checked="checked"><%= Html.Encode(item.nome) %></input>

      

+3


a source


You cannot mix ASP.NET control tags with syntax <%= %>

. You have two options:

Use raw HTML for your checkbox, then you can use <%= %>

just fine. This style is better suited for ASP.NET MVC.

<input type="checkbox" name="cb" checked="checked"><%= Html.Encode(item.nome) %></input>

      

Or you can use ASP.NET compatible data binding syntax:

<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Html.Encode(Container.DataItem, "nome") %>' Checked="true"/> 

      

But to use the data binding syntax, you need a data source control and be inside a Repeater control. See ASP.NET Data Binding for details .

+1


a source







All Articles