我需要使用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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句