checkboxlist selectedindexchanged事件c#无效

时间:2013-11-14 09:21:02

标签: asp.net c#-4.0 checkboxlist selectedindexchanged

我的asp.net页面上有一个复选框列表。我正在填写国家。我添加了一个“SelectedIndexChanged”事件,该事件应该检查​​如果所选国家之一是非洲,它应该使文本框可见,否则如果未选中则可见= false。我已将AutoPostback更改为true。但我遇到的问题是它没有进行自动回复(它根本不会进入方法)。有人可以帮我这个吗?

这就是我所做的:

     <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>

填充复选框列表:

     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();
    }

这是我的selectedIndexChanged事件:

    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方法:

     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();
        }
    }

2 个答案:

答案 0 :(得分:3)

我的猜测:您在CreateRegionList中呼叫Page_Load而未检查IsPostBack属性。这会重新加载数据库中的所有项目,并阻止触发此事件。

所以检查一下:

protected bvoid Page_Load(Objject sender, EventArgs e)
{
    if(!IsPostBack)
    {
        CreateRegionList();
    }
}

答案 1 :(得分:2)

那是因为你必须设置为:

lstLocations.AutoPostBack = true;