4

我的 asp.net 页面上有一个复选框列表。我用国家填充它。我添加了一个“SelectedIndexChanged”事件,该事件应该检查​​是否选定的国家之一是非洲,它应该使文本框可见,否则如果未选中,则为 visible=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();
        }
    }
4

2 回答 2

3

我的猜测:您CreateRegionListPage_Load没有检查IsPostBack财产的情况下打电话。这会重新加载数据库中的所有项目并防止触发此事件。

所以检查一下:

protected bvoid Page_Load(Objject sender, EventArgs e)
{
    if(!IsPostBack)
    {
        CreateRegionList();
    }
}
于 2013-11-14T09:25:02.270 回答
3

那是因为您必须将 true 设置为:

lstLocations.AutoPostBack = true;
于 2014-09-03T18:00:51.127 回答