gpt4 book ai didi

asp.net - checkboxlist selectedindexchanged 事件 c# 不工作

转载 作者:行者123 更新时间:2023-12-03 21:37:23 33 4
gpt4 key购买 nike

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

最佳答案

我的猜测:你调用 CreateRegionListPage_Load不检查 IsPostBack属性(property)。这会重新加载数据库中的所有项目并防止触发此事件。

所以检查一下:

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

关于asp.net - checkboxlist selectedindexchanged 事件 c# 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19973776/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com