-
Updating txtQuantity in a Repeater
I created a repeater to list my data inside a dataTable of Session("Cart")
however i have a problem. I cannot seem to update the Quantity.
How do i code a 'btnUpdate' so that it will read the txtQuantity and update
each row of item's quantity according to the txtQuantity.Text?
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td height="15"><%# Container.DataItem("Product") %></td>
td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox ID="txtQuantity" Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td>Remove</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Header Codes:
Dim dtCart As DataTable
Dim drCart As DataRow
dtCart = Session("Cart")
viewCart.DataSource = dtCart.DefaultView
viewCart.DataBind()
-
How can i delete that row too?
-
Re: Updating txtQuantity in a Repeater
You want to loop through the textboxes right?
Here's a sample of me looping through the items in the repeater, looking for a checkbox. You can modify it according to your needs to assign a value to your textboxes.
VB Code:
For Each rItem In Me.rptDepts.Items
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
Dim chkB As New CheckBox
chkB = CType(rItem.FindControl("chkDepartment"), CheckBox)
If chkB.Checked = True Then
'update it here
End If
End If
Next
rItem is declared as a RepeaterItem
-
Re: Updating txtQuantity in a Repeater
thanks, in my below case, i tried, it doesnt work.
<script runat="server">
Sub updateQuantity(sender as Object, e As EventArgs)
For Each viewCart In Me.viewCart.Items
If viewCart.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
Dim txtbox As New TextBox
txtbox = CType(viewCart.FindControl("txtQuantity"), TextBox)
If txtbox.Text <> "" Then
txtQuantity.Text = txtbox.Text
End If
End If
Next
End Sub
</script>
HTML BODY
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td><%# Container.DataItem("Product") %></td>
<td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox ID="txtQuantity" Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td align="center">Remove</td>
</tr>
<tr><td colspan="5"><hr size="1"></td></tr>
</ItemTemplate>
</asp:Repeater>
-
Re: Updating txtQuantity in a Repeater
??
For Each viewCart In Me.viewCart.Items
That's quite a paradox.
Look at what I wrote:
For Each rItem In Me.rptDepts.Items
The first variable must be declared as a RepeaterItem. I don't see where you've declared the variable.
-
Re: Updating txtQuantity in a Repeater
thanks i tried, is the below correct? theres an error saying txtQuantity is not declared, however i have a asp:Textbox id="txtQuantity".
Sub updateCart(sender As Object, e As ImageClickEventArgs)
Dim rItem As RepeaterItem
For Each rItem In Me.viewCart.Items
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
Dim txtBox As New TextBox
txtBox = CType(rItem.FindControl("txtQuantity"), TextBox)
If txtBox.Text <> "" Then
txtQuantity.Text = txtBox.Text
End If
End If
Next
Error MSG:
Compiler Error Message: BC30451: Name 'txtQuantity' is not declared.
-
Re: Updating txtQuantity in a Repeater
What is this line:
Code:
txtQuantity.Text = txtBox.Text
Supposed to do?
Note that txtBox is basically the current row's txtQuantity textbox, you already have it.
-
Re: Updating txtQuantity in a Repeater
i'm confused, i thot thats to update the textbox?
-
Re: Updating txtQuantity in a Repeater
What do you want to update each of those textboxes with? Where is that value stored?
-
Re: Updating txtQuantity in a Repeater
in txtQuantity textbox inside this repeating
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td height="15"><%# Container.DataItem("Product") %></td>
td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox ID="txtQuantity" Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td>Remove</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Header Codes:
Dim dtCart As DataTable
Dim drCart As DataRow
dtCart = Session("Cart")
viewCart.DataSource = dtCart.DefaultView
viewCart.DataBind()
-
Re: Updating txtQuantity in a Repeater
Dude, what do you want to update that textbox with?
-
Re: Updating txtQuantity in a Repeater
sorry for not being clear, the repeater will display the product name, code
cost and quantity. The quantity is in a Textbox, editable by the user. Therefore
each product has a Textbox (txtQuantity) in the repeater. Therefore user can
edit the Quantity in the textbox and when click 'update'. The values of this
textbox txtQuantity will be updated to what the user keyed in 1-99 value.
-
Re: Updating txtQuantity in a Repeater
I don't think that still makes any sense. Because the user will key in the value into the textbox. You have to decide the next step, what occurs next when they click update. Is it just a postback of the page, or do you save the values somewhere (to a database) and then redirect to another page?
-
Re: Updating txtQuantity in a Repeater
oh ok, it should postback the page and update the textbox of the new Quantity value previously entered in the same textbox inside the repeater loop. its like refreshing the same page, but only difference is editing the values of the Quantity textboxes..
Is it clear this way?
-
Re: Updating txtQuantity in a Repeater
been trying for hours doesnt work
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
i tried looking up repeateritem control, doesnt seem to have anything.
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
Quote:
Originally Posted by john83
oh ok, it should postback the page and update the textbox of the new Quantity value previously entered in the same textbox inside the repeater loop. its like refreshing the same page, but only difference is editing the values of the Quantity textboxes..
Is it clear this way?
It isn't, sorry.
-
Re: Updating txtQuantity in a Repeater
with the code below, i have a textbox txtQuantity that is repeated in
a datatable Session("Cart") that has 4 rows example. Therefore i have
4 x textboxes of txtQuantity right? each of them represent a field value
of 'Quantity' referenced to each 'quantity' of of the data table, how do i
code the update button to loop through the repeater textbox item and
have it update the datatable.
DataTable columns: Product, Code, Cost, Quantity
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td height="15"><%# Container.DataItem("Product") %></td>
td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox ID="txtQuantity" Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td>Remove</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Header Codes:
Dim dtCart As DataTable
Dim drCart As DataRow
dtCart = Session("Cart")
viewCart.DataSource = dtCart.DefaultView
viewCart.DataBind()
-
Re: Updating txtQuantity in a Repeater
Is that what you wanted?? Then why didn't you just say so? :D
OK, in the loop that you've got above, you must place an intCounter (of type... integer), which you increment in each loop. When you reach a txtQuantity box, set the value in your datatable to the value in the textbox.
I do not have the IDE open but I'm sure you know how to access the value in your datatable. It might be something like this:
dt.Rows(intCounter)("Quantity") = Integer.Parse(txtQuantity.Text)
-
Re: Updating txtQuantity in a Repeater
yep thats what i wanted. where do i put that line in.
my question will be how do i code my btnUpdate procedure?
-
Re: Updating txtQuantity in a Repeater
In your loop! Right after the itemtype confirms, increment the counter. Just place the code in the btnUpdate click event.
-
Re: Updating txtQuantity in a Repeater
thanks its getting clearer for me, however i got an error saying txtQuantity is not declared. How can this be so, as it is in the repeateritem loop?
I tried to change it to, but it still doesnt work, can u check my codes?
Dim rItem As RepeaterItem
Dim intCounter As Integer
Dim dt As DataTable
dt = Session("Cart")
For Each rItem In Me.Cart.Items 'Cart is the repeater id & datatable
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
dt.Rows(intCounter)("Quantity") = Integer.Parse(Request.Form("txtQuantity")) 'since txtQuantity.Text gives error.
intCounter += 1
End If
Next
Session("Cart") = dt
End Sub
-
Re: Updating txtQuantity in a Repeater
Use it in the loop above, so you'd assign the value in txtBox.Text. Not txtQuantity.
-
Re: Updating txtQuantity in a Repeater
I tried the below codes myself, but it doesnt seem to work. theres
an error below. Please advise!
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td height="15"><%# Container.DataItem("Product") %></td>
td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox ID='txtQuantity_<%# Container.DataItem("Code") %>' Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td>Remove</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Header Codes:
Dim dtCart As DataTable
Dim drCart As DataRow
dtCart = Session("Cart")
dtCart = Session("Cart")
For Each rItem In Me.viewCart.Items
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
dtCart.Rows(intCount)("Quantity") = Integer.Parse(txtQuantity_(dtCart.Rows(intCount)("Code")).Text)
intCount += 1
End If
Next
Session("Cart") = dtCart
viewCart.DataSource = dtCart.DefaultView
viewCart.DataBind()
-
Error:
'txtQuantity_<%# Container.DataItem("Code") %>' is not a valid identifier.
-
Re: Updating txtQuantity in a Repeater
For Each rItem In Me.viewCart.Items
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
Dim txtBox As New TextBox
txtBox = CType(rItem.FindControl("txtQuantity"), TextBox)
If txtBox.Text <> "" Then
dtCart.Rows(intCount)("Quantity") = Integer.Parse(txtBox.Text)
End If
-
Re: Updating txtQuantity in a Repeater
Thanks, still doesnt work. the values go back to the old ones after i click update. Heres my code.
<script runat="server">
Sub updateCart(sender As Object, e As ImageClickEventArgs)
Dim rItem As RepeaterItem
Dim intCount As Integer
Dim dtCart As DataTable
dtCart = Session("Cart")
For Each rItem In Me.viewCart.Items
If rItem.ItemType = ListItemType.AlternatingItem OrElse rItem.ItemType = ListItemType.Item Then
Dim txtBox As New TextBox
txtBox = CType(rItem.FindControl("txtQuantity"), TextBox)
If txtBox.Text <> "" Then
dtCart.Rows(intCount)("Quantity") = Integer.Parse(txtBox.Text)
End If
End If
Next
Session("Cart") = dtCart
End Sub
</script>
<asp:Repeater id="viewCart" runat="server">
<ItemTemplate>
<tr>
<td><%# Container.DataItem("Product") %></td>
<td><%# Container.DataItem("Code") %></td>
<td><%# Double.Parse(Container.DataItem("Cost")).ToString("C") %></td>
<td align="center"><asp:TextBox id="txtQuantity" Text='<%# Container.DataItem("Quantity") %>' ToolTip="Quantity" Width="20" MaxLength="2" runat="server" /></td>
<td align="center">Remove</td>
</tr>
<tr><td colspan="5"><hr size="1"></td></tr>
</ItemTemplate>
</asp:Repeater>
<asp:ImageButton ID="btnUpdate" AlternateText="Update Cart" ImageUrl="images/updatecart.gif" ToolTip="Update Cart" OnClick="updateCart" runat="server" />
-
Re: Updating txtQuantity in a Repeater
Step through the code and see if the textbox contains the new values.
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
Set EnableViewState = "true" for the textbox control in the repeater.
-
Re: Updating txtQuantity in a Repeater
tried txtBox.EnableViewState = True
still same! sorry abt this.
is there anyway i can show u, hmm trace or anything?
-
Re: Updating txtQuantity in a Repeater
Attach your aspx and aspx.vb to this thread.. so we can download and look at it. Christ Mendhak your patient ;) !
-
Re: Updating txtQuantity in a Repeater
I did have to take a small break in between :afrog:
-
1 Attachment(s)
Re: Updating txtQuantity in a Repeater
attached. please let me know where i went wrong.
-
Re: Updating txtQuantity in a Repeater
You're using Visual Studio? :ehh:
-
Re: Updating txtQuantity in a Repeater
using Dreamweaver MX and manually coding the VB codes.
-
Re: Updating txtQuantity in a Repeater
-
Re: Updating txtQuantity in a Repeater
Not good. :sick: This'll take some time to 'understand'