Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2011, 11:46 AM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default Chapter 4 Try It Out Working with List Controls

I can get this try it out to run without an error, however when I select c# using the CheckBoxList1 and click the button Label1 will not report I selected c#. The selections Visual Basic and CSS are reported by Label1, just not c#. Not sure what is going on here.

The DropDownList1 runs just fine.

EDIT: ISSUE RESOLVED
I just used the source code (DUH!) and everything runs fine, however I cannot seem to discover WTF it was I missed, fat fingered or left out! Oh well....

JJ

Last edited by jpjamie; September 20th, 2011 at 12:07 PM..
 
Old September 25th, 2011, 01:50 PM
Friend of Wrox
 
Join Date: Apr 2010
Posts: 125
Thanks: 20
Thanked 3 Times in 3 Posts
Default

Hi, can you past your code in vb and c#?
__________________
Please excuse me for poor and bad English.
 
Old September 25th, 2011, 03:13 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by morteza View Post
Hi, can you past your code in vb and c#?
Morteza: my posting above may have confused a bit. First of all for reference, the code example is from the Try It Out which starts on page 114 and goes page 117.

The code is in C# not Visual Basic (VB).

Unfortunately my posting may have given the impression I had written both C# and VB code examples which is not the case. The code just uses C# and Visual Basic as select choices.

I don't know why, but when I ran the code (opened ListControl.aspx) in my browser it did not run properly. When I copied and used the books code it ran perfectly.

Here is the code for the Try It Out:
First the the aspx file which is named ListControls.aspx. I am assuming here you know HTML, so here is just the code between the <form> tags...
Code:
<form id="form1" runat="server">
<div>
<asp.DropDownList ID="DropDownList1" runat="server">
<asp.ListItem>C#</asp.ListItem>
<asp.ListItem>Visual Basic</asp.ListItem>
<asp.ListItem>CSS</asp.ListItem>
</asp.DropDownList>

<asp.CheckBoxList ID="CheckBoxList1" runat="server">
<asp.ListItem>C#</asp.ListItem>
<asp.ListItem>Visual Basic</asp.ListItem>
<asp.ListItem>CSS</asp.ListItem>
</asp.CheckBoxList>

<asp.Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

<br />
<br />

<asp.Label ID = "Label1" runat="server" Text="Label"></asp.Label>

</div>
</form>
The first time I posted the above code I had the closing tag for the CheckBoxList as DropDownList, so corrected that one! These errors are probably why my code failed initially LOL! Hope this all helps!

Here is the ListControls.aspx.cs file, which is the C# Code Behind file for the above page...

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Site_Demos_ListControls : System.Web.UI.Page
{
     protected void Page_Load(object sender, eventArgs e)
     {
     
      }
     protected void Button1_Click(object sender, EventArgs e)
     {
          Label1.Text = "In the DDL you selected" + DropDownList1.SelectedValue + "<br />";
           
           foreach (ListItem item in CheckBox1.items)
           {
               if (item.Selected == true)
                 {
                    Label1.Text += "In the CBL you selected" + item.Value + "<br />";
                  }
             }
         }
}
The first time I posted the code above I had Button2_Click listed which I corrected to Button1_Click. Sorry if that caused an error as you might not refresh and see the change!

Last edited by jpjamie; September 25th, 2011 at 03:22 PM.. Reason: Code error fix
 
Old September 25th, 2011, 04:54 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi jpjamie,

Thanks for the detailed follow up, even if the problem was already resolved. One thing caugh my attention though:

Code:
public partial class Site_Demos_ListControls : System.Web.UI.Page
This implies that Site is a folder within your web site, and not the root of the site. E.g. you opened C:\BegASPNET instead of C:\BegASPNET\Site.

This may not be an issue now, but will become one in later chapters when many of the files are assumed to be in the root.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 26th, 2011, 05:52 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Imar View Post
Hi jpjamie,

Thanks for the detailed follow up, even if the problem was already resolved. One thing caugh my attention though:

Code:
public partial class Site_Demos_ListControls : System.Web.UI.Page
This implies that Site is a folder within your web site, and not the root of the site. E.g. you opened C:\BegASPNET instead of C:\BegASPNET\Site.

This may not be an issue now, but will become one in later chapters when many of the files are assumed to be in the root.

Cheers,

Imar
Just reread page 37 Creating and Opening a New Web Site and appreciate what you pointed out! Thanks!

Now that we're kind of on this subject, I wrote code for two online exams (running locally) this weekend basically using the code below for radiobuttons instead of Checkboxes for each of the questions. One exam has 24 questions so I ended up with a lot of of closing "}" braces for both the radio buttons and the (comments) text boxes. While the code runs fine, I feel it could be written more efficiently possibly using a grouping or an array approach. One could say "if it works, don't fix it" and I'm happy to have the exams run, but while it's part of the learning experience there's always a better way.



Code:
foreach (ListItem item in CheckBox1.items)
           {
               if (item.Selected == true)
                 {
                    Label1.Text += "In the CBL you selected" + item.Value + "<br />";
                  }
             }

JJ

Last edited by jpjamie; September 26th, 2011 at 06:28 PM..
 
Old September 27th, 2011, 02:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I guess it depends on the questions and the way they are designed (and type of answers they have).

You could use FindControl on the parent class to find controls by name dynamically. For example:

for (int i = 0; i < 10; i++)
{
CheckBoxList list = FindControl("CheckboxList" + i.ToString()) as CheckBoxList;
}

This would find 10 lists number from 0 to 9 in your page. It might do the trick for you, or you may need a different solution. It all depends.... ;-) It may give you some ideas though.

Cheers,

BTW: It's often better to post a new reply in the thread than to edit the original. I almost overlooked your change and started searching the book for an answer based on your original post I received by e-mail.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old September 27th, 2011, 10:24 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Thanks for the advice on the new reply instead of editing, glad you didn't miss what I posted.

My exam has only multiple choice (A, B, C, D) questions and as I previously posted I have basically the code below for finding what the user selects...
Code:
foreach (ListItem item in CheckBox1.items)
           {
               if (item.Selected == true)
                 {
                    Label1.Text += "In the CBL you selected" + item.Value + "<br />";
                  }
             }
The code above works for finding the one true (selected) radio button, but what if I want to provide the user a notice when a question is NOT answered. If you use the above conditional code to check for false, it loops through all four (foreach) answers, finds four false and the label returns four times. I would just like one label return stating "you missed question 6" or whatever other question they miss. I'll figure something out. Don't want you spoon feeding me here!
 
Old September 28th, 2011, 02:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

There are a few ways to do that.

1. Using conditional logic:

Code:
 
bool hasItem;
foreach (ListItem item in CheckBox1.items)
{
  if (item.Selected == true)
  {
    Label1.Text += "In the CBL you selected" + item.Value + "<br />";
    hasItem = true;
  }
}
if (!hasItem)
{
  // No item selected
}
This code sets hasItem to true when it encounters a selected item. It's never reset to false, so if after the loop it still has a value of false, no checkbox was checked.

2. You can check one of the Selected properties on the control. For example, if SelectedItem or SelectedValue is not nothing / an empty string, at least one checkbox was checked.

3. Go wild, and implement your own CheckBoxListValidator ;-)

http://codeclimber.net.nz/archive/20...ckBoxList.aspx
http://www.google.com/#sclient=psy&h...xListValidator

Hope this helps.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
jpjamie (November 24th, 2011)
 
Old November 24th, 2011, 06:54 PM
Authorized User
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default Girdview Limitations

Imar: I haven't been on here in a while but have been working carefully along with this book and on my second go through as I missed a few key things (which you caught above...thanks!) at first. DUH! Also, your suggestions on using conditional logic worked perfectly for me.

As per my above postings, I've made further progress coding my online quiz. I now validate user input and when the submit button is pressed the user is taken to another page where he/she sees the answers they selected along with their comments. At this stage, ColdFusion would have me utilizing CRUD for database interactivity, but this is a totally different game.

So at this point, CRUD (database connection etc.) with ASP.NET seems abstracted in comparison to ColdFusion. While I'm sure there ARE specifics, Visual Studio's Gridview works locally (in other words on my PC) as a demonstrator of database connectivity, but it doesn't seem to work well with real world projects.

What's frustrating is that I'm able to code everything in my online quiz BUT get database CRUD figured out. I may be getting ahead of myself a bit too much, but while Gridview is a Visual Studio "turnkey" example do real world online aps require more complexity (ADO?) to do the same?

JJ

Last edited by jpjamie; November 24th, 2011 at 08:57 PM..
 
Old November 25th, 2011, 08:52 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
but it doesn't seem to work well with real world projects.
Can you elaborate on that? I use GridViews many times in real-world applications.

Quote:
but while Gridview is a Visual Studio "turnkey" example do real world online aps require more complexity (ADO?) to do the same?
No, although you could if you wanted to. You can hook up a GridView to variosu data sources for stuff like display, edit and delete. In additon you can use a DetailsView / FormView or a hand coded solution as explained in my book.

Since I don't know what problems you're having, I can't recommend how to fix them....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax Extender Controls not working dotnetDeveloper ASP.NET 4 General Discussion 1 May 27th, 2010 04:33 PM
Chapter 1 Page 24 Data Grid + Linked List Box not working VS2008 nineismine BOOK: Beginning ASP.NET 2.0 and Databases 0 May 10th, 2009 03:11 PM
Problem with Try it Out List Controls phage BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 September 15th, 2008 02:15 PM
working with 2 user controls hossrad C# 9 May 13th, 2008 09:21 AM
ASP List Controls rsearing ASP.NET 2.0 Basics 0 September 25th, 2006 09:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.