Tangara G

Tangara G

  • NA
  • 265
  • 23.3k

How do I get the selected item to be shown?

Oct 28 2016 1:33 AM
Hi,
 
I am trying out how the selection item can work with Model layer but am not sure why I can't get the selected item shown?
 
Do I need to create a business logic in order for the selected item event to show the item that has been selected?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8.   
  9. namespace WebApplication3  
  10. {  
  11.     public partial class WebForm1 : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             Message.InnerText = "To make text bold, use the <b> tag.";  
  16.             Message1.InnerHtml = Server.HtmlEncode("Welcome! You accessed this page at: " + DateTime.Now);  
  17.           
  18.             if(! this.IsPostBack)  
  19.             {  
  20.                 //chklst.Items.Add("Python");  
  21.                 //chklst.Items.Add("Java");  
  22.                 //chklst.Items.Add("C");  
  23.             }  
  24.         }  
  25.   
  26.         protected void cmdOK_Click(object sender, EventArgs e)  
  27.         {  
  28.   
  29.             lblResult.Text = "You chose: ";  
  30.   
  31.             foreach(WebApplication3.Models.Item items in chklst.Items)  
  32.             {  
  33.                 if (items.Selected == true)  
  34.                 {  
  35.                     //Add text to label  
  36.                     lblResult.Text += "<br/>" + items.Text;  
  37.                 }  
  38.             }  
  39.             lblResult.Text = "</b>";  
  40.         }  
  41.           
  42.     } 


 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <b><span id="Message" runat="server"></span></b>  
  13.     </div>  
  14.     <div>  
  15.     <b><span id="Message1" runat="server"></span></b>  
  16.     </div>  
  17.     <div>  
  18.         <asp:CheckBoxList ID="chklst" runat="server" >  
  19.             <asp:ListItem Value="Python"></asp:ListItem>  
  20.             <asp:ListItem Value="Java"></asp:ListItem>  
  21.             <asp:ListItem Value="C"></asp:ListItem>  
  22.         </asp:CheckBoxList>  
  23.         <br /><br />  
  24.         <asp:Button ID="cmdOK" Text="OK" OnClick="cmdOK_Click" runat="server" />  
  25.         <br /><br />  
  26.         <asp:Label ID="lblResult" runat="server" />  
  27.     </div>  
  28.       
  29.     </form>  
  30. </body>  
  31. </html> 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace WebApplication3.Models  
  7. {  
  8.     public class Item  
  9.     {  
  10.         private List<Item> items;  
  11.   
  12.         public List<Item> Items  
  13.         {  
  14.             get { return items; }  
  15.             set { items = value; }  
  16.         }  
  17.   
  18.         public bool Selected { get; set; }  
  19.   
  20.         public string Text { get; set; }  
  21.     }  
  22. }
  23.                     

Answers (1)