In my .NET application I need to add a checkbox to each row in a dynamically created asp:Table.
Is it possible to do that by dynamically creating an asp:CheckBox for each row and somehow put it inside a TableCell object? (In that case how?)
Or do I need to replace the asp:table control with something else, like a Repeater control or GridView to make it work?
I'm looking for the quickest solution because I don't have much time.
Thanks in advance!
/Ylva
in aspx:
<asp:Table id=T1 runat=server />
in cs:
TableCell tc;
foreach(TableRow tr in T1.Rows)
{
tr.Cells.Add(tc = new TableCell());
((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
}
You do not want to do it on server side( in the cs as Yossarian said). because every time your page is reloaded or refreshed, you would have to recreate those checkboxes, which would mean new checkboxes every load, which also would mean your checkbox controls info will be lost because they are not on the client side, so all updated info done by the user (checkbox checked)will be lost, so you want be able to find out what is checked unless you add jquery in and it starts to get more complicated then it needs to be
if you are using webpages then it would be best to use asp:Gridview web control and bind the data to the table in code behind as so:
Gridview.Datasource=//ex:data;
Gridview.Databind();
As shown in the example on this page here
but if you are using MVC then you would add them in the client code in a form as so:
<% using (Html.BeginForm("Presentation", "Home")) %>
<% { %>
<table id="Table" class="color" width="100%" border="1">
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
<tr class="dxgvHeader_Glass">
<th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>
<th id="DateTime" runat="server"></th>
<th id="Description" runat="server"></th>
</tr>
</thead>
<tbody >
<%try
{ %>
<% foreach (var SamAuditLog in ViewData.Model)
{ %>
<tr>
<td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>
<td><%= data.DateTime%></td>
<td><%= data.Description%></td>
</tr>
<% } %>
<%} %>
</tbody>
Related
I am getting this error by simply trying to insert some VB.NET code in ASP.NET markup. See the code:
<%# Control Inherits="PerformanceWeb.Framework.SiteSettings" CodeBehind="sitesettings.ascx.vb" language="vb" AutoEventWireup="false" %>
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0" runat="server">
<% If EditDowntimeMode Then%>
<tr><td class="Normal"><asp:label id="lblDowntimeLegacyMode" Runat="server">lblDowntimeLegacyMode</asp:label></td></tr>
<tr>
<td class="DowntimeLegacyModeIndented" width="130">
<asp:label id="lblLegacyMode" Runat="server">lblLegacyMode</asp:label>
</td>
<td class="Normal" colSpan="2">
<asp:RadioButton id="rdoLegacyMode" GroupName="DowntimeLegacyMode" Runat="server"></asp:RadioButton>
</td>
</tr>
<tr>
<td class="DowntimeLegacyModeIndented" width="130">
<asp:label id="lblNewCauses" Runat="server">lblNewCauses</asp:label>
</td>
<td class="Normal" colSpan="2">
<asp:RadioButton id="rdoNewCauses" GroupName="DowntimeLegacyMode" Runat="server"></asp:RadioButton>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<% End if%>
</table>
Codebehind
#Region "Properties"
Public Property EditDowntimeMode() As Boolean
Get
Return m_EditDowntimeMode
End Get
Set(ByVal value As Boolean)
m_EditDowntimeMode = value
End Set
End Property
#End Region
When you add a runat='server' to an HTML control you change the rendering and code blocks aren't supported inside.
Changed from:
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0" runat="server">
to:
<table id="TABLE1" cellSpacing="0" cellPadding="2" border="0">
In the code behind on the Page Load, couldn't you just set the table's visible property to the negation of EditDowntimeMode ? That would seem to be a better solution than trying to mix all that markup together in what you are doing.
Something like this in the Page_Load method of the code behind:
Table1.Visible = Not EditDowntimeMode
The given example code in the question should be updated if that is the case as the example code has the if blocking out everything within the table. You could use an ASP:Panel block and control its visibility for another idea or possibly nest the tables so that an inner table could be just what the "EditDowntimeMode" would show is in its own table.
I have a view in ASP.NET MVC. It takes the model object and iterates over a list of strings and displays them in a table row, like so:
Details
<table>
<tbody>
<tr>
<th>Values in the database</th>
</tr>
<% foreach (string value in Model.lstDistinctValues)
{%>
<tr>
<%=value%>
<%} %>
</tr>
</tbody>
The problem is that the values appear ABOVE the header. So 'Values in the database' appears at the bottom, while the values are at the top.
Why would this be happening?
You need to add TD elements.
<table>
<tbody>
<tr>
<th>Values in the database</th>
</tr>
<% foreach (string value in Model.lstDistinctValues)
{%>
<tr>
<td><%=value%></td>
</tr>
<%} %>
</tbody>
</table>
I've ported a page from classic ASP to ASP.net. Part of what happens in this page is that a collection of custom types is generated and then displayed via Response.Write() commands. I'd like to get the business logic separated out into a code behind file (and maybe move this all into a user control), but I can't seem to figure out how I'd actually display the collection once it's been generated. I want to specify a master page here, too, so the code can't stay inline. Here's a very stripped down version of the current code:
<%
Dim objs as ArrayList = New ArrayList()
For i = 0 To 2
Dim obj as Obj = New Obj()
obj.setProp1("ASDF")
obj.setProp2("FDSA")
objs.Add(obj)
Next i
%>
<table>
<thead>
<tr>
<th scope="col">Property 1</th>
<th scope="col">Property 2</th>
</tr>
</thead>
<tbody>
<%
For Each obj As Obj In objs
Dim objProp1 As String = obj.getProp1
Dim objProp2 As String = obj.getProp2
%>
<tr>
<td><% Response.Write(objProp1)%></td>
<td><% Response.Write(objProp2)%></td>
</tr>
<%
Next
%>
</tbody>
</table>
What is the ".net" way of doing this?
You can also take a look at the ListView control which is a newer version of the repeater that Joe R mentioned. There's a great tutorial on what you can do with a ListView on ScottGu's blog.
Your code would basically turn into something along these lines:
<asp:ListView id="ListView1" runat="server" enableviewstate="false">
<LayouTemplate>
<table>
<thead>
<tr>
<th scope="col">Property 1</th>
<th scope="col">Property 2</th>
</tr>
</thead>
<tbody>
<asp:Placeholder runat="server" id="ItemPlaceholder" />
</tbody>
</table>
</LayouTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("objProp1" )%></td>
<td><%# Eval("objProp2" )%></td>
</tr>
</ItemTemplate>
</asp:ListView>
These guys consider using Eval to not be a good practice, but it made writing the example easier. If you're presenting read only data, don't forget to turn off ViewState or your pages will get very big very quickly.
EDIT Also found a feature comparison chart between the different list style controls here.
In .NET2 your best bet it the repeater. Something like this:
<asp:Repeater id="rpt1" runat="server" EnableViewState="false">
<HeaderTemplate>
<table>
<thead>
<tr>
<th scope="col">Property 1</th>
<th scope="col">Property 2</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("objProp1" )%></td>
<td><%# Eval("objProp2" )%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
and in your code behind you bind the data like this:
rpt1.DataSource = objs;
rpt1.DataBind();
This would be c# though - hope that's okay
I would use something like a repeater or datagrid and bind it to a List<> that you can create in your Business Logic layer.
I'm wondering what the best way to create text boxes in my loop here so that the IDs are unique instead of just "TextBox" for all of them. I need to add up the Price based on the Price per unit and the Quantity.
My items are loaded from a database so there could be any number of them, so I need to generate the fields on the fly. I was thinking maybe an array of fields could be used like in a form application, or is this not the right way to go?
I tried googling an answer, but maybe I'm just not wording my question well. It seems like this should be solved rather easily, but I just can't find the answer.
Here is my code:
<table class="proposal" width="800">
<tr>
<th>Item</th>
<th>Price per Unit</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<% int x = 0; %>
<% do
{
%>
<tr>
<td valign="top">
<%= this.name[x] %><br />
<%= this.desc[x] %></td>
<td valign="top" align="right"><%= "$" + this.price[x] %></td>
<td valign="top" align="center"><asp:TextBox ID="TextBox" runat="server" Width="75px"></asp:TextBox></td>
<td valign="top" align="right"></td>
</tr>
<% x++;
} while (x != this.y);
%>
</table>
You can't do a dynamic id for an asp.net TextBox. Could you use plain html <input type="text"> text boxes instead? Then you could assign whatever id you want.
Another approach would be to use a repeater control to generate your table. Each TextBox will then automatically get a unique id. If you need to get the ids in code, then in the repeater's ItemDataBound method, use FindControl to get a handle to the TextBox and read the ids/
You'll want to use the Repeater control as in this demonstration.
hows the best way to display comments/user data from a db using mvc?
do we just do our own for loop and manually display it or? will doing it this way cause paging problems in the future? any best methods or practice?
jquery?
thanks guys.
When I first started learning MVC, I put a lot of conditional code in the view to manage the display of the data and controls. However, once I figured out the benefit of HTML helpers, I found that it not only reduced the code in my views, but it also made it easier to test the resulting output.
As an example, this is a code fragment for an entry form that would appear in the finished HTML page:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><input id="LastName" name="LastName" value="Smith" /></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><input id="FirstName" name="FirstName" value="John" /></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><input id="JobTitle" name="JobTitle" value="Manager" /></td>
</tr>
</table>
Building the view using standard MVC helpers would result in code that looks something like this:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><%= Html.TextBox("LastName", Model.LastName) %></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><%= Html.TextBox("FirstName", Model.FirstName) %></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><%= Html.TextBox("JobTitle", Model.JobTitle) %></td>
</tr>
</table>
However, I was able to create HTML helpers that allowed me to make the view look like this:
<table class="listing">
<%= Html.Edit_LastName(Model.LastName) %>
<%= Html.Edit_FirstName(Model.FirstName) %>
<%= Html.Edit_JobTitle(Model.JobTitle) %>
</table>
Here are the HTML helpers:
private string Edit_Field(string prompt, string fieldName, string value)
{
string textBox = helper.TextBox(fieldName, value);
return string.Format("<tr><td class='prompt'>{0}</td><td>{1}</td></tr>", prompt, textBox);
}
public string Edit_LastName(this HtmlHelper helper, string value)
{
return EditField("Last Name:", "LastName", value);
}
public string Edit_FirstName(this HtmlHelper helper, string value)
{
return EditField("First Name:", "FirstName", value);
}
public string Edit_JobTitle(this HtmlHelper helper, string value)
{
return EditField("Job Title:", "JobTitle", value);
}
This method is not nearly as straightforward as the standard method, but if you have similar controls that are used on multiple views, this approach clearly helps with maintenance. For example, if you want to change the prompt text from "Job Title" to "Position", you just change the prompt string in the Edit_JobTitle method and the change is reflected in every view where that method is called.
is the best way to use client jquery
with tablesorter or server side for
loop?
I don't think there is a connection between these two. Anyways you have to use an iterator to get all the rows from the database. Then you can use jQuery plugin to view the data in pages.
Here is a nice implementation of this
Table Sorting, Paging and Filtering with jQuery
IMO, use the iterator. I use jquery plugins when I need a display format that is complex. jquery itslef is useful when you need to access/modify dom or make ajax calls. Paging shouldn't affect this decision or vice-versa.