I'm not sure if I said that clearly enough, :). What I'm trying to do is, seeing as how I'm not able to do a Shopping Cart at the moment, I'm trying to make it so if you select Item33 or something by clicking the 'Order' button, when it redirects to the Order page the Item33 will be the default value in the drop-down box.

Any ideas? Thanks for reading!

Recommended Answers

All 6 Replies

send it via form with "post" and request the form there. Otherwise, send it via server form and request it there..

or do it the easy way and just set a session variable and request it there.. :)

Session("selecteditem") = "Item33"

<DropDownList....>
<% if Session("selecteditem") <> "" or Len(Session("selecteditem")) > 1 then %>
<option value="<%= Session("selecteditem") %>" selected><%=Session("selecteditem") %></option>
<% end if %>
</DropDownList...>

(if you're using a control, instead of <option value...> to <asp:ListItem Text="Session..." Value="Session..." />)

Oh, that's on page. If you wish to do it server-side, just do it like below:

DropDownListName.Items.Insert(Session("selecteditem"), Session("selecteditem"))
DropDownListName.SelectedValue = Session("selecteditem")

Thanks, :). I'll give this a shot this evening and will reply further when I get it up and working.

By the way, the "selecteditem" - would that be the name, or...? What I've done is set up a button link that the user will click to get to the order form, so is the name of that button link on the shop page assigned to the session as in Session("buttonLinkName")="Item33" ?

Yes, the selecteditem would be the name. Name the session variable anything you would like. I just chose to name it "selecteditem" with the value of the dropdownlist's selected item (item33).

What this session variable should be used for is just keeping selection across pages, UNLESS your postback URL is the new page. If that is the case, then you should just request.form("dropdownlist") and avoid session variables.

Exactly what I mean:
When a user clicks the dropdownlist and selects one value, your backend code is posted back to the same page and turns that selected item into a session variable called "session("selecteditem")". Then, on the order page, the selecteditem session variable is referenced in to the dropdownlist. That is initially what I thought you were asking for.

What I suggest:
Unless this is not possible, do a postbackUrl to your order page. This way you can just reference in whatever you need to by using "request.form" methods. If this is not possible, use session variables or text files to hold your values. Session variables drop periodically so it would be cumbersome to any visitor if their entire shopping cart emptied or what not. You can also use cookies, but lots of people have them disabled, but many more do not. ultimately, I would suggest just loging the person's IP address into a database and hold your information in there. It prevents any lost catastrophes and is.. well I believe to be good and secure, just less efficient.

Let me put it this way. I'm very new to ASP.NET, and I'm using controls at the moment. Is this the easiest way, considering I'm still in the progress of learning and practising ASP.NET, or is there an easier method? I'm still very willing to use the Session method, though. I'm adding to your reputation for helping me thus far, :).

Oh, and the drop-down list is part of the Order Form. I wish it to be so that on the product page, when the button is clicked, the user proceeds automatically do the order form with the product name drop-down value to be selected. Like, a Ceramic Pot might have a value of 1 outside of the drop-down, and I wish that value to be picked up, so that the drop-down box will display the item with a value of 1 as default - so the user doesn't need to select the item manually. I would like it to be so that the user doesn't need to search through the list and click an item. And this isn't a shopping cart really, it's just an order form to order a product. I'm nowhere near knowledgeable enough to develop something that complex. In other words, I wish the drop-down to change the default displayed value within it according to the link that is clicked. Sort of like changing a picture dynamically with 3 links.

Then, the easiest solution for you, use the session variable. Have the page postback to you with a form runat=server tag. do this for your submit button:

<script language="vb" runat="server">
Sub btnSubmit_Click(ByVal S as Sender, ByVal E As EventArgs)
Session("selectedItem") = Trim(yourdropdownlistname.SelectItem.Text)
response.redirect("order.aspx")
'or if it is all on the same server, use the below line:
Server.Transfer("order.aspx")
End Sub
</script>

<form runat="server".
...
...
<asp:Button id="btnSubmit" onClick="btnSubmit_Click" Text="proceed to order" runat="server" />
</form>

'''''''''''''''''''''''''''''' Order page:

<script language="vb" runat="server">
Sub Page_Load(ByVal S As Sender, ByVal E As EventArgs)
yourotherdropdownlistname.Items.FindByValue(Session("selectedItem")).Selected = True
End Sub
</script>

Now the above will throw an error to your page if the SelectedItem is not found on your order page. If you wish to bypass it, just throw an easy if else statement and trade out the script above for this one:

<script language="vb" runat="server">
Sub Page_Load(ByVal S As Sender, ByVal E As EventArgs)
Dim ddl As List Item
ddl = yourotherdropdownlistname.Items.FindByValue(Session("selectedItem"))
if Not ddl is Nothing then
  ddl.Selected = True
end if
End Sub
</script>

This will try to set the value, and if the value is not found on your dropdownlist, then your user will have to select it himself. This will not throw an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.