How to access contents of cells within a generated table?

Discussion in 'ASP .Net Building Controls' started by aualias, Jan 4, 2005.

  1. aualias

    aualias Guest

    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
    aualias, Jan 4, 2005
    #1
    1. Advertising

  2. "aualias" <> wrote in message
    news:...
    >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
    John Saunders, Jan 4, 2005
    #2
    1. Advertising

  3. aualias

    aualias Guest

    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
    aualias, Jan 5, 2005
    #3
  4. 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" <> wrote in message
    news:...
    > 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
    >
    >
    Mike MacMillan, Jan 5, 2005
    #4
  5. aualias

    aualias Guest

    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" <> wrote in message
    news:RINCd.8355$...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
    aualias, Jan 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    510
  2. Replies:
    2
    Views:
    357
  3. Craig Taylor
    Replies:
    1
    Views:
    328
    Craig Taylor
    Dec 7, 2006
  4. Joel Finkel

    Cells[].Text or Cells[].Controls[0]

    Joel Finkel, Sep 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    294
    Joel Finkel
    Sep 1, 2003
  5. SandyTipper

    Generated cells in HTML table not showing

    SandyTipper, Aug 12, 2009, in forum: Javascript
    Replies:
    7
    Views:
    142
    Dr J R Stockton
    Aug 13, 2009
Loading...

Share This Page