Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Answer
How do I get the selected item to be shown?
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
Tangara G
7y
194
1
Reply
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?
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
WebApplication3
{
public
partial
class
WebForm1 : System.Web.UI.Page
{
protected
void
Page_Load(object sender, EventArgs e)
{
Message.InnerText =
"To make text bold, use the <b> tag."
;
Message1.InnerHtml = Server.HtmlEncode(
"Welcome! You accessed this page at: "
+ DateTime.Now);
if
(!
this
.IsPostBack)
{
//chklst.Items.Add("Python");
//chklst.Items.Add("Java");
//chklst.Items.Add("C");
}
}
protected
void
cmdOK_Click(object sender, EventArgs e)
{
lblResult.Text =
"You chose: "
;
foreach(WebApplication3.Models.Item items in chklst.Items)
{
if
(items.Selected ==
true
)
{
//Add text to label
lblResult.Text +=
"<br/>"
+ items.Text;
}
}
lblResult.Text =
"</b>"
;
}
}
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"WebForm1.aspx.cs"
Inherits=
"WebApplication3.WebForm1"
%>
<!DOCTYPE html>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<b><span id=
"Message"
runat=
"server"
></span></b>
</div>
<div>
<b><span id=
"Message1"
runat=
"server"
></span></b>
</div>
<div>
<asp:CheckBoxList ID=
"chklst"
runat=
"server"
>
<asp:ListItem Value=
"Python"
></asp:ListItem>
<asp:ListItem Value=
"Java"
></asp:ListItem>
<asp:ListItem Value=
"C"
></asp:ListItem>
</asp:CheckBoxList>
<br /><br />
<asp:Button ID=
"cmdOK"
Text=
"OK"
OnClick=
"cmdOK_Click"
runat=
"server"
/>
<br /><br />
<asp:Label ID=
"lblResult"
runat=
"server"
/>
</div>
</form>
</body>
</html>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
WebApplication3.Models
{
public
class
Item
{
private
List<Item> items;
public
List<Item> Items
{
get {
return
items; }
set { items = value; }
}
public
bool
Selected { get; set; }
public
string Text { get; set; }
}
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Select different Text of Dropdown if all items value is same
next and previous pagination link in asp.net