RecipeDan
asked on
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 ("CheckBox 1");
chkvalue = chkBox.Text;
}
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
chkvalue = chkBox.Text;
}
do you mean the chkBox.Checked property?
Check this out. This might help.
https://www.experts-exchange.com/questions/24242283/Get-value-of-checkbox-in-populated-check-box-list-or-data-list.html
https://www.experts-exchange.com/questions/21089095/ASP-Net-VB-Net-DataList-How-to-reset-all-the-checkboxes-in-a-datalist.html
https://www.experts-exchange.com/questions/22096099/CheckBox-in-a-DataList.html
https://www.experts-exchange.com/questions/24242283/Get-value-of-checkbox-in-populated-check-box-list-or-data-list.html
https://www.experts-exchange.com/questions/21089095/ASP-Net-VB-Net-DataList-How-to-reset-all-the-checkboxes-in-a-datalist.html
https://www.experts-exchange.com/questions/22096099/CheckBox-in-a-DataList.html
ASKER
Hosneylk:
When I do this
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl ("CheckBox 1");
chkvalue = chkBox.checked;
}
I get this error: Identifier expected ;checked' is a keyword
When I do this
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl
chkvalue = chkBox.checked;
}
I get this error: Identifier expected ;checked' is a keyword
it should be Checked not checked
ASKER
Well now I get a different error:
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl ("CheckBox 1");
chkvalue = chkBox.Checked;
}
Error: Cannot implicity convert type bool to string
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl
chkvalue = chkBox.Checked;
}
Error: Cannot implicity convert type bool to string
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.
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.
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)
bool chkvalue;
Checked property returns boolean result (true or false)
ASKER
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 ("CheckBox 1");
chkValue = chkBox.Checked.ToString();
}
Alfred1: With this addition bool chkvalue;
bool chkvalue;
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl ("CheckBox 1");
chkValue = chkBox.Checked;
}
I get the same error as above.
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl
chkValue = chkBox.Checked.ToString();
}
Alfred1: With this addition bool chkvalue;
bool chkvalue;
foreach(DataListItem item in DataList1.Items)
{
CheckBox chkBox = (CheckBox)item.FindControl
chkValue = chkBox.Checked;
}
I get the same error as above.
Please note that C# is case-sensitive. chkValue is not the same as chkvalue.
ASKER
Yes....I figured that one out. I still get the same error though
what's the error? what do you mean by value not passed? where are you passing/using this value?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am trying to get the checkbox value from this DataList:
<asp:DataList RepeatColumns="5" RepeatDirection="Horizonta l" ID="DataList1" runat="server" DataSourceID="DataListSour ce">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" value="75" />
<%# DataBinder.Eval(Container. DataItem, "Category") %>
</ItemTemplate>
</asp:DataList>
<asp:DataList RepeatColumns="5" RepeatDirection="Horizonta
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" value="75" />
<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:DataList>
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
http://msdn.microsoft.com/en-us/library/4s78d0k1%28v=VS.90%29.aspx
do you want to get the value of checkbox1 when it's checked? if so you could use CheckBox1.Attributes["valu e"] since there is no native value property for the checkbox control in asp.net
e.g.
if CheckB
chkvalue = CheckBox1.Attributes["valu e"].ToStri ng();
e.g.
if CheckB
chkvalue = CheckBox1.Attributes["valu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hosneylk...That worked.
Thank you both your help!!!
Thank you both your help!!!