By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,933 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

Referring to a textbox within an ItemTemplate

P: n/a
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!
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <do**@emailme.com> wrote in
news:eR**************@tk2msftngp13.phx.gbl:
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!


Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.