"Code blocks are not supported in this context" error in ASP.NET - asp.net

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.

Related

Horizontally tile tables in listview?

I have records from database separated by department, I want to tile them horizontally inside different tables (for each department). Here is what I tried (This does not work)
Aspx
<asp:ListView ID="lvUnderwriting" GroupItemCount="6" runat="server" GroupPlaceholderID="grpPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table>
<asp:PlaceHolder ID="grpPlaceHolder1" runat="server" ></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<span><b><%# Eval("gensubbusiclass") %></b></span>
<table>
<tr>
<th>Role Name:</th>
<th>Pending Tasks:</th>
<th>On Leave:</th>
</tr>
<tr>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
</tr>
</table>
</GroupTemplate>
<ItemTemplate>
<tr>
<td><span> <%#Eval("RoleName") %></span></td>
<td><span><%# Eval("Count") %></span></td>
<td><span><%# Eval("OnLeave") %></span></td>
</tr>
</ItemTemplate>
</asp:ListView>
Database rows
My current results. For some reason the second department is divided into two tables. Also how do I tile them horizontally?
Okay what I got was that ListView cannot display master detail records out of the box. Here is what I tried to get it working for me. The trick was to dynamically add a new row to the table if the header databound item was changed, the database records need to be sorted in advance to get this working. Referenced from here using Asp.net 3.5 ListView 4 Guys from Rolla
Aspx
<asp:ListView ID="lvUnderwriting" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table border="1" cellpadding="4px">
<tr>
<th>Role Name:</th>
<th>Assigned:</th>
<th>On Leave:</th>
</tr>
<tr>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server" />
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<%# AddGroupUnderwriting() %>
<td><%# Eval("RoleName") %></td>
<td><%# Eval("Count") %></td>
<td><%# Eval("OnLeave") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
C#
// Add group row to underwriting grid
private string prevUWBusinessClass = "";
public string AddGroupUnderwriting()
{
string currentUWBusinessClass = Eval("gensubbusiclass").ToString();
if (currentUWBusinessClass != prevUWBusinessClass)
{
prevUWBusinessClass = currentUWBusinessClass;
return string.Format(#"<tr><td colspan=""3""><h3>Department: {0}</h3></td></tr>", currentUWBusinessClass);
}
else
return "";
}

Hide tr or td in a table using vb.net in codebehind

In my masterpage I have links for users also the authorization is different like admin and regular user.
links under each other and I can hide the hyperlinks depending on authorization status but the problem is i.e when I have 3 links the second link for the admin the link will hide when the user is regular and the link place empty like 123 1 3.
So I have an idea using table each link in one tr but I can`t hide td or tr because Visible is not in properties.
any help?
thank you
According to how to hide a having asp.net control:
you can give ID either to the TD or TR to which you want to hide/show
with the runat="server" and also you can take that tr/td inside the
div tag and give id to that div tag and also runat=server attribute
and after that you can pro grammatically hide/show that div.
like
<pre>
<tr id="trhide" runat="server"> </tr>
</pre>
in code behind write
trhide.visible=true/false
In the master page VB code behind add a public procedure: Then call the public set from your aspx page.
'======================================================================================================
'Set Tab No invisible
'======================================================================================================
Public Sub setTabNumberLabel(visible As Int16)
If visible = 0 Then
td_page.Visible = False
Else
td_page.Visible = True
End If
End Sub
The master aspx would be:
<table style="width:100%">
<!--<tr style="background-color:#565656;">-->
<tr>
<td style="width:15%;text-align:left;vertical-align:bottom;padding-left:20px;">Stategic Energy Assessment ( <asp:Label ID="lbl_year_ended" runat="server" /> )</td>
<td style="text-align:center;vertical-align:bottom;"><asp:Label ID="lbl_utility_name_and_id" runat="server" /></td>
<td id="td_page" runat="server" style="width:15%;text-align:right;vertical-align:bottom;padding-right:20px;">Tab No: <asp:Label ID="lbl_page" runat="server" /></td>
</tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
<tr>
<td style="width:15%;text-align:left;vertical-align:central">
<asp:Label ID="lbl_print_version" runat="server" Text="View Printable Vision" Visible="false" />
</td>
<td style="font-size:larger; font-weight:bold; text-align:center; text-transform:capitalize;vertical-align:central">
<asp:Label ID="lbl_schedule_name" runat="server" />
</td>
<td style="width:15%;text-align:right;vertical-align:central;padding-right:20px;">
<asp:LinkButton ID="btn_footnotes" runat="server" Visible="false">Footnotes</asp:LinkButton>
</td>
</tr>
<%--<tr><td colspan="3" style="vertical-align:central" class="auto-style1"></td></tr>--%>
<tr><td colspan="3" style="vertical-align:central; padding-right:20%;padding-left:20%; ">
<i><asp:Label ID="lbl_headnotes" runat="server" Text="" /></i></td></tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
</table>
The other answer is correct and works fine. Just adding complete piece of code.
It's quite amusing that you don't need to add runat=server for a table but you can still hide tr for that table using runat attribute.
<table>
<tr>
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide1" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide2" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr>
<td>aa</td><td>bb</td>
</tr>
</table>
Now just set properties in codebehind (hiding the tr)
trHide1.Visible = false;
trHide2.Visible = false;

How to add blank row in grid view?

I am new to the ASP.NET i am binding one list of data object to the grid view. I want to display blank row after each record in grid view so i have done this by as below in code behind
List<DatabaseDTO> lstdatabase= new List<DatabaseDTO>();
foreach(int jobNumber in JobnumberList)
{
DatabaseDTO dataObject = new DatabaseDTO();
dataobject = GetDatabaseData(jobNumber);//Method to retrieve data and return data object
lstdatabase.Add(dataObject);
lstdatabase.Add(new DatabaseDTO());
}
gridView.DataSource = lstdatabase;
gridView.DataBind();
it's working correct i am getting the desired blank row in the grid view but i know this is not right way because i am adding object to the list so i can add the blank row in place of that i would very much like to adjust this blank row from the aspx page. I know there is another way using the DataTable but it is also not very good because it also adds the unnecessary records to the DataTable. So any other work around or way to solve this would be very great. Thank you.
Try This
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderTemplate>
<table width="900px">
<tr>
<td width="300px">
<b>Name</b>
</td>
<td width="300px">
<b>Account No</b>
</td>
<td width="300px">
<b>Company</b>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="900px">
<tr>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "AccountNo")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Company")%>
</td>
</tr>
<tr>
<td align="left" width="300px">
<br />
</td>
<td align="left" width="300px">
<br />
</td>
<td align="left" width="300px">
<br />
</td>
</tr>
</table>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<SeparatorTemplate><br /></SeparatorTemplate>
</asp:DataList>
</div>
You can't have an empty row in the Datagrid if it isn't present in the data source. You have to think that after all the grid data is just a representation of your data source, if there is a empty row, the grid will show it, if there is not, it wont.
Write a stored procedure to get Output Parameter from sql server and bind to grid view if record is not there..

display and hide particular column in repeater?

I have a repeater. And i want to hide and display a particular column for a particular condition. I have three types of subjects and their ids are 0,1,2 respectively. Now i want to show that particular column when the subject will be 2 only..
My code is :-
<table id="table1" class="yui" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
EmpID #
</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repaddressorbbl" runat="server" OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<td>
<asp:LinkButton ID="lknumber" runat="server" Text="Edit" CommandName="subjectid" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
</tfoot>
</table>
You could catch the OnItemDataBound event of the repeater and hide the column there if the (subject) item id is 2.
In order you can get a reference to the column, make it a server control:
<td style="text-align: center;" id="COL_TO_HIDE" runat="server"><%#Eval("empid")%></td>
Then in the repeater event you can simply look for the control and hide it:
protected void YourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var subject = (Subject)e.Item.DataItem;
if (subject.Id == 2)
{
var col = e.Item.FindControl("COL_TO_HIDE");
col.Visible = false;
}
}
}
Please note, this is just a simplified example which should you get started.
I think you should start by using <HeaderTemplate></HeaderTemplate> and <FooterTemplate></FooterTemplate> to define the start and end of your table just to tidy it up.
You can get the table to run on the server by adding a runat="server" and give the column <td> an id and a runat="server" attribute so you can program server code against it. I'd then eval bind the visible attribute of the cell based on your field value or use attributes.add("display:none") or just use a grid view as suggested in the link.
<asp:Repeater ID="Repaddressorbbl" runat="server"
OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empid")%>
</td>
<% if (false){ %>
<td>
<asp:LinkButton ID="lknumber" runat="server"
Text="Edit" CommandName="subjectid" />
</td>
<% } %>
</tr>
</ItemTemplate>
</asp:Repeater>

What is the best practice for ouputting data from a collection on an ASP.net Page?

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.

Resources