我需要使用C#显示和隐藏ASP.NET页面的不同区域。当用户单击第一个单选按钮时,我希望ASP.NET html显示第一段并隐藏第二段;当用户单击第二个单选按钮后,我希望我的ASP.NET html显示第二段。
这是我的html代码。
<form id="form1" runat="server">
<div>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="P1">Show First Paragraph</asp:ListItem>
<asp:ListItem Value="P2">Show Second Paragraph</asp:ListItem>
</asp:RadioButtonList>
<br />
This is my 1st Paragraph.<br />
<br />
<br />
This is my 2nd Paragraph.<br />
<br />
</div>
</form>
因此,当用户单击标记为"显示第一段"的第一个单选按钮时,我希望我的页面显示第一段并显示文本"这是我的第一段",而隐藏第二段并显示"这是第二段",反之亦然。
有人能给我看正确的C#事件代码来完成这项任务吗?
首先添加一个OnSelectedIndexChanged事件,并将其设置为AutoPostBack,这样当您选择单选按钮时,它就会发布回服务器。然后把你的段落放在面板或文字中。
ASP.NET:
<asp:RadioButtonList ID="RadioButtonList1" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true" runat="server">
<asp:ListItem Value="P1">Show First Paragraph</asp:ListItem>
<asp:ListItem Value="P2">Show Second Paragraph</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Panel ID="Panel1" Visible="false" runat="server">
This is my 1st Paragraph.<br />
</asp:Panel>
<asp:Panel ID="Panel2" Visible="false" runat="server">
This is my 2nd Paragraph.<br />
</asp:Panel>
然后在代码中,您编写一个名为:的事件
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Panel1.Visible = Panel2.Visible = false;
if (RadioButtonList1.SelectedValue == "P1")
Panel1.Visible = true;
else if (RadioButtonList1.SelectedValue == "P2")
Panel2.Visible = true;
}