Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 18-Jan-13 5:24am
v2

1 solution

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:
XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
    onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>

This will handle the postback event of selected index changed:
C#
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do something
}


Example of coding in code behind
C#
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
}
 
Share this answer
 
v4
Comments
fjdiewornncalwe 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 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 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 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 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900