Referring to a textbox within an ItemTemplate

V

VB Programmer

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!
 
C

cbDevelopment

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.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top