Simple task make difficult with a repeater

E

Eniac

*argh* ... *pull hairs*

I've recently started developing from ASP to ASP.NET

The switch was fairly smooth since i had done some VB.net before ...
then came...FORMS! :)

I find it astounding at how difficult it has become to control a form,
something that was so dead easy in ASP.

Look, its simple, I've made something very basic. I created a small
datatable in session to act as a shopping cart. whenever i on my
shopping basket page, I want to create a list of all the items in the
cart with the quantity in a textbox so they can change it. two
buttons/links on each row to update / delete then at the end, a sum of
all items to be ordered and the total price.

I'm going to show the latest version of my code but I've tried several
ways already and it seems that the repeater just wont see my controls.
..... here, let me paste this code...

can PLEASE anyone tell me what im doing wrong ? any example I've found
on the web thius far only shows how to display bare data but i can do
pretty easily, its adding controls and responding to them that seems to
be difficult.

(might as well ask the next question in line...)
if thats not the way to go, what would be the way to go.

i know the repeater doesnt support the ItemCommand event thing, how
will i know which "update" or "delete" button was clicked ?

Thanks a bunch!

-----------------
<asp:Repeater ID="order_cart" Runat="server"
OnItemDataBound="ComputeSum" Visible="True">
<HeaderTemplate>
<table width="800" cellpadding="0" cellspacing="0">
<tr>
<th>
Description</th>
<th>
Quantité</th>
<th>
Prix</th>
<th>
Action</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,
"ProduitDesc") %></td>
<td>
<asp:TextBox ID="txtQteTotal">
<%#DataBinder.Eval(Container.DataItem,
"QteTotal") %>
</asp:TextBox></td>
<td><%#DataBinder.Eval(Container.DataItem,
"PrixDetail") %></td>
<td>
<asp:Button ID="btnUpdate" Text="Mise à jour" />
<asp:Button ID="btnDelete" Text="Supprimer" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>&nbsp;</td>
<td>
<asp:Label ID="lblGrandTotal" Visible="true"
/></td>
<td>
<asp:Label ID="lblGrandPrix" Visible="true"
/></td>
<td>&nbsp;</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
------------------

then in the code behind...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dtCart As DataTable = Nothing
Dim aControl As System.Web.UI.Control

If IsNothing(Session("ContactInfo")) Then
Response.Redirect("login.aspx")
End If

If Not Page.IsPostBack Then
If Session("ShoppingCart").GetType.FullName.IndexOf("DataTable") <>
-1 Then
dtCart = CType(Session("ShoppingCart"), DataTable)
Me.order_cart.DataSource = dtCart
Me.order_cart.DataBind()

For Each aControl In Me.order_cart.Controls
Dim theLabel As System.Web.UI.WebControls.Label

' theLabel = CType(aControl.ID .FindControl("lblGrandPrix"),
System.Web.UI.WebControls.Label)
'If Not IsNothing(theLabel) Then
' theLabel.Text = String.Format("{0:C}", sglGrandPrix)
'End If

'theLabel = CType(aControl.FindControl("lblGrandTotal"),
System.Web.UI.WebControls.Label)
'If Not IsNothing(theLabel) Then
' theLabel.Text = String.Format("{0:C}", intGrandTotal)
'End If

Next

Else
Throw New Exception("Cette page requiert une session")
End If

End If
End Sub

Public Sub ComputeSum(ByVal sender As Object, ByVal e As
RepeaterItemEventArgs) Handles order_cart.ItemDataBound
'First, make sure we are dealing with an Item or AlternatingItem
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
'Snip out the ViewCount
sglGrandPrix += Convert.ToSingle(DataBinder.Eval(e.Item.DataItem,
"PrixDetail"))
intGrandTotal += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem,
"QteTotal"))

ElseIf e.Item.ItemType = ListItemType.Footer Then
End If

End Sub
 
S

Shimon Sim

I just took fast look at the code.
<asp:TextBox ID="txtQteTotal">
<%#DataBinder.Eval(Container.DataItem,
"QteTotal") %>
</asp:TextBox></

this will never work.
first of all you need to make sure that you have runat=server if you want
asp.net to process it.
2) TextBox has Text property that you must use.
3) you won't be able to set the value of most asp control using inline
databindings in repeater. you must handle ItemDataBound event for it.

Good luck.


*argh* ... *pull hairs*

I've recently started developing from ASP to ASP.NET

The switch was fairly smooth since i had done some VB.net before ...
then came...FORMS! :)

I find it astounding at how difficult it has become to control a form,
something that was so dead easy in ASP.

Look, its simple, I've made something very basic. I created a small
datatable in session to act as a shopping cart. whenever i on my
shopping basket page, I want to create a list of all the items in the
cart with the quantity in a textbox so they can change it. two
buttons/links on each row to update / delete then at the end, a sum of
all items to be ordered and the total price.

I'm going to show the latest version of my code but I've tried several
ways already and it seems that the repeater just wont see my controls.
..... here, let me paste this code...

can PLEASE anyone tell me what im doing wrong ? any example I've found
on the web thius far only shows how to display bare data but i can do
pretty easily, its adding controls and responding to them that seems to
be difficult.

(might as well ask the next question in line...)
if thats not the way to go, what would be the way to go.

i know the repeater doesnt support the ItemCommand event thing, how
will i know which "update" or "delete" button was clicked ?

Thanks a bunch!

-----------------
<asp:Repeater ID="order_cart" Runat="server"
OnItemDataBound="ComputeSum" Visible="True">
<HeaderTemplate>
<table width="800" cellpadding="0" cellspacing="0">
<tr>
<th>
Description</th>
<th>
Quantité</th>
<th>
Prix</th>
<th>
Action</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,
"ProduitDesc") %></td>
<td>
<asp:TextBox ID="txtQteTotal">
<%#DataBinder.Eval(Container.DataItem,
"QteTotal") %>
</asp:TextBox></td>
<td><%#DataBinder.Eval(Container.DataItem,
"PrixDetail") %></td>
<td>
<asp:Button ID="btnUpdate" Text="Mise à jour" />
<asp:Button ID="btnDelete" Text="Supprimer" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>&nbsp;</td>
<td>
<asp:Label ID="lblGrandTotal" Visible="true"
/></td>
<td>
<asp:Label ID="lblGrandPrix" Visible="true"
/></td>
<td>&nbsp;</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
------------------

then in the code behind...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dtCart As DataTable = Nothing
Dim aControl As System.Web.UI.Control

If IsNothing(Session("ContactInfo")) Then
Response.Redirect("login.aspx")
End If

If Not Page.IsPostBack Then
If Session("ShoppingCart").GetType.FullName.IndexOf("DataTable") <>
-1 Then
dtCart = CType(Session("ShoppingCart"), DataTable)
Me.order_cart.DataSource = dtCart
Me.order_cart.DataBind()

For Each aControl In Me.order_cart.Controls
Dim theLabel As System.Web.UI.WebControls.Label

' theLabel = CType(aControl.ID .FindControl("lblGrandPrix"),
System.Web.UI.WebControls.Label)
'If Not IsNothing(theLabel) Then
' theLabel.Text = String.Format("{0:C}", sglGrandPrix)
'End If

'theLabel = CType(aControl.FindControl("lblGrandTotal"),
System.Web.UI.WebControls.Label)
'If Not IsNothing(theLabel) Then
' theLabel.Text = String.Format("{0:C}", intGrandTotal)
'End If

Next

Else
Throw New Exception("Cette page requiert une session")
End If

End If
End Sub

Public Sub ComputeSum(ByVal sender As Object, ByVal e As
RepeaterItemEventArgs) Handles order_cart.ItemDataBound
'First, make sure we are dealing with an Item or AlternatingItem
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
'Snip out the ViewCount
sglGrandPrix += Convert.ToSingle(DataBinder.Eval(e.Item.DataItem,
"PrixDetail"))
intGrandTotal += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem,
"QteTotal"))

ElseIf e.Item.ItemType = ListItemType.Footer Then
End If

End Sub
 
E

Eniac

Hum, thanks, i probably would've found that one sooner or later. i know
about the runat=server, for some reason i just forgot about it when i
wrote it. and the <% %> was not in the text attribute because it messes
up the designer.

Let me put it another way.

Can someone give me a hint on how to display a bunch of rows with
controls associated to them, surely there's a simple way no ? I have to
stick with built-in web forms control, no custom stuff.

Thanks again.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top