473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Referring to a textbox within an ItemTemplate

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
1 7845
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tyson | last post by:
Hi Very novice sorry. Trying to add a simple query to a textbox...I am getting Enter Parameter Value I have a Form called (frm_tbl_RR_Datacapture). With a series of Textboxes. I am wanting...
0
by: KatB | last post by:
Hi, I have a web control (ucTickets) in an asp.net page (MasterPage). From ucTickets, I open a calendar popup window to which I pass a control.value (which textbox to fill with the date). I've...
1
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers...
1
by: rockdale | last post by:
Hi, All I have an datagrid with alternating color, I also want the back-color of my textbox in ItemTemplate also appears alternating, how could I do that? <asp:datagrid id="dgridEmpHours"...
6
by: rn5a | last post by:
When the EditCommandColumn in a DataGrid is clicked, all the BoundColumns get replaced by TextBoxes so that users can alter the data. By default, the Text in the TextBoxes are left-aligned. Is...
2
by: AC [MVP MOSS] | last post by:
I have a repeater with a header, footer, and item template. The item template is the only one with server controls (hyperlinks, labels, and a nested repeater). Within the top repeater, I'm handling...
3
by: =?Utf-8?B?QW5kcmV3IEhheWVz?= | last post by:
Does anyone else find that when they use a table to handle the layout for a FormView template, and they have textboxes set to width="100%", that the rendered input is wider than the cell they are...
3
by: Steph | last post by:
hello, l ve tried to list control into a the itemtemplate (datalist). The script run into Page_Init (or Page_Load) and build a ArrayList with the found control ID. if somoene have a solution...
2
by: slao | last post by:
I am unable to get the updated value of textbox within a listview when "Update" link button is clicked which calls a function. <asp:ListView ID="_lvCartItems"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.