Adding an imagebutton and text to a table control dynamically

C

crferguson

Hello all!

I've run myself into a wall on something that sounds like it should be
simple. I'm building a table control dynamically and want to add a
string of text followed by an imagebutton control. I'm able to do this
in individual cells, but I really need it in one cell. I'm wanting to
add the string of text to the cell followed by a space and then the
imagebutton control. For instance, the result in the cell would be
something like "My Name [imagebutton]"

I can do this without a problem manually in a form by adding a table
and then both the text and button in a cell. But I can't figure out
how to do it dynamically. The code I've been working with goes
something like this:

=======================
dim t as table, tr as tablerow, tc as tablecell, ib as iamgebutton
dim strText as string = "Some Text"

t = new table
tr = new tablerow
tc = new tablecell
ib = new imagebutton

tc.text = strText & " "
tc.controls.add(ib)
tr.cells.add(tc)
t.rows.add(tr)
======================

Doing the above displays the imagebutton in the cell, but not the text.
 
K

Ken Cox - Microsoft MVP

Hi,

I suspect that the control is replacing the text with the image button. I'd
use a literal instead as shown in the following code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim t As Table, tr As TableRow, tc As TableCell, ib As ImageButton,
lit As Literal
Dim strText As String = "The true north strong and free"
t = New Table
tr = New TableRow
tc = New TableCell
ib = New ImageButton
lit = New Literal
ib.ImageUrl = "http://www.gc.ca/images/flag.gif"
ib.PostBackUrl = "http://www.gc.ca/"
lit.Text = "&nbsp;" & strText & "&nbsp;"
tc.Controls.Add(ib)
tc.Controls.Add(lit)
tr.Cells.Add(tc)
t.Rows.Add(tr)
PlaceHolder1.Controls.Add(t)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>

</div>
</form>
</body>
</html>
 
S

Steve C. Orr [MVP, MCSD]

Create a new LiteralControl, add the text to it, then add the LiteralControl
to the cell.

Here's more info:
http://search.msn.com/results.aspx?q=LiteralControl+site:http://SteveOrr.net





Ken Cox - Microsoft MVP said:
Hi,

I suspect that the control is replacing the text with the image button.
I'd use a literal instead as shown in the following code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim t As Table, tr As TableRow, tc As TableCell, ib As ImageButton,
lit As Literal
Dim strText As String = "The true north strong and free"
t = New Table
tr = New TableRow
tc = New TableCell
ib = New ImageButton
lit = New Literal
ib.ImageUrl = "http://www.gc.ca/images/flag.gif"
ib.PostBackUrl = "http://www.gc.ca/"
lit.Text = "&nbsp;" & strText & "&nbsp;"
tc.Controls.Add(ib)
tc.Controls.Add(lit)
tr.Cells.Add(tc)
t.Rows.Add(tr)
PlaceHolder1.Controls.Add(t)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>

</div>
</form>
</body>
</html>

Hello all!

I've run myself into a wall on something that sounds like it should be
simple. I'm building a table control dynamically and want to add a
string of text followed by an imagebutton control. I'm able to do this
in individual cells, but I really need it in one cell. I'm wanting to
add the string of text to the cell followed by a space and then the
imagebutton control. For instance, the result in the cell would be
something like "My Name&nbsp;[imagebutton]"

I can do this without a problem manually in a form by adding a table
and then both the text and button in a cell. But I can't figure out
how to do it dynamically. The code I've been working with goes
something like this:

=======================
dim t as table, tr as tablerow, tc as tablecell, ib as iamgebutton
dim strText as string = "Some Text"

t = new table
tr = new tablerow
tc = new tablecell
ib = new imagebutton

tc.text = strText & "&nbsp;"
tc.controls.add(ib)
tr.cells.add(tc)
t.rows.add(tr)
======================

Doing the above displays the imagebutton in the cell, but not the text.
 
K

Ken Cox - Microsoft MVP

Thanks Steve! <grin>

Steve C. Orr said:
Create a new LiteralControl, add the text to it, then add the
LiteralControl to the cell.

Here's more info:
http://search.msn.com/results.aspx?q=LiteralControl+site:http://SteveOrr.net





Ken Cox - Microsoft MVP said:
Hi,

I suspect that the control is replacing the text with the image button.
I'd use a literal instead as shown in the following code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim t As Table, tr As TableRow, tc As TableCell, ib As
ImageButton, lit As Literal
Dim strText As String = "The true north strong and free"
t = New Table
tr = New TableRow
tc = New TableCell
ib = New ImageButton
lit = New Literal
ib.ImageUrl = "http://www.gc.ca/images/flag.gif"
ib.PostBackUrl = "http://www.gc.ca/"
lit.Text = "&nbsp;" & strText & "&nbsp;"
tc.Controls.Add(ib)
tc.Controls.Add(lit)
tr.Cells.Add(tc)
t.Rows.Add(tr)
PlaceHolder1.Controls.Add(t)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>

</div>
</form>
</body>
</html>

Hello all!

I've run myself into a wall on something that sounds like it should be
simple. I'm building a table control dynamically and want to add a
string of text followed by an imagebutton control. I'm able to do this
in individual cells, but I really need it in one cell. I'm wanting to
add the string of text to the cell followed by a space and then the
imagebutton control. For instance, the result in the cell would be
something like "My Name&nbsp;[imagebutton]"

I can do this without a problem manually in a form by adding a table
and then both the text and button in a cell. But I can't figure out
how to do it dynamically. The code I've been working with goes
something like this:

=======================
dim t as table, tr as tablerow, tc as tablecell, ib as iamgebutton
dim strText as string = "Some Text"

t = new table
tr = new tablerow
tc = new tablecell
ib = new imagebutton

tc.text = strText & "&nbsp;"
tc.controls.add(ib)
tr.cells.add(tc)
t.rows.add(tr)
======================

Doing the above displays the imagebutton in the cell, but not the text.
 
C

crferguson

Thanks both of you! That's exactly what I needed. I'm going from VB6
to .NET and it's almost like having to learn a new language
alltogether. Plus, my only web site building experience prior to this
is straight HTML 4.0! Thanks again!
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top