ASPX C#.NET Equivilent to ASP VBA Code

G

Guest

I have some legacy ASP code that looks like this:

<%
if not (Rs.EOF) then
i=0
do while not Rs.EOF
i=i+1
PaymentItemName = Rs("PaymentItemName")
PaymentItemCode = Rs("PaymentItemCode")
%>
<tr>
<td><input type="text" name="PaymentItemName_N<%=i%>" size="20"
value="<%=PaymentItemName%>" style="background:#D5FFC8"
<%=Use_permission%>></td>
<td></td>
<td><input type="text" name="PaymentItemCode_N<%=i%>" size="10"
value="<%=PaymentItemCode%>" <%=Use_permission%>></td>
</tr>
<%
loop
end if
%>

What would be the C#.NET equivilent?

I tried (snipped for ease of reading):

<td><input type="text" name="PaymentItemName_N<%=i;%>" size="20"
value="<%=PaymentItemName;%>" style="background:#D5FFC8"
<%=Use_permission;%>></td>

and I just get errors.
 
G

Guest

Thanks for the pointer.

I trawled through the results looking for how to use the <%# ... %> code to
set the property of a control, and after much trial and error this is what
I've found:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET 2.0 Projects</title>

<script language="C#" runat="server">
String[] urlArray = {"http://localhost/Corporate",
"http://localhost/FormViewData/employees.aspx",
"http://localhost/Membership",
"http://localhost/TestASP",
"http://localhost/Worklist"};

String[] textArray = {"Corporate Website",
"FormViewData",
"Membership",
"TestASP",
"Worklist"};

Int32 count;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>ASP.NET 2.0 Projects</h1>
<%
for (count = 0; count < urlArray.Length; count++)
{
Page.DataBind();
%>
<br />
<asp:HyperLink runat="server" NavigateUrl="<%# urlArray[count]
%>"><%=textArray[count]%></asp:HyperLink><br />
<%
}
%>
</div>
</form>
</body>
</html>

Only by using <%# urlArray[count] %> can I set the NavigateURL property of
the HyperLink control. And only by calling Page.DataBind() on every iteration
of the loop can I ensure that the urlArray[count] variable has the correct
value. If I don't, then it always returns urlArray[0].

I can also use the older ASP syntax <%=textArray[count]%>, but only when not
assigning it to a property. Using NavigateURL="<%=urlArray[count]%>" will set
the property to the literal string.

Why this is the case puzzles me. In older ASP code, the <%=...%> syntax is
often used to assign values to button text, dropdown list items, and control
names, but while the syntax is supported in ASPX, it doesn't work the same
way for control property assignments, meaning you have to rewrite all the ASP
code.

So this ASP code:

<td><input type="text" name="PaymentItemName_N<%=i%>" size="20"
value="<%=PaymentItemName%>" style="background:#D5FFC8"
<%=Use_permission%>></td>

Becomes this ASPX code:

<td><input type="text" name="<%# PaymentItemControlName(i) %>" size="20"
value="<%# PaymentItemName %>" style="background:#D5FFC8"
<%=Use_permission%>></td>

Where PaymentItemControlName() returns a String which is a concatenation of
"PaymentItemName_N" and i, PaymentItemName has to be an eval as it's assigned
to a property, but Use_permission doesn't as it's a string literal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top