So I'm taking a simple C# program and trying to put it into a web page for an assignment
I have a sorted list, kennel, it's defined like this and I have two test items added to start with
public partial class WebForm1 : System.Web.UI.Page
{
private List<Dog> kennel;
protected void Page_Load(object sender, EventArgs e)
{
kennel = new List<Dog>();
kennel.Add(new Dog(1, "Ricky"));
kennel.Add(new Dog(2, "James"));
}
here's what Dog is
public class Dog
{
private int tag;
private string dogname;
public Dog()
{
}
public Dog(int tag, string dogname)
{
Tag = tag;
Dogname = dogname;
}
public int Tag
{
get { return tag; }
set { tag = value; }
}
public string Dogname
{
get { return dogname; }
set { dogname = value; }
}
}
Now when I display the list into a label with a foreach loop
foreach (Dog doge in kennel)
{
lstDisplay.Text = lstDisplay.Text + "Tag: " + doge.Tag.ToString() + " Name: " + doge.Dogname + "<br />";
}
The two test dogs show up just fine, exactly how I need
But when I go to add a new item to the list on a button press, it doesn't do it, displaying the list produces the same results
I've tried doing it with variables, I've even tried going extremely basic like this
protected void btnAdmit_Click(object sender, EventArgs e)
{
kennel.Add(new Dog(3, "butts"));
{
but it doesn't work. What's even more confusing to me is that the kennel.Count (which I have displayed in a label) shows me a count of 2 to start off with (the two predefined entries on pageload), goes to 3 when I add a new one, but stays at 3 no matter how many more times I hit the add button (and this even if I go so far to have two add buttons and one of them adds another predefined entry like this, to say nothing of using variables to define the tag and dogname)
I can't for the life of me figure out why this doesn't work
heres how the relevant controls are defined in the aspx
<asp:TextBox ID="txtDogName" runat="server" style="margin-left: 15px" Width="110px"></asp:TextBox>
<asp:Button ID="btnAdmit" runat="server" Text="Admit Dog" Width="210px" OnClick="btnAdmit_Click" />
<div style ="float: right"><asp:Label ID="lstDisplay" runat="server" Height="211px" style="margin-left: 0px; margin-top: 0px" Width="190px" /></div>
I feel like I'm taking crazy pills
Posts
It correctly displays any items added on page load, but none added via button (even if I literally just cut it from the page load and paste it to the button)
the button is firing correctly too because any other functions I put on it work fine (and adding an item to a list with it causes my list counter to update, however it only adds one item on the list counter no matter how many I try to add with the button)
idk why the title of the assignment is sorted list, since it's not what I'm asked to do
Nothing is really saved, and it's all reset back to the defaults of your original page load like kamiro said. So you have to keep that all in mind when designing those events and button clicks.
It almost seems like the instructor is trying to make it easy on you guys by letting you do ASP and web based for your UI but ignores the key sticking point that they're basically stateless.
then I don't know how to do this assignment, because I need to have buttons that display the current list, remove one specific item from the list, display the list in alphabetical order, and display the list by the tag value
I don't know how to do that if it's stateless
this is the example program:
with furniture.cs in a seperate cs file in a different folder called App_Code (I dont know what that's about)
So my pageload looks like this now
I don't know if that's right, and if it is I don't know how to retrieve or manipulate it because everything from trying to display it to altering it causes a crash
I'm not using the Me.IsPostBack because that doesn't seem to work in C# at all
this is trying to add something to it
So when your program/page loads you'd initialize your kennel into the session. Something like:
Then when you get to a button click you'd do:
Then when you click the button to display you'd do:
I've never done ASP, but, if it's anything like non-asp c#, you'd need to cast your kennel back into its type in order to be able to use it.
hot damn I wish my professor would update his materials to c# instead of VB, or have labs, or lectures that say how to do anything, or respond to e-mails, or offer explanations instead of just deducting points
so far so good
thanks a lot!
When you add stuff on the button click you'll want to add it to the session variable (after casting it into the right list of objects). Probably you can save yourself some time by having a function on the page called SessionKennels or something that will grab the session variable and cast it into the right thing. That way you can go SessionKennels.Add blah blah instead of casting Session["kennels"] into the right type every time you want to use it.
The Me.IsPostback is a VB thing.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
Strongly typed languages are not so hot for web/stateless programming.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
well that's not fair, he did talk about sessions, they're mentioned in the slides
but just definitions, not practical instructions
I suspected as much from the past few posts about it in chat and this thread.
doesn't seem to work, I take it you can't use list.Contains with a session variable?
For something like "contains" in a list, they're known to use the equals operator, so you'd need to overload a whole bunch of stuff. That's pretty advanced at this stage in the game, so you want to keep it simple.
You'd want to loop through the list and see if you can find the tag number
foreach dog in kennel, etc.
seems to work, now I can make it give the appropriate message in the appropriate place
There was a pretty significant bug (heartbleed) caused exactly because of something like that.
EDIT: Posted before I saw your altered code.
Also, seriously consider the code snippet I pasted above, as having a function do your casting for you is infinitely preferable than doing it every time you want to get at it. You don't really need kennel at all. Instantiate the session variable with a new list of whatever in the !IsPostback bit of the page load, then refer to your function that does the casting for you.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
I don't even know the basic terminology half the time
and now I can't figure out how to remove things from lists, it just crashes
I've been trying to do this assignment for something like 9 hours now
And hang in there. Coding is hard, and learning is just failing slightly less often as you go.
Choose Your Own Chat 1 Choose Your Own Chat 2 Choose Your Own Chat 3
You can't remove things from a list while it's being enumerated over in C#.
There's a few ways to go about this, you can use a dictionary instead of a list, and use the tag number as the key and you can just remove it via the key with a simple call.
The other option is to keep record of the item you want to remove, then remove it after you find it (and break out of your loop), making sure it was actually found.
hopefully he'll post the code of how it's supposed to be done before the midterm
I really appreciate all the help, at least 6 out of 7 things on it work and I was 0 out of 7 before
I wish I could go to bed
I find that this is where properties really shine. Hide all your session monkeying in the getter and setter, then just use like a normal variable elsewhere.
It's really hard to learn if no one's teaching you.
If you've got LINQ available to you you can do this:
Though I have used similar syntax in some other collections to return subsets so maybe I've been LINQing this whole time and didn't know it.
Keep up the good work OP. With programming you never stop learning, so don't forget its supposed to be fun!
however dicking around with it I realized I don't actually have to delete the list, I can just recreate the list with every item on it except the one in the textbox
that seems to work