Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help how can i give default selection to my checklist on checkbox selection
for eg:
<asp:checkbox id="chkbox1" runat= "server"/>

This is my main checkbox an on this selection my checklist value should be default true so that user can see the selection and i can save it as a parameter in my stored procedure
<asp:checkboxlist id="chkoptn" runat="server>
<asp:listitem>1<asp:listitem>
<asp:listitem>2<asp:listitem>
<asp:listitem>3<asp:listitem>
<asp:listitem>4<asp:listitem>


Please let me know how can i do
Posted
Comments
Sergey Alexandrovich Kryukov 8-Aug-12 2:14am    
After postback, new selection to become default? Or one default for all cases?
--SA

In the aspx side you can specify the selected="true" to the listitem

XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
            <asp:ListItem Selected="True">Item4</asp:ListItem>
        </asp:CheckBoxList>


You can set it from the code-behind also,
C#
CheckBoxList1.Items[1].Selected = true;
CheckBoxList1.Items[2].Selected = true;
 
Share this answer
 
Comments
priti2010 8-Aug-12 2:21am    
ok then how this value should be passed as parameter because if checked then values should get inserted
Prasad J 8-Aug-12 2:53am    
You can get the checked values from the checkboxlist as:
string values = getCheckBoxValues();
private string getCheckBoxValues()
{
string value = "";
for (int loopIndex = 0; loopIndex < CheckBoxList1.Items.Count; loopIndex++)
{
if (CheckBoxList1.Items[loopIndex].Selected)
value += "true,";
else
value += "false,";
}
return value.TrimEnd(',');
}

Also, you can preset the values by calling
presetChechbox("true,true,false,false");
private void presetChechbox(string value)
{
string[] values = value.Split(',');
// Ensure both chechbox itexs and string contain same number of items.
if (values.Length != CheckBoxList1.Items.Count)
return;

for (int loopIndex = 0; loopIndex < CheckBoxList1.Items.Count; loopIndex++)
{
bool selectedVal=false;
bool.TryParse(values[loopIndex], out selectedVal);
CheckBoxList1.Items[loopIndex].Selected = selectedVal;
}
}
This is the attribute selected="true" to be added to one of the choices (a listitem, in terms of ASP.NET controls).

—SA
 
Share this answer
 
v2
Comments
priti2010 8-Aug-12 2:18am    
i have did that but i give me list of values whose listitem is checked or unchecked but i want it by default true on main checkbox select
Sergey Alexandrovich Kryukov 30-Oct-12 0:30am    
Excuse me?
--SA

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