Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Referring to a textbox within an ItemTemplate

Reply
Thread Tools

Referring to a textbox within an ItemTemplate

 
 
VB Programmer
Guest
Posts: n/a
 
      11-16-2005
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!


 
Reply With Quote
 
 
 
 
cbDevelopment
Guest
Posts: n/a
 
      11-18-2005
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" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error referring to a template within a template - why? Adam Nielsen C++ 3 10-10-2007 06:30 AM
How to find which button was clicked in a repeater itemtemplate and find the value of textbox AlecL ASP .Net 3 04-11-2007 10:52 PM
Referring to a textbox within a web control within an aspx KatB ASP .Net 0 04-22-2005 07:14 PM
can code inside a Repeater's ItemTemplate modify controls in the ItemTemplate? Bennett Haselton ASP .Net 1 09-24-2004 01:59 AM
Referencing a TextBox control in Javascript from DataList ItemTemplate Candice ASP .Net Datagrid Control 0 12-05-2003 07:11 AM



Advertisments