Referring to a textbox within an ItemTemplate

Discussion in 'ASP .Net' started by VB Programmer, Nov 16, 2005.

  1. With HTML, how do I get the value of a textbox in an ItemTemplate column
    within a datagrid?

    Simple shopping cart. For each row: 1 column is a textbox where they can
    enter the quantity. 1 column is an update button that calls UpdateCart.aspx
    passing it the CartId and Qty.

    Here's the HTML....

    <asp:TemplateColumn>
    :
    <ItemTemplate>
    <asp:TextBox id=txtQty runat="server" Width="28px" Text='<%#
    DataBinder.Eval(Container.DataItem, "ProductQty") %>'>
    </asp:TextBox>
    </ItemTemplate>
    :
    <ItemTemplate>
    <A href='UpdateCart.aspx?CartId=<%#
    DataBinder.Eval(Container.DataItem, "CartID") %>&amp;Qty=<%# txtQty.text
    %>'>
    <asp:Image id="Image3" runat="server"
    ImageUrl="Resources/UpdateBtn.jpg"></asp:Image></A>
    </ItemTemplate>
    </asp:TemplateColumn>

    It doesn't like txtQty.text. I also tried entering
    DataBinder.Eval(Container.DataItem, "ProductQty") but, of course, it selects
    the ORIGINAL value...

    Sorry. I'm just confused.

    Thanks!
     
    VB Programmer, Nov 16, 2005
    #1
    1. Advertising

  2. You are confusing server-side with client-side code. When you are
    assigning the querystring value of <%#txtQty.text%>, you are hard-coding
    the value to whatever the server-side last knew about.

    Normally, it would be as simple as using a bit of javascript to set the
    value, as in:

    <a href="#" onclick="window.location='UpdateCart.aspx?CartId=<%#
    DataBinder.Eval(Container.DataItem, "CartID") %>&amp;Qty=' +
    document.getElementById['txtQty'].value">

    However, you're in a datagrid (pretty sure since you're using
    TemplateColumns), so the txtQty ID is going to change for every row. So
    we can't hard-code this form ID value in the link. That means we will
    have to evaluate it per row and generate the URL for the link per row.

    You will need to make the link runat=server and give it an ID like
    lnkUpdate.

    You will need an event handler for the ItemDataBound event and you will
    need to to update the link's address in it. Like this:

    private sub UpdateLink(s as object, e as datagriditemeventargs) handles
    dgWhatever.ItemDataBound
    dim txtQty as textbox
    dim lnkupdate as htmlAnchor
    dim js as string
    if e.item.itemtype=item or e.item.itemtype=alternatingitem then
    lnkupdate=e.item.findcontrol("lnkUpdate")
    txtqty=e.item.findcontrol("txtQty")
    js="window.location='UpdateCart.aspx?CartId=" & e.item.dataitem
    ("CartID") & "&Qty=' + document.getElementById['" & txtQty.clientid &
    "'].value
    lnkupdate.attributes.add("onclick",js)
    end if
    end sub

    So what is happening in there is when we are in an item or alternating
    item, find the qty text box and the update link. Make a javascript
    statement and use the current dataitem's CartID value (which is the same
    as you did in the ASPX code), and get the clientID value of the textbox
    because it's going to be different for each row.

    This code is just off the top of my head so it may not be copy-pasteable,
    but the concept is correct.

    Hope this helps.


    "VB Programmer" <> wrote in
    news::

    > With HTML, how do I get the value of a textbox in an ItemTemplate
    > column within a datagrid?
    >
    > Simple shopping cart. For each row: 1 column is a textbox where they
    > can enter the quantity. 1 column is an update button that calls
    > UpdateCart.aspx passing it the CartId and Qty.
    >
    > Here's the HTML....
    >
    > <asp:TemplateColumn>
    > :
    > <ItemTemplate>
    > <asp:TextBox id=txtQty runat="server" Width="28px" Text='<%#
    > DataBinder.Eval(Container.DataItem, "ProductQty") %>'>
    > </asp:TextBox>
    > </ItemTemplate>
    > :
    > <ItemTemplate>
    > <A href='UpdateCart.aspx?CartId=<%#
    > DataBinder.Eval(Container.DataItem, "CartID") %>&amp;Qty=<%#
    > txtQty.text %>'>
    > <asp:Image id="Image3" runat="server"
    > ImageUrl="Resources/UpdateBtn.jpg"></asp:Image></A>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    > It doesn't like txtQty.text. I also tried entering
    > DataBinder.Eval(Container.DataItem, "ProductQty") but, of course, it
    > selects the ORIGINAL value...
    >
    > Sorry. I'm just confused.
    >
    > Thanks!
    >
    >
    >
     
    cbDevelopment, Nov 18, 2005
    #2
    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. Bennett Haselton
    Replies:
    1
    Views:
    5,363
    Joe Fallon
    Sep 24, 2004
  2. KatB
    Replies:
    0
    Views:
    455
  3. AlecL
    Replies:
    3
    Views:
    1,241
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 11, 2007
  4. Adam Nielsen
    Replies:
    3
    Views:
    1,330
    Kai-Uwe Bux
    Oct 10, 2007
  5. Candice
    Replies:
    0
    Views:
    256
    Candice
    Dec 5, 2003
Loading...

Share This Page