これには 2 つの方法があります。必要に応じて、クライアント側で行うか、サーバー側で行うことができます。
サーバー側の解決策AutoPostBack
: RadioButtonListのプロパティを true に追加する必要があります。これによりdivs
、プロパティが次のようにrunat
設定されます。server
マークアップ:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="SearchByName" Value="1"></asp:ListItem>
<asp:ListItem Text="SearchByDate" Value="2"></asp:ListItem>
<asp:ListItem Text="SearchByValue" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<div id="myDiv1" runat="server" visible="false">Div Content</div>
コードビハインド:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedValue =="2")
this.myDiv1.Visible = true;
else
this.myDiv1.Visible = false;
}
クライアント側のソリューション:
Javascript :
window.onload = function () {
var inputs = document.getElementsByTagName("input");
if (inputs.length > 0) {
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "radio") {
inputs[i].onclick = function () {
if (this.value == "2") {
document.getElementById("div1").style.display = "block";
}
else {
document.getElementById("div1").style.display = "none";
}
}
}
}
}
}
マークアップ:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" >
<asp:ListItem Text="SearchByName" Value="1"></asp:ListItem>
<asp:ListItem Text="SearchByDate" Value="2"></asp:ListItem>
<asp:ListItem Text="SearchByValue" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<div id="div1" style="display:none">Div Content</div>
もちろん、これによりページ内のすべてのラジオ ボタンが取得されるため、正しいボタンを取得したことを確認する必要がある場合があります。