Reading input field from innerhtml span

N

Nez

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.

HTML EXAMPLE

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

with a bunch of code all around and inbetween,

CODE BEHIND

look something like this.

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

Dim cItem As HttpCookie =
Request.Cookies(tempCookieName)
Dim varProductDescription As String
Dim varProductCost As Double
Dim varCategoryID As Integer

'==========================SomeCode
here=========================

SQLConnection.Open()
MyCommand.ExecuteNonQuery()
varProductDescription =
HttpUtility.HtmlDecode(MyCommand.Parameters("@Description").Value)

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

'==========================SomeCode
here=========================

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'
color='#ff0000'>QTY</FONT></TD><TD
align='center'>Options</TD><TD>Price</TD>"
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
vAlign='top'>"
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
(e-mail address removed)
 
F

Flinky Wisty Pomm

You can always do this the old skool way and iterate the Request.Form
collection

http://msdn2.microsoft.com/en-us/library/system.web.httprequest.form.aspx

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?
 
W

Will Asrari

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.
 
N

Nez

I have thought about using a repeater. I am storing a table in
cookies.
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.
 
N

Nez

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..
 
F

Flinky Wisty Pomm

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;
myRepeater.DataBind();


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?
 
N

Nez

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?
 
W

Will Asrari

You have to add an ID attribute along with runat="server" to
programmatically access the control.
 

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

Top