How to access contents of cells within a generated table?

A

aualias

I am writing a custom control to display the contents of a shopping cart
within a table. The quantity of each item appears in a textbox inside a
cell. It all appears just fine on a web page, but I do not know how to
access the contents of the text boxes. Adding the runat="server" attribute
seems to have no effect.



Here is the code for Render()...



protected override void Render(HtmlTextWriter output)

{

if ( _cart == null )

throw new ApplicationException("No Cart assigned to
the ShoppingCartDisplay object");



output.AddAttribute(HtmlTextWriterAttribute.Id, _tableID);

output.AddAttribute(HtmlTextWriterAttribute.Bgcolor, _bgcolor);

output.AddAttribute(HtmlTextWriterAttribute.Bgcolor, _bgcolor);

output.AddAttribute("runat", "server");

output.RenderBeginTag(HtmlTextWriterTag.Table);



int i = 0;

foreach ( ShoppingCart.CartItem item in _cart )

{

output.RenderBeginTag(HtmlTextWriterTag.Tr);



output.AddAttribute(HtmlTextWriterAttribute.Nowrap,
String.Empty);

output.RenderBeginTag(HtmlTextWriterTag.Td);

output.Write(item.Name);

output.RenderEndTag();



output.AddAttribute(HtmlTextWriterAttribute.Nowrap,
String.Empty);

output.RenderBeginTag(HtmlTextWriterTag.Td);

output.Write(item.ProductID);

output.RenderEndTag();



output.AddAttribute(HtmlTextWriterAttribute.Nowrap,
String.Empty);

output.RenderBeginTag(HtmlTextWriterTag.Td);

output.Write(item.Price);

output.RenderEndTag();



// this is what I am trying to access...

output.RenderBeginTag(HtmlTextWriterTag.Td);

output.AddAttribute(HtmlTextWriterAttribute.Type,
"text");

output.AddAttribute(HtmlTextWriterAttribute.Id,
String.Format("nItems{0}", i));

output.AddAttribute(HtmlTextWriterAttribute.Name,
String.Format("nItems{0}", i));

output.AddAttribute(HtmlTextWriterAttribute.Value,
item.Quantity.ToString());


output.AddAttribute(HtmlTextWriterAttribute.Maxlength, "4");

output.AddAttribute(HtmlTextWriterAttribute.Size,
"2");

output.AddAttribute("runat", "server");

output.RenderBeginTag(HtmlTextWriterTag.Input);

output.RenderEndTag();

output.RenderEndTag();



output.RenderEndTag();

i++;

}



output.RenderBeginTag(HtmlTextWriterTag.Tr);

output.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");

output.AddAttribute(HtmlTextWriterAttribute.Align, "right");

output.RenderBeginTag(HtmlTextWriterTag.Td);



output.AddAttribute(HtmlTextWriterAttribute.Type, "submit");

output.AddAttribute(HtmlTextWriterAttribute.Id, "btnUpdate");

output.AddAttribute(HtmlTextWriterAttribute.Name, "btnUpdate");

output.AddAttribute(HtmlTextWriterAttribute.Value, "Update");

output.AddAttribute("runat", "server");

output.RenderBeginTag(HtmlTextWriterTag.Input);

output.RenderEndTag();

output.RenderEndTag();



output.RenderEndTag();

}

}



Thanks for any help.



David
 
J

John Saunders

aualias said:
I am writing a custom control to display the contents of a shopping cart
within a table. The quantity of each item appears in a textbox inside a
cell. It all appears just fine on a web page, but I do not know how to
access the contents of the text boxes. Adding the runat="server"
attribute
seems to have no effect.

Runat="server" is something interpreted by ASP.NET. You're rendering that
into the HTML which will be sent to the client, which has no idea what to do
with it.

Exactly where do you need the contents of the text box?

John Saunders
 
A

aualias

Hi John,

Just looked at my post. Guess I won't use Word again...
Runat="server" is something interpreted by ASP.NET. You're rendering that
into the HTML which will be sent to the client, which has no idea what to do
with it.
Obviously. Don't know what I was thinking...
Exactly where do you need the contents of the text box?
The contents of the text box contains the quantity of each item ordered.
The user can change this quantity before placing the order. If they change
the quantity and click on the Update button, I want to update the cart with
the new value. The code in Page_Load() goes roughly like this...

if ( IsPostBack )
{
int n = < column that contains the text box >;
Cart cart = (Cart)Session["cart"];
for ( int i = 0; i < cart.Count; i++ )
{
cart.Quantity = < the value from the text box in row i, column n
of the rendered table >;
}
}

David
 
M

Mike MacMillan

since it looks like your update button triggers a post back, you will have
access to the changed values for each textbox using Request.Form (or
Request.Params). with that, your code below only requires a small change :
if ( IsPostBack )
{
int n = < column that contains the text box >;
Cart cart = (Cart)Session["cart"];
for ( int i = 0; i < cart.Count; i++ )
{
cart.Quantity = < the value from the text box in row i, column

n

cart.Quantity = Request.Form["nItems"+ i]; //** convert to
datatype of choice

also, the script block above appears to assume each textbox will be named in
the format:

<rowNumber>items<itemNumber>

but your custom control renders the name of each textbox with a hardcoded
'n', not a row number (hence the hardcoded n above):

output.AddAttribute(HtmlTextWriterAttribute.Name,
String.Format("nItems{0}", i));


Mike MacMillan



aualias said:
Hi John,

Just looked at my post. Guess I won't use Word again...
Runat="server" is something interpreted by ASP.NET. You're rendering that
into the HTML which will be sent to the client, which has no idea what
to
do
with it.
Obviously. Don't know what I was thinking...
Exactly where do you need the contents of the text box?
The contents of the text box contains the quantity of each item ordered.
The user can change this quantity before placing the order. If they change
the quantity and click on the Update button, I want to update the cart with
the new value. The code in Page_Load() goes roughly like this...

if ( IsPostBack )
{
int n = < column that contains the text box >;
Cart cart = (Cart)Session["cart"];
for ( int i = 0; i < cart.Count; i++ )
{
cart.Quantity = < the value from the text box in row i, column n
of the rendered table >;
}
}

David
 
A

aualias

Mike,

Request.Form will definitely do it.

I put in the "n" in an attempt to clarify - I guess it just served to
confuse...
output.AddAttribute(HtmlTextWriterAttribute.Name,
String.Format("nItems{0}", i));

The name attribute is nItems0, nItems1, nItems2, etc. A value, n, is not
the hardcoded prefix. I just use n to indicate a positive integer, a habit
from C++.
A variable named n was a poor choice to use for indicating the column index
in the post.

Thanks for your help.

David


Mike MacMillan said:
since it looks like your update button triggers a post back, you will have
access to the changed values for each textbox using Request.Form (or
Request.Params). with that, your code below only requires a small change :
if ( IsPostBack )
{
int n = < column that contains the text box >;
Cart cart = (Cart)Session["cart"];
for ( int i = 0; i < cart.Count; i++ )
{
cart.Quantity = < the value from the text box in row i,
column
n

cart.Quantity = Request.Form["nItems"+ i]; //** convert to
datatype of choice

also, the script block above appears to assume each textbox will be named in
the format:

<rowNumber>items<itemNumber>

but your custom control renders the name of each textbox with a hardcoded
'n', not a row number (hence the hardcoded n above):

output.AddAttribute(HtmlTextWriterAttribute.Name,
String.Format("nItems{0}", i));


Mike MacMillan



aualias said:
Hi John,

Just looked at my post. Guess I won't use Word again...
Runat="server" is something interpreted by ASP.NET. You're rendering that
into the HTML which will be sent to the client, which has no idea what
to
do
with it.
Obviously. Don't know what I was thinking...
Exactly where do you need the contents of the text box?
The contents of the text box contains the quantity of each item ordered.
The user can change this quantity before placing the order. If they change
the quantity and click on the Update button, I want to update the cart with
the new value. The code in Page_Load() goes roughly like this...

if ( IsPostBack )
{
int n = < column that contains the text box >;
Cart cart = (Cart)Session["cart"];
for ( int i = 0; i < cart.Count; i++ )
{
cart.Quantity = < the value from the text box in row i,
column
n
of the rendered table >;
}
}

David
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top