Solved

CheckBox Values in Datalist

Posted on 2010-11-25
17
693 Views
Last Modified: 2012-05-10
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
Comment
Question by:RecipeDan
  • 6
  • 6
  • 5
17 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34215061
do you mean the chkBox.Checked property?
0
 
LVL 21

Expert Comment

by:Alfred1
ID: 34215069
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215122
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
 
LVL 10

Expert Comment

by:hosneylk
ID: 34215137
it should be Checked not checked
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215144
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
 
LVL 10

Expert Comment

by:hosneylk
ID: 34215150
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
 
LVL 21

Expert Comment

by:Alfred1
ID: 34215152
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
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215171
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 21

Expert Comment

by:Alfred1
ID: 34215176
Please note that C# is case-sensitive.    chkValue is not the same as chkvalue.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215195
Yes....I figured that one out. I still get the same error though
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34215203
what's the error? what do you mean by value not passed? where are you passing/using this value?
0
 
LVL 21

Assisted Solution

by:Alfred1
Alfred1 earned 100 total points
ID: 34215207
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
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215217
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
 
LVL 21

Expert Comment

by:Alfred1
ID: 34215223
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
 
LVL 10

Expert Comment

by:hosneylk
ID: 34215224
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
 
LVL 10

Accepted Solution

by:
hosneylk earned 400 total points
ID: 34215226
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
 
LVL 1

Author Comment

by:RecipeDan
ID: 34215242
Hosneylk...That worked.

Thank you both your help!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now