For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET Building Controls > January 2005 > How to access contents of cells within a generated table?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to access contents of cells within a generated table?
aualias

2005-01-16, 8:59 pm

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






John Saunders

2005-01-16, 8:59 pm

"aualias" <aualias@newsgroups.nospam> wrote in message
news:uooHYCp8EHA.3124@TK2MSFTNGP11.phx.gbl...
>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


aualias

2005-01-16, 8:59 pm

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[i].Quantity = < the value from the text box in row i, column n
of the rendered table >;
}
}

David


Mike MacMillan

2005-01-16, 8:59 pm

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[i].Quantity = < the value from the text box in row i, column

n

cart[i].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" <aualias@newsgroups.nospam> wrote in message
news:OxNaKYs8EHA.1300@TK2MSFTNGP14.phx.gbl...
> Hi John,
>
> Just looked at my post. Guess I won't use Word again...
>
that[color=darkred]
to[color=darkred]
> do
> Obviously. Don't know what I was thinking...
>
> 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[i].Quantity = < the value from the text box in row i, column

n
> of the rendered table >;
> }
> }
>
> David
>
>



aualias

2005-01-16, 8:59 pm

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" <mikejmacmillan@gmail.com> wrote in message
news:RINCd.8355$5R.1455@newssvr21.news.prodigy.com...
> 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

:
>
column[color=darkred]
> n
>
> cart[i].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" <aualias@newsgroups.nospam> wrote in message
> news:OxNaKYs8EHA.1300@TK2MSFTNGP14.phx.gbl...
> that
> to
> change
> with
column[color=darkred]
> n
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com