samedi 11 avril 2020

How to check checkbox using DropDownCheckBoxes based on database values in C#

I need to retrieve the selected Checkbox values which are stored in database into the form.

I have created Checkbox list and Button to store Checked Values in ASP.Net application using C# as follows, using DropDownCheckBoxes

<asp:DropDownCheckBoxes ID="DropDownCheckBoxes1" runat="server"
    UseSelectAllNode="false"
    Enabled="true"
    ValidationGroup="ValidationSummary1"
    CssClass="body"
    UseButtons="true"
    OnSelectedIndexChanged="DropDownCheckBoxes1_SelectedIndexChanged"
    AutoPostBack="true">
    <Style SelectBoxWidth="400"
        DropDownBoxBoxWidth="250"
        DropDownBoxBoxHeight="200"
        SelectBoxCssClass="body"
        DropDownBoxCssClass="body" />
    <Texts SelectBoxCaption="[ ------- Select ------- ]" />
    <Items>
        <asp:ListItem Text="S" Value="S"></asp:ListItem>
        <asp:ListItem Text="N" Value="N"></asp:ListItem>
    </Items>
</asp:DropDownCheckBoxes>

I have binded DropDownCheckBoxes in Code behind as,

string S = reader["DropDownCheckBoxes1"].ToString();
foreach (System.Web.UI.WebControls.ListItem item in DropDownCheckBoxes1.Items)
{
   if (S == item.Value)
   {
      item.Selected = true;
   }
}

But in DropDownCheckBoxes1 the value of corresponding checkbox not selected.

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire