Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
 
Thread Tools Search this Thread
07-04-2009, 14:02   #1
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
C# shopping basket, trying to remove an item...

Hi all,

I've built a shopping basket/cart that works great, it works by adding a record to my database on a Button_Click event, and I display the contents of the records in my DB (the contents of the shopping cart), by using the solution below. What it does is append a new record to a string, using a while loop, each time something is added to the basket, which works great.

The only problem I have now is if someone wants to remove something from the cart, I've been pulling out my hair trying to come up with a solution for this that is consistent with the approach below. It should be easy as I have a unique key for this table so every record has a unique ID that can be identified/deleted but where I'm stuck is I can't work out how to put a button or a hyperlink inside my while loop and bind the behaviour of this event handler to the unique ID in my DB??? Is it possible to put a button or a hyperlink (that does something besides bring you to another page), inside a label and also inside a while loop???

What I need is if I have 3 items in my cart, I need 3 buttons or something else like a hyperlink beside each item, to remove it from the cart or more accurately to remove it from my DB. The while loop just runs through the table when the purchase button is clicked and picks out any records that share the same SessionID.

SqlDataReader DataReader;
DataReader = cmdy.ExecuteReader();

if (DataReader.HasRows)
{
Label13.Text = "";

while (DataReader.Read())
{
object obj1 = new object();
obj1 = string.Format(DataReader["eq_type"].ToString());

object obj2 = new object();
obj2 = string.Format(DataReader["eq_make"].ToString());

object obj3 = new object();
obj3 = string.Format(DataReader["eq_model"].ToString());

object obj4 = new object();
obj4 = string.Format(DataReader["Component_Manufacturer"].ToString());

object obj5 = new object();
obj5 = string.Format(DataReader["Product_Description"].ToString());

object obj6 = new object();
obj6 = string.Format(DataReader["Retail_Price"].ToString());

object obj7 = new object();
obj7 = string.Format(DataReader["Item_ID"].ToString());



string newstring = string.Concat(obj1," " + obj2, " " + obj3, "<br>" + obj4, " " + obj5, "<br> <p1>Retail Price: € " + obj6);
Label13.Text += string.Format(newstring.ToString() + "<br><br>");

Label16.Text = string.Format("Shopping Cart: € {0}", Convert.ToString(cmdu.ExecuteScalar()));

Label8.Text = string.Format("Shipping Cost: € {0:F2}", Convert.ToDecimal(Delivery));

//string FullTotal = Convert.ToString(cmdu.ExecuteScalar()) + Convert.ToString(cmdL.ExecuteScalar());
//Label9.Text = string.Format(FullTotal);

decimal Cart = Convert.ToDecimal(cmdu.ExecuteScalar());
decimal Carry = Convert.ToDecimal(Delivery);

decimal Cart_Total = Cart + Carry;
//Label9.Text = Cart_Total.ToString();
Label9.Text = string.Format("Total Invoice: € {0:F2}", Cart_Total);

Any help hugely appreciated!
Darragh29 is offline  
Advertisement
07-04-2009, 14:20   #2
LowOdour
Registered User
 
Join Date: Jan 2009
Posts: 1,616
Have to say that im not really gone on the idea of storing records in a database that may not get used. It means you could be hitting the database with 100 different products that may never get purchased.
But thats not your question!

inside the while loop you could generate some html, which will spit out the code for a html button. You could add the id of the product while generating the html and also an onclick call which would pass the id as a parameter. Have a javascript call that when the button is clicked, it shows a dialog box informing of the delete. Hit the ok button, it calls a delete page that deletes the record and returns you to the page with the other products.
string = "< input type=\"button\" id = \""+GetIDFromDBHere+"\" onclick ="\"+CallJavaScriptHereWithID+"\" />";

Couple of things...this can definately be done simpler than ive explained it, but its a start for you. You may not need the extra delete form, its only if you want to inform the user.
LowOdour is offline  
07-04-2009, 14:56   #3
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
Quote:
Originally Posted by LowOdour View Post
Have to say that im not really gone on the idea of storing records in a database that may not get used. It means you could be hitting the database with 100 different products that may never get purchased.
But thats not your question!

inside the while loop you could generate some html, which will spit out the code for a html button. You could add the id of the product while generating the html and also an onclick call which would pass the id as a parameter. Have a javascript call that when the button is clicked, it shows a dialog box informing of the delete. Hit the ok button, it calls a delete page that deletes the record and returns you to the page with the other products.
string = "< input type=\"button\" id = \""+GetIDFromDBHere+"\" onclick ="\"+CallJavaScriptHereWithID+"\" />";

Couple of things...this can definately be done simpler than ive explained it, but its a start for you. You may not need the extra delete form, its only if you want to inform the user.
Hi LowOdour,

I don't think your solution can be used for my problem because the while loop is in a codebehind C# page, separate from the HTML code...
Darragh29 is offline  
07-04-2009, 15:23   #4
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
Hi LowOdour,

I don't think your solution can be used for my problem because the while loop is in a codebehind C# page, separate from the HTML code...
Put a runat=server in the <input> tag and you can access the clientID property of the control in the codebehind.

< input type=\"button\" id = \""+GetIDFromDBHere+"\" onclick ="\"+CallJavaScriptHereWithID+"\" runat="server" />

In the while loop then you could set the onclick property of the input type:

GetIDFromDBHere.onclick="functionName('" + GetIDFromDBHere.ClientID +"');";

Last edited by John_Mc; 07-04-2009 at 15:24. Reason: Changed from Page Load to While loop
John_Mc is online now  
07-04-2009, 15:28   #5
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Another alternative is to add an asp.net button in the while loop. You could set it's ID, CommandName and CommandArgument to whatever type of button it is.

Should be a simple enough solution. For me at least, it'd be easier than using javascript and a seperate form as suggested above
John_Mc is online now  
Advertisement
07-04-2009, 19:16   #6
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
Ok, I've decided to use an asp.net LinkButton to get me over this last hurdle...

Right now, I have my LinkButton in my aspx page, but where I'm stuck is actually getting my while loop to create a new instance of the button for each record in my DB. Can I create an instance of a LinkButton from my codebehind page??? If I can, I think I can crack this...
Darragh29 is offline  
07-04-2009, 19:25   #7
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
Ok, I've decided to use an asp.net LinkButton to get me over this last hurdle...

Right now, I have my LinkButton in my aspx page, but where I'm stuck is actually getting my while loop to create a new instance of the button for each record in my DB. Can I create an instance of a LinkButton from my codebehind page??? If I can, I think I can crack this...
You can create pretty much add anything in the code behind

I'm writing this without a compiler so you'll probably need to correct the case in some places:

Code:
LinkButton lb = new LinkButton();
lb.id="delete_cmd" + n; //n is your index for looping or just a counter
lb.Text="Delete";
lb.CommandName="Delete";
lb.CommandArgument=nRowID;
lb.OnClick="EventHandlerName";
//lb.OnClientClick="";
//Set any other properties here

//Then add to a container such as a placeholder or panel
CartPanel.Controls.Add(lb);
I know that you're new to this so I'll save you some future headache and advise you to lookup the .net page life cycle. You'll have all sorts of problems with dynamically added controls if you dont fully grasp it.

Last edited by John_Mc; 07-04-2009 at 19:31. Reason: Grammar
John_Mc is online now  
07-04-2009, 21:49   #8
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
Quote:
Originally Posted by John_Mc View Post
You can create pretty much add anything in the code behind

I'm writing this without a compiler so you'll probably need to correct the case in some places:

Code:
LinkButton lb = new LinkButton();
lb.id="delete_cmd" + n; //n is your index for looping or just a counter
lb.Text="Delete";
lb.CommandName="Delete";
lb.CommandArgument=nRowID;
lb.OnClick="EventHandlerName";
//lb.OnClientClick="";
//Set any other properties here

//Then add to a container such as a placeholder or panel
CartPanel.Controls.Add(lb);
I know that you're new to this so I'll save you some future headache and advise you to lookup the .net page life cycle. You'll have all sorts of problems with dynamically added controls if you dont fully grasp it.
Ya see here is my problem, I have to get the LinkButton into my Label that is appended above, or at least I think I do if I want to stick with my approach above... So I can't use a placeholder then I think??? I dunno!
Darragh29 is offline  
07-04-2009, 23:06   #9
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
Ya see here is my problem, I have to get the LinkButton into my Label that is appended above, or at least I think I do if I want to stick with my approach above... So I can't use a placeholder then I think??? I dunno!
Sorry, you've lost me here. Why do you need to insert into a Label?
John_Mc is online now  
Advertisement
08-04-2009, 10:32   #10
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
Quote:
Originally Posted by John_Mc View Post
Sorry, you've lost me here. Why do you need to insert into a Label?
Thanks for sticking with me so far John... This is my problem here, I use an appended string to display the shopping cart contents back to the aspx page, and then I display the value of the appended string in a Label. What I'm trying to do is come up with some way of putting a LinkButton that will delete, into the appended string, that goes into the Label.

If I can't do this, I think I'll have to go backwards and come up with another way of displaying the shopping cart contents in the aspx page.
Darragh29 is offline  
08-04-2009, 10:35   #11
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
Thanks for sticking with me so far John... This is my problem here, I use an appended string to display the shopping cart contents back to the aspx page, and then I display the value of the appended string in a Label. What I'm trying to do is come up with some way of putting a LinkButton that will delete, into the appended string, that goes into the Label.

If I can't do this, I think I'll have to go backwards and come up with another way of displaying the shopping cart contents in the aspx page.
Why cant you use a placeholder or panel instead of the label?

You can add any control you want to the container without it being constrained to html markup (as would be the case in a label).

You can still use the Appended string, but populate the container instead of the label.
John_Mc is online now  
08-04-2009, 11:20   #12
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
Quote:
Originally Posted by John_Mc View Post
Why cant you use a placeholder or panel instead of the label?

You can add any control you want to the container without it being constrained to html markup (as would be the case in a label).

You can still use the Appended string, but populate the container instead of the label.
OK, I'll give it a shot, I'm just nervous changing it because apart from the delete item issue, it works perfectly.... But I'll copy the page and change the new copy and see how I fare out...
Darragh29 is offline  
08-04-2009, 11:27   #13
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
OK, I'll give it a shot, I'm just nervous changing it because apart from the delete item issue, it works perfectly.... But I'll copy the page and change the new copy and see how I fare out...
Yeah make a backup of it so you have that to fall back on should thinks go wrong.

The method I've suggested is relatively easy to implement and will be valuable learning for you. You may encounter problems with Postback but we can cross that bridge when we come to it.

You'll probably be able to reuse most of your code, except where it sets the label value, instead create a new label with the value and add it to the panel/placeholder.

Label lb = new Label();
lb.ID="lblCartRow";
lb.Text="Blah Blah";
lb.CssClass="cartLabel";
//Add to panel.

panCar.Controls.Add(lb);

If you're intending on having rows of these labels, then insert a literal control with "<div style="width:100%;">" before and </div> afterwards.

You will also put your delete button in between these, and any other buttons you may want.

Last edited by John_Mc; 08-04-2009 at 11:31. Reason: Clarity
John_Mc is online now  
08-04-2009, 13:56   #14
Darragh29
 
Join Date: Jun 2006
Posts: 6,956
OK, I have my container working, I can read a field from my DB and click on my purchase button and it appears in the container. Do I just repeat this process to get the rest of the information that I had appended into my label, to now appear in the container?
Darragh29 is offline  
08-04-2009, 14:53   #15
John_Mc
Registered User
 
Join Date: May 2005
Posts: 2,453
Quote:
Originally Posted by Darragh29 View Post
OK, I have my container working, I can read a field from my DB and click on my purchase button and it appears in the container. Do I just repeat this process to get the rest of the information that I had appended into my label, to now appear in the container?
The answer to that really depends on what your requirements are...

So you have the items displaying in the container, and you now want to add a delete button. Is that where you want to go from here?

You just need to add some more logic to the loop which creates a Button/Linkbutton with command arguments that can be used to identify that record in the delete event handler. If you've added the cart item to the DB as a record, you can set the command arg to the records ID.
Set the button event handler and finally, add the button to the panel.
John_Mc is online now  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet