4

I've got a checkboxlist on my asp.net page. I'm populating it with countries. I've added a "SelectedIndexChanged" event that is supposed to check that if one of the selected countries is Africa, it should make a textbox visible, otherwise visible=false if it is not selected. I have changed the AutoPostback to true. But the problem that I'm having is that it is not doing an autopostback (it's not going into the method at all). Could somebody please help me on this?

This is what I've done:

     <div id="div1" style="overflow-x:auto; width:100%; max-width:100%; height:150px; max-height:150px;" runat="server">
      <asp:CheckBoxList ID="lstLocations" CssClass="CheckBoxList" runat="server" Width="40%" Height="100%" AutoPostBack="True" OnSelectedIndexChanged="lstLocations_SelectedIndexChanged" >
      </asp:CheckBoxList>
     </div>

Populating the checkboxlist:

     private void CreateRegionList()
    {
        lstLocations.Items.Clear();

        cn = new SqlConnection(GetConnectionString());
        SqlCommand myCmd = new SqlCommand("SELECT ID, Region FROM CanonSALeads_Region ORDER BY Region", cn);
        cn.Open();
        SqlDataReader myReader = myCmd.ExecuteReader();

        lstLocations.AutoPostBack = false;
        lstLocations.CellPadding = 5;
        lstLocations.CellSpacing = 5;
        lstLocations.RepeatColumns = 1;
        lstLocations.RepeatDirection = RepeatDirection.Vertical;
        lstLocations.RepeatLayout = RepeatLayout.Flow;
        lstLocations.TextAlign = TextAlign.Right;
        lstLocations.CssClass = "CheckBoxList";

        if (myReader.HasRows)
        {
            while (myReader.Read())
            {
                CheckBox cb = new CheckBox();
                cb.ID = myReader[0].ToString();
                cb.Text = myReader[1].ToString();
                cb.AutoPostBack = false;
                cb.CssClass = "CheckBox";

                lstLocations.Items.Add(new ListItem(myReader[1].ToString(), myReader[0].ToString()));
                lstLocations.Controls.Add(new LiteralControl("<br>"));
            }
        }
        cn.Close();
        myReader.Close();
    }

And this is my selectedIndexChanged event:

    protected void lstLocations_SelectedIndexChanged(object sender, EventArgs e)
    {
        string value = null;
        try
        {
            foreach (ListItem checkBox in lstLocations.Items)
            {
                if (checkBox.Selected == true)
                {
                    value = checkBox.Text;

                    if (value == "Africa")
                    {
                        txtCountryOfAfrica.Visible = true;
                        lblAfricaCountry.Visible = true;
                    }
                }
                else
                {
                    value = checkBox.Text;

                    if (value == "Africa")
                    {
                        txtCountryOfAfrica.Visible = false;
                        lblAfricaCountry.Visible = false;
                    }
                }
            }

        }
        catch (Exception ex)
        {
            string msg = "Select Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }

    }

Page_Load method:

     protected void Page_Load(object sender, EventArgs e)
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        txtUser.Text = userName;

        if (!IsPostBack)
        {
            ContainerDocumentation.ActiveTab = tabAddCustomer;
            PopulateSector();
            CreateRegionList();
            PopulateOpportunitySource();
            CreatelstProductGroupList();
            PopulateStatus();

            PopulateTenders();
            PopulateOtherOpportunityType();
        }
    }
Kerieks
  • 1,042
  • 7
  • 25
  • 53
  • 1
    Using !Ispostback in form load check – senthilkumar2185 Nov 14 '13 at 09:29
  • 1
    @SenthulKumar, I've added in both inside and outside the (!IsPostBack), but this is not the part that is not working. Populating works perfect, the only part that is not working is when selecting on a checkbox it is supposed to go to the selectedIndexChanged event, but it's not going into that method.... – Kerieks Nov 14 '13 at 09:35
  • 1
    value = checkBox.SelectedItem.Text; – senthilkumar2185 Nov 14 '13 at 09:48
  • 1
    @SenthilKumar, Thanks for your help, appreciate it! Problem is its not even reaching the string value = null; or foreach statement... it's not going into that method at all.... – Kerieks Nov 14 '13 at 09:53

2 Answers2

3

My guess: you are calling CreateRegionList in Page_Load without checking the IsPostBack property. That reloads all items from database and prevents this event from being triggered.

So check it:

protected bvoid Page_Load(Objject sender, EventArgs e)
{
    if(!IsPostBack)
    {
        CreateRegionList();
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I have checked, and it is in the IsPostBack... I have edited my question to put the pageLoad in as well. Thank you for your fast answer.... – Kerieks Nov 14 '13 at 09:29
3

That's because you have to set true to:

lstLocations.AutoPostBack = true;