I have the following code in a ascx : I run a SQL query and I put it in a Datatable. Then I show the values in the page like this
<% For each row As DataRow In myDataTable.Rows %>
<%=row("something") %>
<%=row("somethingelse") %>
<asp:CheckBox ID="CheckBox1" runat="server" />
<% next %>
Now… how can I set the ID of the checkbox dynamically?
something like
<asp:CheckBox ID="<%=row("MyId")%>" runat="server" />
that obviously do not work.
I need to set the id as the value I get from the DB so if it is checked I can pass the checked id to another page.
2
Answers
The problem is that
row("MyId")
returns type ofobject
so you need to convert it tostring
to be able to bind it to CheckboxSee: DataRow.Item[] Property
Hum, would not a data control say like listview, or gridview be better here?
You can have this markup:
Now to load we have this:
and we get this:
So, it is a lot less work to do the above.
Even if you don’t have a bound check box, you can even feed the grid the data, and display a un-bound check box for selecting rows if that what you need/want.