469,647 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Reading input field from innerhtml span

Help needed!
Hello, I have looked everywhere for a solution to my problem and
this is pretty much my last resource. I have created a table in a span
with the innerhtml command in my code behind. In this table I have
created textbox html input fields. Now I need to allow the user to
change the value in the textfield, and compare it to the old value I
have stored in a cookie.

Problem is, because the textfield was created in the code behide I
can't simple referer to it value as QTY1.text.


<form id="Cart" method="post" runat="server">
<table id="border">
<table id="Body">
<TABLE id="Table1" >
<SPAN id="Span1"

with a bunch of code all around and inbetween,


look something like this.

While y <> x
y = y + 1
tempCookieName = "Item" & y
If Not Request.Cookies(tempCookieName) Is Nothing Then

Dim cItem As HttpCookie =
Dim varProductDescription As String
Dim varProductCost As Double
Dim varCategoryID As Integer


varProductDescription =
HttpUtility.HtmlDecode(MyCommand.Parameters("@Desc ription").Value)

varProductCost = MyCommand.Parameters("@Price").Value
varProductCost = varProductCost * cItem.Values("Q")
varCategoryID =


lblSubTotal.Text = String.Format("{0:###,##0.00}",
lblSubTotal.Text + varProductCost)
Span1.InnerHtml += "<TR><TD width='255'><P
align='left'><FONT face='Helvetica' color='#ff0000'>"
Span1.InnerHtml += "Cart Items</FONT></P></TD><TD
align='center' width='72'>"
Span1.InnerHtml += "<FONT face='Helvetica'
Span1.InnerHtml += "</TR><TR><TD vAlign='top'
width='255'><A HREF='../Store/ProductDetails.aspx?ProductID=" &
cItem.Values("P") & "&CategoryID=" & varCategoryID & "&View=1'>" &
varProductDescription & "</A>"
Span1.InnerHtml += "</asp:HyperLink></TD><TD
vAlign='top' align='center' width='72'>"
Span1.InnerHtml += "<asp:TextBox id='QTY2'
ReadOnly='False' >" & cItem.Values("Q") & "</asp:TextBox></TD><TD
Span1.InnerHtml += "<P><A HREF='cart.aspx'>Save for
Later</A><BR><A HREF='cart.aspx?Delete=" & y & "'>Delete</A>"
Span1.InnerHtml += "</P></TD><TD vAlign='top'><P>$" &
varProductCost & "</P><br><asp:Label id='lblItem" & y & "'
runat='server' ForeColor='White'>" & y & "</asp:Label></TD></TR>"
End If

End While
'========================== SomeCode Here
Anyway, there's no problems displaying what I want, the textfields
are created and they are given a unique id of "QTY#".
I simply want to read the value that has been changed in the textfield.
Compare it to the number if used to create the text field and change
it if it is different.

I have tried everything to get this to work, tried using javascript,
Which I did get to discover the value for me but then I could not use
it to change the asp cookie, leaving me once again without a solution.
I have tried using the "Findcontrol" function but without any luck.
Anything useful is most appreciated....
Tony Philip

May 10 '06 #1
7 2409
You can always do this the old skool way and iterate the Request.Form


but um... looking at your code, have you thought about using an
asp:Repeater? Then you can either use FindControl on each element of
the repeater's items collection or, better yet, add an OnChange event
to an asp:TextBox and handle your changes from there.

Is there a reason you're using InnerHtml like this?

May 10 '06 #2
Couldn't you add runat="server" to the textfield to make it accessible
server-side? I did this with a registration page to hide TR's that the
user didn't need to fill out.

May 10 '06 #3
I have thought about using a repeater. I am storing a table in
I can loop through the cookies and write that to the screen but I don't
know how to write that data to a table or dataset or somthing that can
be bound to the repeater.
Cookie Sample
ITEM1 P = Product name
ITEM1 PD = Product Decription
ITEM1 Q = Quantity

ITEM2 P = Product name
ITEM2 PD = Product Decription
ITEM2 Q = Quantity

and so on. If you know a way to write these to a table of somesort in
memory that I can then bind that would be great... Do you know how or
what read the cookies to that I can then Bind to repeater?
Thanks for the help so far... We are on the same page. I just don't
know how to get the cookies to bind.

May 11 '06 #4
I tried using the runat="server" no luck. I had it there orignially.
I've made so many changes and tried so many things. Keep them comming
though. I really appreciate the help..

May 11 '06 #5
If you're telling me that you've got values like Request.Cookies["ITEM1
P"], Request.Cookies["ITEM1 PD"] ...

then you'd want to parse your cookie structure first unless anyone has
any tricks up their sleeve.

Probably the best way to do that with that format would be to knock up
a regex that tests for (ITEM\d+) so you can get a list of the item
names before you start reading values out of your cookie.

If you want to use a cookie, you might find it easier with a more
structured format... You could always serialize a strongly typed list
of products to some format and store that instead. The .Net framework
has excellent support for serializing objects to Xml or binary data.

Then you can do

// deserialize shopping cart held in the cookie value "Cart".
// You can always encrypt this with, for example, the user's password
ProductList shoppingCart = GetCartFromCookie(Request.Cookies["Cart"]);

// now you're back and object oriented and the rest is a doddle.
myRepeater.DataSource = shoppingCart;
I'm sure some clever MVP will give you manifold reasons why this is a
bad idea but it seems eminently sensible to me. That's why I'm a Least
Valued Professional.

Why a cookie and not in Session state or a database?

May 12 '06 #6
Thanks for the response,
I was trying not to use Session states, and writing to the Database as
to keep down the Server resources. I do final right this information
to the database but only after I know they are going to purchase.

I can read out the information from the cookies to the database,
adding one Item at a time. Is there a Variable that will hold the
table in memory which I can then bind. I would be able to build the
Table variable the same way I right to the DB; One Item row at a time.
Once it was stored in the Variable I could then perhaps bind it. An
Array will not hold a table and Hashtable will not bind to a repeater.
I'm truly at a loss here. Can I not create a Dataset, and populate it
with the data? Then just bind the Dataset?

Is there a way to create an empty dataset and then insert each row of
the table through some kind of add row to dataset statement?

May 13 '06 #7
You have to add an ID attribute along with runat="server" to
programmatically access the control.

May 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Rose Chambers | last post: by
4 posts views Thread by Anders Nielsen | last post: by
5 posts views Thread by gimme_this_gimme_that | last post: by
1 post views Thread by spolsky | last post: by
3 posts views Thread by Ralph | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.