我有一个简单的问题。我在互联网上研究,但我找不到明确的解决方案。我在may页面中有一个下拉列表,我想显示或隐藏某些内容取决于它所选择的索引更改。我有这个代码,我确信这段代码应该有效,因为我写了很多次这样的代码:
<div class="icerik-tablo">
<div>
<h3>Telefon Bilgileri</h3>
<div class="ekle"><a href="javascript:;" onclick="ekle_goster('ekle_telefon')">Ekle</a></div>
<div class="clear"></div>
<div class="pop" id="ekle_telefon" style="display:none;">
<asp:UpdatePanel ID="upTelefon" runat="server">
<ContentTemplate>
<div>
<asp:DropDownList ID="ddlTelefonTipi" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTelefonTipi_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvTelefonTipi" runat="server" ControlToValidate="ddlTelefonTipi" ErrorMessage="*" ValidationGroup="telefonEkle"></asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlTelefonErisimTipi" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvTelefonErisimTipi" runat="server" ControlToValidate="ddlTelefonErisimTipi" ErrorMessage="*" ValidationGroup="telefonEkle"></asp:RequiredFieldValidator>
<asp:CheckBox ID="cbTelefonBirincil" runat="server" Text="Birincil Yap" />
</div>
<br /><br />
<div class="islem">
<asp:Button ID="btnTelefonEkle" runat="server" Text="Ekle" />
<a href="javascript:;" onclick="ekle_gizle('ekle_telefon')">İptal</a>
</div>
<div class="clear"></div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
这些是我的网页代码。 C#代码在这里:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TelefonTipiBagla();
TelefonErisimTipiBagla();
}
}
protected void TelefonTipiBagla()
{
ddlTelefonTipi.DataSource = Telefon.StringTelefonTipi;
ddlTelefonTipi.DataBind();
ddlTelefonTipi.Items[0].Value = "";
}
protected void TelefonErisimTipiBagla()
{
ddlTelefonErisimTipi.DataSource = IletisimBaseClass.StringIzinTipi;
ddlTelefonErisimTipi.DataBind();
ddlTelefonErisimTipi.Items[0].Value = "";
}
protected void ddlTelefonTipi_SelectedIndexChanged(object sender, EventArgs e)
{
if ((Telefon.TelefonTipi)ddlTelefonErisimTipi.SelectedIndex == Telefon.TelefonTipi.CepTelefonu || (Telefon.TelefonTipi)ddlTelefonErisimTipi.SelectedIndex == Telefon.TelefonTipi.Faks)
{
string val = ddlTelefonErisimTipi.SelectedValue;
tbTelefonDahili.Visible = false;
lblTelefonTire.Visible = false;
lblTelefonDahili.Visible = false;
}
else
{
string val = ddlTelefonErisimTipi.SelectedValue;
tbTelefonDahili.Visible = true;
lblTelefonTire.Visible = true;
lblTelefonDahili.Visible = true;
}
}
问题是dropdownlist选择索引总是为零(0)。所以,我的代码无法正常工作。请帮帮我,我确定我错过了什么。谢谢你的帮助。
答案 0 :(得分:0)
这是因为ddlTelefonTipi_SelectedIndexChanged
来自ddlTelefonErisimTipi
的所有演员都是DropDownList
,这是错误的ddlTelefonErisimTipi
。将ddlTelefonTipi
更改为{{1}}。
答案 1 :(得分:0)
ddlTelefonErisimTipi.Items.Insert(0,"");
在您的下拉列表中插入空值