asp:TableCell & Background image

  • Thread starter Thread starter Brian W
  • Start date Start date
B

Brian W

OK, I give up!

How are you **supposed** to set the background image of a single
asp:TableCell or a asp:TableRow ?

TIA
Brian W
 
Hi Brian,

As for the problem that "setting back ground image for a ASP.NET Table 's
TableRow or TableCell", I think we need to set it by code:

Every ASP.NET Server Control has encapsulated its related HtmlControl's
Attributes and Css Sytle as two Collectons
"Attributes" and "Style" collections, since in Html table element we use
the following means to set background image:
<TR style="BACKGROUND-IMAGE:
url(http://support.microsoft.com/library/images/support/bnr_microsoft.gif)">
<TD
background="http://support.microsoft.com/library/images/support/bnr_microsof
t.gif">fsdfds</TD>

Then, in ASP.NET Table control we can use the following code to specify the
background image:

aspTable.Rows[0].Style.Add("BACKGROUND-IMAGE","url(images/tr.gif)");
aspTable.Rows[0].Cells[0].Style.Add("BACKGROUND-IMAGE","url(http://support.m
icrosoft.com/library/images/support/bnr_microsoft.gif)");

aspTable.Rows[0].Cells[0].Attributes.Add("background","http://support.micros
oft.com/library/images/support/bnr_microsoft.gif");

In addition, here are the reference on ASP.NET server control's attributes
an sytles collections in MSDN:
#AttributeCollection Members
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIAttribute
CollectionMembersTopic.asp?frame=true

#ASP.NET Server Controls and CSS Styles
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconwebformscontrolscsss
tyles.asp?frame=true

#Customizing the Appearance of ASP.NET Server Controls Using Styles
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskcustomizingcontrolap
pearanceusingstyles.asp?frame=true

Hope also help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

-------------------------------------------------------------------------
 
Thanks again Steven that makes sense. You have been of tremendous help this
evening!

But, I have to say that it would be a lot better if the original architects
had allowed us to set this attribute at design time, in order keep a
separation of code and content.


Thanks again
Brian W


Steven Cheng said:
Hi Brian,

As for the problem that "setting back ground image for a ASP.NET Table 's
TableRow or TableCell", I think we need to set it by code:

Every ASP.NET Server Control has encapsulated its related HtmlControl's
Attributes and Css Sytle as two Collectons
"Attributes" and "Style" collections, since in Html table element we use
the following means to set background image:
<TR style="BACKGROUND-IMAGE:
url(http://support.microsoft.com/library/images/support/bnr_microsoft.gif)">
background="http://support.microsoft.com/library/images/support/bnr_microsof
t.gif">fsdfds</TD>

Then, in ASP.NET Table control we can use the following code to specify the
background image:

aspTable.Rows[0].Style.Add("BACKGROUND-IMAGE","url(images/tr.gif)");
aspTable.Rows[0].Cells[0].Style.Add("BACKGROUND-IMAGE","url(http://support.m
icrosoft.com/library/images/support/bnr_microsoft.gif)");
aspTable.Rows[0].Cells[0].Attributes.Add("background","http://support.micros
oft.com/library/images/support/bnr_microsoft.gif");

In addition, here are the reference on ASP.NET server control's attributes
an sytles collections in MSDN:
#AttributeCollection Members
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIAttribute
CollectionMembersTopic.asp?frame=true

#ASP.NET Server Controls and CSS Styles
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconwebformscontrolscsss
tyles.asp?frame=true

#Customizing the Appearance of ASP.NET Server Controls Using Styles
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskcustomizingcontrolap
pearanceusingstyles.asp?frame=true

Hope also help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi Brian,

Thanks. I'm glad that my suggestions have helped you. In addition, as for
the "the original architects
had allowed us to set this attribute at design time" you mentioned, the
ASP.NET has provided the HtmlControls which are mapped to the original html
elements such as

HtmlInputText == <input type="text" ..>
HtmlTable == <table id=".." ..>

To use a HtmlTable control we can just code in the html view as below:
<table id="tbHtml" runat="server" >
<tr>
<td></td>
</tr>
...
</table>

Then, in code behind , we can also reference the HtmlControls' instance
like a server control, such as

tbHtml.Rows[0].Cells[0]...

The most important difference is that we can edit a HtmlControl as a normal
html element in the HtmlView at design time. For detailed info on ASP.NET
Html Controls , you can refer to the following reference in MSDN:

#The Forgotten Controls: HTML Server Controls
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-forgottencontrols
.asp?frame=true

#HTML Server Controls
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconaspsyntaxforhtmlc
ontrols.asp?frame=true

Thanks again for posting here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Yes, that's very true, and in most cases works just fine for me. But, this
is the situation I am in...

I'm using MasterPages from Paul Wilson's article and I must resolve the URL
for the background image of the table cell at runtime.

Now, when I'm talking about an <A> tag or a <IMG> the problem is solved by
simply adding a runat=server to the attributes like this:

<img src="~/images/image.gif" runat="server">

The frame work will resolve the path to the image for me, in this case. But
this does not work for the background attribute of a <TD> tag, so I tried
the following:

<td background='<%= ResolveUrl("~/images/bkimage.gif")%>'

Which, of course, gives me the error "The Controls collection cannot be
modified because the control contains code blocks (i.e. <% ... %>)."

So, this begs the question, how does one resolve a URL at runtime in a
situation like this?

Actually, I have a similar problem when coding static .HTML pages.

In either case, using "~/" (as I would expect) does nothing unless in
ASP.NET you use the ResolveUrl() method.


Thanks
Brian W
 
Hi Brian,

Thanks for your followup. As for the problem you of the HtmlTable control
you mentioned in the last reply .I've tested on my side and did encountered
the problem also. I found it's ok to use
<%= ResolveUrl(....) %> in a normal html <table ><tr><td>... element but
not work when it's set as runat=server.
I think it is because when set and runat=server, it become a certain server
control and will be parsed by the ASP.NET at runtime which limit it to
embed <% %> code block as normal html element. So it seems that we'd have
to set it in the code behind by the control's instance reference.

In addition, as the "~/..." style path, I think it's used at server side
and need to be Resolved to client useable format before render to
clientside. So in normal html page, it is not resolved so that can't be
used by the client browser.

Anyway, I do agree that the inline edit of the ASP or Html Table control be
a limitation of the current ASP.NET and hope it'll be improved in the
furture version. And thanks again for your feedback on this.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top