Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a checkboxlist and it's values are "Max Payne","Hitman","Call Of Duty" etc. Now my question is that whenever i"ll click on any or all of these checkboxes then there values("Max Payne","Hitman" etc.) will be displayed in a listbox(listbox is there in my webform). please give full coding in C#.
Thanx in advance.....
Posted 18-Jan-13 4:34am
Edited 18-Jan-13 5:24am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is not a full coding, its more like hints.
However, I do feel that it is almost near a full coding.
 
try to fill in the blanks for the rest.
 
Every control in ASP.NET has a property called AutoPostBack.
AutoPostBack is functioned like a button's click event.
If there is any changes made to control, it will carry out a postback event.
Next, the ASP.NET control need a action event for you to handle in code behind.
In your case, you need the CheckBoxList's SelectedIndexChanged action event.
 
Example of coding in ASP.NET page:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
    onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
This will handle the postback event of selected index changed:
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do something
}
 
Example of coding in code behind
protected void Page_Load(object sender, EventArgs e)
{
    CheckBoxList1.AutoPostBack = true;
    CheckBoxList1.SelectedIndexChanged += new EventHandler(CheckBoxList1_SelectedIndexChanged);
}
 
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do something
}
  Permalink  
v4
Comments
Marcus Kramer at 18-Jan-13 11:25am
   
+5. Nice. You've given enough information to get him started without giving him all the details so he has to think a bit for himself.
Chiklu.Soumya at 18-Jan-13 11:58am
   
I"m completely new to asp .net and don't know anything. Excuse my mistakes. Please try to solve my code. My requirement is:"whenever i"ll click on one or more checkboxlist there value/s will be displayed on listbox." Thanks.....
 

<form id="form1" runat="server">
<div>

<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem>Max Payne
<asp:ListItem>Call Of Duty
<asp:ListItem>Hitman
<asp:ListItem>Shader
<asp:ListItem>Mafia


</div>
<asp:ListBox ID="ListBox1" runat="server" Height="146px" Width="134px">

</form>
 

protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList1.SelectedIndexChanged += new EventHandler(CheckBoxList1_SelectedIndexChanged);
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox1.Items.Add(CheckBoxList1.SelectedItem.ToString());
}
adriancs at 18-Jan-13 12:06pm
   
Hi, your question asked that how to do it without a button. So which means you know how to do it if using a button?
 
by the way, if you declare the CheckBoxList1_SelectedIndexChanged event in ASP.NET page like this:
 
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">

 
You don't need to declare it in code behind:
 
protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList1.SelectedIndexChanged += new EventHandler(CheckBoxList1_SelectedIndexChanged);
}

 
and if you do it in code behind, you won't have to do it in ASP.NET page.
Chiklu.Soumya at 18-Jan-13 12:26pm
   
Don't know how to display values using button. Please give me a full code for my convenience. Bcoz i"m completely new to asp.net.
adriancs at 18-Jan-13 12:58pm
   
new to asp.net is not a problem. here,
 
for tutorial of using ListBox, you may have a look at here:
http://www.dotnetspark.com/kb/637-simple-example-with--listbox.aspx
 
for tutorial of using CheckBoxList, you may have a look at here:
http://www.youtube.com/watch?v=xmJPxCn3s0Q
http://www.dotnetcurry.com/ShowArticle.aspx?ID=77
 
you may post back comments if you have any question.
adriancs at 18-Jan-13 13:20pm
   
To the best advise for beginner of ASP.NET
 
ASP.NET is a technology that built on top of HTML, CSS and JAVASCRIPT.
Before you start with ASP.NET, you must get your feet wet into html and css.
 
have some patient, read all through the topic about html at here:
http://www.w3schools.com/html/default.asp
 
then read all the topic about css at here:
http://www.w3schools.com/css/default.asp
 
then try to have some understanding of Javascript at here:
http://www.w3schools.com/js/default.asp
 
Without the knowledge of these 3, you will feel that something seems to be missing and you can't really perform.
 
ASP.NET can be described as a dynamic html generator. At the mean time ASP.NET runs processing logic in behind, it also generates HTML document and transmits it through internet to your client. And Javascript helps to process logic at client's computer. Javascript can dynamically generates or change the HTML at client's computer too. Javascript also helps to collect data from client and post it back to server for C# to process data.
 
Understanding HTML, CSS and Javascript will help you to understand the life circle of website better. You will eventually find that, ASP.NET do not fully generate all the HTML for you. Some of the parts will still require you to write HTML syntax manually. ASP.NET will help you combine the HTML syntax.
 
After that you can start to learn ASP.NET.
Here is a very good book for you to start off as a beginner.
A very nice book to read: Download this:
Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional
http://www.mediafire.com/view/?f24w1mwfl52b4jd
Chiklu.Soumya at 19-Jan-13 8:47am
   
Dear Adriancs thank u very much for ur useful links and special thanks for the asp.net ebook link.I"m remained ever grateful to you for your kind advice.
adriancs at 19-Jan-13 9:54am
   
Good luck. :)
adriancs at 19-Jan-13 10:05am
   
this is the code:
 
protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList1.AutoPostBack = true;
CheckBoxList1.SelectedIndexChanged += new EventHandler(CheckBoxList1_SelectedIndexChanged);
}
 
void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox1.Items.Clear();
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
ListBox1.Items.Add(li.Text);
}
}
}
Chiklu.Soumya at 19-Jan-13 12:15pm
   
Thank You Very Much. It works fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 380
2 OriginalGriff 236
3 Gihan Liyanage 210
4 kbrandwijk 204
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 405
4 thatraja 370


Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100