using render block to set a server control attribute

  • Thread starter Thread starter Temo
  • Start date Start date
T

Temo

Can someone tell me what's wrong with this code?

<%
int width=100;
%>
<asp:TextBox runat="server" id="boom" width="<%=width%>"></asp:TextBox>
 
Your post went unanswered. Have you resolved this issue? If you still need
help, please post the original question with your request.
 
Hi Alvin,
This was the original question:

Can someone tell me what's wrong with creating a Textbox in this fashion?
<%
int width=100;
%>
<asp:TextBox runat="server" id="boom" width="<%=width%>"></asp:TextBox>


Thanks,
Temo
 
technically there is nothing wrong with it. theoretically, you should move
away from inline coding and develop with the code behind concept. there are
several advantages of using the codebehind which are not available using
inline methods
 
Actually, I get an error when I try this. I suspect it has something to
do with using a renderblock variable to set the attribute of a server
control.

Here's the exact code I use.
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<% int width=1; for(int i=0; i<5; i++, width=i*10) {%>
<tr>
<td>Line <%=i%> </td>
<td><asp:TextBox runat="server"
Width="<%=width%>"></asp:TextBox></td>
<td>Width = <%=width%></td>
</tr>
<%}%>
</table>
</form>
</body>
</HTML>
and I get the following error message:
Parser Error Message: '<%=width%>' can not be parsed as a unit as there
are no numeric values in it. Examples of valid unit strings are '1px'
and '.5in'

As for code behind, I do most of my programming in code behind. I need
to use inline code in this situation cause I want to set the width of a
textbox which is embedded inside a datalist. Yes, I could always use
codebehind, to go through each row in my datalist once its databound and
then set the widths of the textbox. But I think this is simpler to do,
if it would work :)

Thanks.
Temo
 
I was wrong on this one.

The declaration must take a numeric constant value. Width is a variable.

What are you trying to accomplish?

If you need to adjust the width of the textbox at run-time, set it to a
default value, and then adjust the width from the codebehind like
TextBox1.width = width;
or use the attributes property to reset the width.
 
Back
Top