Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get the selected checkbox value in a label.....help me Please



C#
for (int itemcount = 0; itemcount < Rpt_Company.Items.Count; itemcount++)
           {
               CheckBox chkbox = ((CheckBox)Rpt_Company.Items[itemcount].FindControl("chkbox"));
               if (chkbox != null && chkbox.Checked == true)
               {

                 
               }
           }
Posted
Updated 4-May-12 1:25am
v2
Comments
Member 8405530 4-May-12 7:33am    
in repeater control each row consist of checkbox ...selected records must be stored in label...how to do it....how to sore the id values in a label...

If the requirement is to get the Text value of the Checked CheckBox and to assign to the Label then the following LINQ query can be used.

C#
CheckBox selectedCheckBox = 
    Rpt_Company.Items.OfType<checkbox>().FirstOrDefault (cb => cb.Checked );
if (selectedCheckBox != null) Label1.Text = selectedCheckBox.Text;

where Items is collection of Controls
 
Share this answer
 
v2
Comments
Member 8405530 4-May-12 8:01am    
Thank u...Instead of label i am using literal...is it the same steps to be followed
VJ Reddy 4-May-12 8:21am    
The above code returns the first selected CheckBox among the controls available in the Items control collection. If either a CheckBox is not available or check box is available but not checked then null is returned.
Now check for null as done in the second step and use the required property of the CheckBox returned. In the above code Text property is used. If you are using Literal class, then see the writable property to which the Text retrieved from the CheckBox can be assigned.
Thank you.
Member 8405530 4-May-12 9:19am    
n repeater control each row consist of checkbox ...selected records must be stored in string...how to do it....how to store the id values in a string...
C#
for (int itemcount = 0; itemcount < Rpt_Company.Items.Count; itemcount++)
           {
               CheckBox chkbox = ((CheckBox)Rpt_Company.Items[itemcount].FindControl("chkbox"));
   if (chkbox != null && chkbox.Checked == true)
    {
      Label lbl = (Label)Rpt_Company.Items[itemcount].FindControl("chkbox"));
        string lblid;
        lblid= lbl.text               
                 
    }
   }
}
 
Share this answer
 
v2
Comments
Member 8405530 4-May-12 7:59am    
Unable to cast object of type 'System.Web.UI.WebControls.CheckBox' to type 'System.Web.UI.WebControls.Literal'.
Thank u for the reply...help me

i am getting error like this...Instead of label i am using literal


for (int itemcount = 0; itemcount < Rpt_Company.Items.Count; itemcount++)
{
CheckBox chkbox = ((CheckBox)Rpt_Company.Items[itemcount].FindControl("chkbox"));
if (chkbox != null && chkbox.Checked == true)
{

// memberno += chkbox.Text;
//memberno += (chkbox.Text + "<br/>");
Literal lbl = (Literal)Rpt_Company.Items[itemcount].FindControl("chkbox");

string lblid;
lblid = lbl.Text;


}
}
Anil Honey 206 4-May-12 8:05am    
What is Literal it is any inbuilt class file
Anil Honey 206 4-May-12 8:05am    
U ask that to store the value in label text
Member 8405530 4-May-12 9:19am    
n repeater control each row consist of checkbox ...selected records must be stored in string...how to do it....how to store the id values in a string...

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