• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

CheckBox Values in Datalist

Hello:

I am trying to get the checkbox value in the datalist. There is no error but whether I put the value in a database or a label it does noty show.

Here is the code:

                foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1");
                    chkvalue = chkBox.Text;
                }
 
0
RecipeDan
Asked:
RecipeDan
  • 6
  • 6
  • 5
2 Solutions
 
hosneylkCommented:
do you mean the chkBox.Checked property?
0
 
RecipeDanAuthor Commented:
Hosneylk:

When I do this                
               foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1");
                    chkvalue = chkBox.checked;
                }

I get this error: Identifier expected ;checked' is a keyword
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hosneylkCommented:
it should be Checked not checked
0
 
RecipeDanAuthor Commented:
Well now I get a  different error:

                foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1");
                    chkvalue = chkBox.Checked;
                }

Error: Cannot implicity convert type bool to string
0
 
hosneylkCommented:
by value of checkbox you must mean if its checked or not right?

if you want to get it as a string you can do

chkValue = chkBox.Checked.ToString();

or else you can change the datatype of chkValue to bool.
0
 
Alfred A.Commented:
This means chkvalue was declared as string and not bool.  Try changing the declaration of chkvalue to

bool chkvalue;

Checked property returns boolean result (true or false)
0
 
RecipeDanAuthor Commented:
With this change. I get no error but no value is passed neither. I even hard coded my value.

                foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1");
                    chkValue = chkBox.Checked.ToString();
                }

Alfred1: With this addition bool chkvalue;

                bool chkvalue;
               foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1");
                    chkValue = chkBox.Checked;
                }

I get the same error as above.
0
 
Alfred A.Commented:
Please note that C# is case-sensitive.    chkValue is not the same as chkvalue.
0
 
RecipeDanAuthor Commented:
Yes....I figured that one out. I still get the same error though
0
 
hosneylkCommented:
what's the error? what do you mean by value not passed? where are you passing/using this value?
0
 
Alfred A.Commented:
OK.  FindControl in there should work.  What exactly the error are you getting now?

Also, try the following just to force the lookup of the control at all levels using a FindControlRecursive function and see if it works or not.
foreach(DataListItem item in DataList1.Items)
                {
                    CheckBox chkBox = (CheckBox) FindControlRecursive(item,"CheckBox1");
                    bool chkvalue = chkBox.Checked;
                } 



public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;

    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }
    return null;
}

Open in new window

0
 
RecipeDanAuthor Commented:
I am trying to get the checkbox value from this DataList:


            <asp:DataList RepeatColumns="5" RepeatDirection="Horizontal" ID="DataList1" runat="server" DataSourceID="DataListSource">
            <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" value="75" />
            <%# DataBinder.Eval(Container.DataItem, "Category") %>
            </ItemTemplate>
            </asp:DataList>

0
 
Alfred A.Commented:
Your CheckBox control in there, it looks like there is no value property for CheckBox.  Check the following link:

http://msdn.microsoft.com/en-us/library/4s78d0k1%28v=VS.90%29.aspx
0
 
hosneylkCommented:
do you want to get the value of checkbox1 when it's checked? if so you could use CheckBox1.Attributes["value"] since there is no native value property for the checkbox control in asp.net

e.g.
if CheckB
chkvalue = CheckBox1.Attributes["value"].ToString();
0
 
hosneylkCommented:
do you want to get the value of checkbox1 when it's checked? if so you could use CheckBox1.Attributes["value"] since there is no native value property for the checkbox control in asp.net

e.g.
if (CheckBox1.Checked) chkvalue = CheckBox1.Attributes["value"].ToString();
0
 
RecipeDanAuthor Commented:
Hosneylk...That worked.

Thank you both your help!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now