I am a fairly new web designer. I am working in Visual Web Developer 2010 Express using SQL Express 2008. I have the following site that I am setting up for a project.
As you can see, I have a link navigation on the left of the screen for Action genre of games. That then goes to this page that uses a SQLDataSource to filter that genre. The results are displayed in a ListView asp element. I have painstaking finally gotten
the formatting close to what I need.
However there are 3 things I need help with.
1.) I want to have each game image generated by the database be able to be clicked on and have it take you to the detailed game summary somehow passing the game ID to that page allowing it to display all the info about the game. I am unsure how to hyperlink
it based on the vaule of the game for each row in the ListView ItemTemplate. I also don't know how to pass the game ID for that image to the next page so that a SQLDataSource can use it as a where condition on which detailed game info to display.
2.) I want to add an asp Label element under the price (which is easy). But I want to be able to add VB code somehow to check the vaule of the Stock for that particular game. Then use the following psudo code:
{
if Stock = 0
Label.Text = "Out of Stock" (and be in Red font)
elseif Stock > 0
Label.Text = "In Stock" (and be in Green font)
}
I have been used to adding such code in the seperate VB file for each page, so I am not sure where or how I would accomplish this.
3.) I want to be able to click on the Shopping Cart image. I will probably change this to an asp Button element if I can change the button to look like the image. When clicked it would do something like the following psudo code:
{
if (Stock = 0) And (ReleaseDate > current date)
give an error message like "Sorry. This item has not yet been released. Please check back after " & ReleaseDate & "."
elseif (Stock = 0) And (ReleaseDate <= current date)
give an error message like "Sorry. We are currently out of stock for this item. Please check back later."
elseif (Stock > 0) And (UserID(Session) = -1)
give an error message like "You must log in before you can add an item to your cart."
elseif (Stock > 0) And (UserID(Session) > 0)
add the item to the users cart
}
I am guessing that once i make the image a button element that I can just add a click event for the button that I can put this code into. But I am not sure how to display an error message to the user. I am not sure how to check the ReleaseDate against the
current date. I am also not sure how to write the asp code to update the SQL row for the customers status to add the item in their cart. (Although I do know how to set up the SQL query to do this manually, I just am not sure the asp code to send the value
to it.)
Any help with any of these issues is very much greatly appreciated! I have managed to get this far by books or googling but I am getting stuck on this stuff as I am not sure how to find the info I need.
1) make the image an asp Image button <asp:ImageButton ID="ImageButton1" runat="server" /> so when you click the image it takes you to the page you want to navigate to. Put the code in the code behind of the image button (response.redirect("UrlOfPageToNavigateTo");
In the same code behind make a session variable Session["SessionName"] = TextBox1.Text; Then in the PageLoad of the page you redirect to catch that session, do something like ...
if (Session["SessionName"] != null)
{
string SessionValue= (string)Session["SessionName"];
...
}
Dont forget to clean up after yourself and do Session.Abandon();
2) Use a sqlconnection and sqlcommand to accomplish this. use executescalar() to query the database and place to the variable. Then set the label to that value. Is my first thought.
3) Once again make the image an image button <asp:ImageButton ID="ImageButton1" runat="server" />
double click on the image to create the code behind function and
*****After each if statement when you want to give an error simply create a label to display a message to the user.*****
78coolbreeze
{
if (Stock = 0) And (ReleaseDate > current date)
give an error message like "Sorry. This item has not yet been released. Please check back after " & ReleaseDate & "."
elseif (Stock = 0) And (ReleaseDate <= current date)
give an error message like "Sorry. We are currently out of stock for this item. Please check back later."
elseif (Stock > 0) And (UserID(Session) = -1)
give an error message like "You must log in before you can add an item to your cart."
elseif (Stock > 0) And (UserID(Session) > 0)
add the item to the users cart
}
Thanks for the reply. This will help me get started at least.
As far as cleaning up things. What if I was using a session variable to store the users ID number from pape to page to keep track of if they are logged in or not. If I use the Session.Abandon(), wouldn't that get rid of everything in the session? If so,
how would I clean up specific variables?
Sessions are only open for a limited amount of time, you create a variable, store the value from that session into that varaible do something with that variable and close the session. If you want to read more on sessions, take a look at this helpful link.
Hrmm. I was told in another thread that I asked about user accounts, that a session variable was stored until the user closed their browser. I had planned on using the session variable to store a -1 when no user was logged in and their customer ID when
they were stored in. That way in my master page I could have a page load event that checked if a user was in or not. Is this not the case?
hmmm ... not for sure the exact time, i use sessions frequently but never knew the exact time when they expire, thought they were only active for 20 mins or so (give or take a few minutes).
Cool. I will play around with it and see if I can get it to work. I have been using Sam's Teach Yourself ASP.NET 4 in 24 Hours. It is good but it mostly teaches you how to use Visual Web Developer. I just ordered these books to help complete my learning
experience.
Head First HTML and CSS (To learn html basics)
Head First Ajax (I guess this is for doing client side script with JavaScript or something)
Head First Web Design (To learn good design practices, not much of an artist, had a graphic designer friend do the work you have seen, I did the coding)
Pro ASP.NET 4 in VB 2010 (Hopefully this will teach me the syntax to get the database stuff working)
Head First SQL (To refresh my memory on stuff I learned in my Databases course this semester)
I really wasn't sure what I was going to do with my Computer Science degree but working with web sites and databases seems pretty fun.
Ok, I tried setting each image of the games to an ImageButton. Since I am inside a ListView element I can't double click on the ImageButton in the Design View. I tried looking in the dropdowns in the code behind view but there is nothing listed for this
button. So I assinged the OnUserClick to "ImageButtonClick" event. I made the event and wanted to output something to a label to see if it worked and I can't reference the Label.Text field inside the code behind. >< Using 3 programming languages all wrapped
around each other is a pain in the rear.
sounds like you can't reference an object from inside a listview.
See if this link will help you.
http://forums.asp.net/t/1764483.aspx/1
The one post I saw was ...
referencing object in listview from nested listview?
Protected Sub doSomething(sender As Object, e As ListViewItemEventArgs)
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim lnk As HyperLink = CType(e.Item.FindControl("innerHL"), HyperLink)
Dim parentItem As ListViewDataItem = DirectCast(e.Item.NamingContainer.NamingContainer, ListViewDataItem)
Dim imgName As Image = DirectCast(parentItem.FindControl("img"), Image)
lnk.Attributes.Add("onmouseover", "myFunction(" + theimg.ClientID + ");")
End If
End Sub
Marked as answer by Mark - MSFT on Nov 22, 2012 04:07 AM
78coolbreeze
Member
3 Points
27 Posts
How And Where To Add Code In The ItemTemplate?
Nov 15, 2012 10:30 PM|LINK
I am a fairly new web designer. I am working in Visual Web Developer 2010 Express using SQL Express 2008. I have the following site that I am setting up for a project.
As you can see, I have a link navigation on the left of the screen for Action genre of games. That then goes to this page that uses a SQLDataSource to filter that genre. The results are displayed in a ListView asp element. I have painstaking finally gotten the formatting close to what I need.
However there are 3 things I need help with.
1.) I want to have each game image generated by the database be able to be clicked on and have it take you to the detailed game summary somehow passing the game ID to that page allowing it to display all the info about the game. I am unsure how to hyperlink it based on the vaule of the game for each row in the ListView ItemTemplate. I also don't know how to pass the game ID for that image to the next page so that a SQLDataSource can use it as a where condition on which detailed game info to display.
2.) I want to add an asp Label element under the price (which is easy). But I want to be able to add VB code somehow to check the vaule of the Stock for that particular game. Then use the following psudo code:
{
if Stock = 0
Label.Text = "Out of Stock" (and be in Red font)
elseif Stock > 0
Label.Text = "In Stock" (and be in Green font)
}
I have been used to adding such code in the seperate VB file for each page, so I am not sure where or how I would accomplish this.
3.) I want to be able to click on the Shopping Cart image. I will probably change this to an asp Button element if I can change the button to look like the image. When clicked it would do something like the following psudo code:
{
if (Stock = 0) And (ReleaseDate > current date)
give an error message like "Sorry. This item has not yet been released. Please check back after " & ReleaseDate & "."
elseif (Stock = 0) And (ReleaseDate <= current date)
give an error message like "Sorry. We are currently out of stock for this item. Please check back later."
elseif (Stock > 0) And (UserID(Session) = -1)
give an error message like "You must log in before you can add an item to your cart."
elseif (Stock > 0) And (UserID(Session) > 0)
add the item to the users cart
}
I am guessing that once i make the image a button element that I can just add a click event for the button that I can put this code into. But I am not sure how to display an error message to the user. I am not sure how to check the ReleaseDate against the current date. I am also not sure how to write the asp code to update the SQL row for the customers status to add the item in their cart. (Although I do know how to set up the SQL query to do this manually, I just am not sure the asp code to send the value to it.)
Any help with any of these issues is very much greatly appreciated! I have managed to get this far by books or googling but I am getting stuck on this stuff as I am not sure how to find the info I need.
chjones2008
Member
258 Points
168 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 12:34 AM|LINK
1) make the image an asp Image button <asp:ImageButton ID="ImageButton1" runat="server" /> so when you click the image it takes you to the page you want to navigate to. Put the code in the code behind of the image button (response.redirect("UrlOfPageToNavigateTo"); In the same code behind make a session variable Session["SessionName"] = TextBox1.Text; Then in the PageLoad of the page you redirect to catch that session, do something like ...
if (Session["SessionName"] != null) { string SessionValue= (string)Session["SessionName"];78coolbreeze
Member
3 Points
27 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 12:40 AM|LINK
Thanks for the reply. This will help me get started at least.
As far as cleaning up things. What if I was using a session variable to store the users ID number from pape to page to keep track of if they are logged in or not. If I use the Session.Abandon(), wouldn't that get rid of everything in the session? If so, how would I clean up specific variables?
chjones2008
Member
258 Points
168 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 12:47 AM|LINK
Sessions are only open for a limited amount of time, you create a variable, store the value from that session into that varaible do something with that variable and close the session. If you want to read more on sessions, take a look at this helpful link.
78coolbreeze
Member
3 Points
27 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 01:02 AM|LINK
Hrmm. I was told in another thread that I asked about user accounts, that a session variable was stored until the user closed their browser. I had planned on using the session variable to store a -1 when no user was logged in and their customer ID when they were stored in. That way in my master page I could have a page load event that checked if a user was in or not. Is this not the case?
chjones2008
Member
258 Points
168 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 01:09 AM|LINK
hmmm ... not for sure the exact time, i use sessions frequently but never knew the exact time when they expire, thought they were only active for 20 mins or so (give or take a few minutes).
I just googled it and here is the link.
The default value is 20 minutes.
Anil Srivast...
Member
442 Points
292 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 02:19 AM|LINK
session.abandon
will only clear session specific variable
so that a user do not again logon using same session
Anil
78coolbreeze
Member
3 Points
27 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 02:26 AM|LINK
Cool. I will play around with it and see if I can get it to work. I have been using Sam's Teach Yourself ASP.NET 4 in 24 Hours. It is good but it mostly teaches you how to use Visual Web Developer. I just ordered these books to help complete my learning experience.
Head First HTML and CSS (To learn html basics)
Head First Ajax (I guess this is for doing client side script with JavaScript or something)
Head First Web Design (To learn good design practices, not much of an artist, had a graphic designer friend do the work you have seen, I did the coding)
Pro ASP.NET 4 in VB 2010 (Hopefully this will teach me the syntax to get the database stuff working)
Head First SQL (To refresh my memory on stuff I learned in my Databases course this semester)
I really wasn't sure what I was going to do with my Computer Science degree but working with web sites and databases seems pretty fun.
78coolbreeze
Member
3 Points
27 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 08:22 PM|LINK
Ok, I tried setting each image of the games to an ImageButton. Since I am inside a ListView element I can't double click on the ImageButton in the Design View. I tried looking in the dropdowns in the code behind view but there is nothing listed for this button. So I assinged the OnUserClick to "ImageButtonClick" event. I made the event and wanted to output something to a label to see if it worked and I can't reference the Label.Text field inside the code behind. >< Using 3 programming languages all wrapped around each other is a pain in the rear.
chjones2008
Member
258 Points
168 Posts
Re: How And Where To Add Code In The ItemTemplate?
Nov 16, 2012 11:22 PM|LINK
sounds like you can't reference an object from inside a listview.
See if this link will help you.
The one post I saw was ...
referencing object in listview from nested listview? Protected Sub doSomething(sender As Object, e As ListViewItemEventArgs) If e.Item.ItemType = ListViewItemType.DataItem Then Dim lnk As HyperLink = CType(e.Item.FindControl("innerHL"), HyperLink) Dim parentItem As ListViewDataItem = DirectCast(e.Item.NamingContainer.NamingContainer, ListViewDataItem) Dim imgName As Image = DirectCast(parentItem.FindControl("img"), Image) lnk.Attributes.Add("onmouseover", "myFunction(" + theimg.ClientID + ");") End If End Sub