C# 如何通过radiobuttonlist循环,如果选中,则显示内容
嗨,我的代码有点问题。我有一个ASP radiobuttonlist,希望在选择某个项目时显示内容。但是,当我选择一个单选按钮,并切换回旧的,我的内容不会显示任何更多 这是我的Asp代码的完整页面,这里的javascript被注释掉了,所以它不是工作代码的一部分 在我使用ASP单选按钮之前,我的按钮确实工作得很好,但我不得不切换到ASP按钮,这是有原因的C# 如何通过radiobuttonlist循环,如果选中,则显示内容,c#,asp.net,radiobuttonlist,C#,Asp.net,Radiobuttonlist,嗨,我的代码有点问题。我有一个ASP radiobuttonlist,希望在选择某个项目时显示内容。但是,当我选择一个单选按钮,并切换回旧的,我的内容不会显示任何更多 这是我的Asp代码的完整页面,这里的javascript被注释掉了,所以它不是工作代码的一部分 在我使用ASP单选按钮之前,我的按钮确实工作得很好,但我不得不切换到ASP按钮,这是有原因的 <%@ Page Title="" Language="C#" MasterPageFile="~/Menu.Master" AutoE
<%@ Page Title="" Language="C#" MasterPageFile="~/Menu.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="DomoticaProject.Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!--
<script type="text/javascript">
$(function () {
var radios = document.getElementsById("RadioButtonList1");
var Login = document.getElementById("lbllogin");
var Signin = document.getElementById("lblSignup");
var Reset = document.getElementById("lblReset");
Login.style.display = 'block'; // show
Signin.style.display = 'none'; // hide
Reset.style.display = 'none'; // hide
for(var i = 0; i < radios.Items.Count; i++) {
radios[i].onclick = function() {
var val = this.value;
if (val == 'signin') {
Login.style.display = 'block';
Signin.style.display = 'none';
Reset.style.display = 'none';
}
else if (val == 'signup') {
Login.style.display = 'none';
Signin.style.display = 'block';
Reset.style.display = 'none';
}
else if (val == 'reset') {
Login.style.display = 'none';
Signin.style.display = 'none';
Reset.style.display = 'block';
}
}
}
});
</script>
-->
<style type="text/css">
.Loginform {
text-align: center;
position: relative;
top: 30vh;
}
.inlog {
display: block;
letter-spacing: 4px;
padding-top: 30px;
text-align: center;
}
.inlog .invoertext {
display:block;
color: white;
cursor: text;
font-size: 20px;
line-height: 20px;
text-transform: uppercase;
-moz-transform: translateY(-34px);
-ms-transform: translateY(-34px);
-webkit-transform: translateY(-34px);
transform: translateY(-34px);
transition: all 0.3s;
}
.inlog .invoertype {
background-color: transparent;
border: 0;
border-bottom: 2px solid white;
color: white;
font-size: 36px;
letter-spacing: -1px;
outline: 0;
padding: 5px 20px;
text-align: center;
transition: all 0.3s;
width: 200px;
}
.inlog .invoertype:focus {
max-width: 100%;
width: 400px;
}
.inlog .invoertype:focus + .invoertext {
color: whitesmoke;
font-size: 13px;
-moz-transform: translateY(-74px);
-ms-transform: translateY(-74px);
-webkit-transform: translateY(-74px);
transform: translateY(-74px);
}
.inlog .invoertype:valid + .invoertext {
font-size: 13px;
-moz-transform: translateY(-74px);
-ms-transform: translateY(-74px);
-webkit-transform: translateY(-74px);
transform: translateY(-74px);
}
.Navigatieinlog {
width: 450px;
height: 30px;
margin: -185px -225px;
position: absolute;
left: 50%;
top: 45vh;
display:block;
}
.alleinput[type=radio]{display:none}
.lblnavigatie {
cursor: pointer;
display: inline-block;
letter-spacing: 4px;
padding-top: 30px;
text-align: center;
}
.lblnavigatie[for="signin"] { margin-right: 20px; }
.lblnavigatie[for="reset"] { float: right; }
.lblnavigatie[type=radio]:checked + .lblnavigatie { opacity: 1; }
.btn-draw {
background-color: #333333;
border: 2px solid white;
border-radius: 27px;
color: white;
cursor: pointer;
font-size: 20px;
margin-top: 20px;
padding: 10px 20px;
text-transform: uppercase;
transition: all 200ms;
}
.btn-draw:hover, .btn-draw:focus {
background-color: white;
color: #333333;
outline: 0;
transition:0.3s;
}
.buttonplacement{
display: block;
letter-spacing: 4px;
text-align: center;
}
.Errormsg{
display: block;
letter-spacing: 4px;
padding-top: 30px;
text-align: center;
font-size:14px;
}
</style>
<link rel = "stylesheet" href = "style.css">
<!-- Inlog -->
<form runat="server">
<div>
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList55" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
<%-- <asp:Button runat="server" OnClick="Unnamed_Click1" Text="check" />--%>
</div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" id="lbllogin" visible="true">
<label id="Login" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailinlog" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtemailinlog"
ErrorMessage="Email is required"
ForeColor="Red"
ValidationGroup="Login"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
class="Errormsg"
runat="server"
ControlToValidate="txtemailinlog"
ErrorMessage="Voer geldig email adress in"
ValidationExpression="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$"
ForeColor="Red"
ValidationGroup="Login"
Display="Dynamic">
</asp:RegularExpressionValidator>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordlogin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat ="server"
ControlTovalidate ="txtpasswordlogin"
ErrorMessage ="Password is required"
ForeColor="Red"
ValidationGroup="Login"
Display="Dynamic">
</asp:RequiredFieldValidator>
<label class="buttonplacement">
<asp:Button ValidationGroup="Login" class="btn-draw" runat="server" Text="Log in" />
</label>
</label>
</asp:Label>
<!-- Signup -->
<asp:Label runat="server" id="lblSignup" visible="false">
<label id="Naam" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtNaam" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Voornaam"></asp:Label>
</label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtNaam"
ErrorMessage="Naam is required"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
class="Errormsg"
runat="server"
ControlToValidate="txtNaam"
ErrorMessage="Voer geldige voornaam in"
ValidationExpression="^[a-zA-Z]{2,}$"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RegularExpressionValidator>
</label>
<label id="Signin" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailsignin" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtemailsignin"
ErrorMessage="Email is required"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
class="Errormsg"
runat="server"
ControlToValidate="txtemailsignin"
ErrorMessage="Voer geldig email adress in"
ValidationExpression="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RegularExpressionValidator>
<asp:CustomValidator
id="Signinemailgebuik"
class="Errormsg"
runat="server"
ErrorMessage="Email is al in gebruik"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic"
></asp:CustomValidator>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtPasswordsignin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtPasswordsignin"
ErrorMessage="Password is required"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
class="Errormsg"
runat="server"
ControlToValidate="txtPasswordsignin"
ErrorMessage="Password does not meet requirements"
ValidationExpression="^.*(?=.{6,})((?=.*[!@#$%^&*()\-_=+{};:,<.>]){1})(?=.*\d)((?=.*[a-z]){1})((?=.*[A-Z]){1}).*$"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:RegularExpressionValidator>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordsignrepeat" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Repeat password">
</asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtpasswordsignrepeat"
ErrorMessage="Password is required"
ValidationGroup="Sigin"
Display="Dynamic"
ForeColor="Red">
</asp:RequiredFieldValidator>
<asp:CompareValidator
class="Errormsg"
runat="server"
ControlToValidate="txtpasswordsignrepeat"
ControlToCompare="txtPasswordsignin"
Type="String" Operator="Equal"
ErrorMessage="Password not the same"
ForeColor="Red"
ValidationGroup="Sigin"
Display="Dynamic">
</asp:CompareValidator>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Sigin" class="btn-draw" runat="server" Text="Sign in" OnClick="Unnamed16_Click" />
</label>
</label>
</asp:Label>
<!-- Reset -->
<asp:Label runat="server" id="lblReset" visible="false">
<label id="Reset" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailreset" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<asp:RequiredFieldValidator
class="Errormsg"
runat="server"
ControlTovalidate="txtemailreset"
ErrorMessage="Email is required"
ForeColor="Red"
ValidationGroup="Reset"
Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
class="Errormsg"
runat="server"
ControlToValidate="txtemailreset"
ErrorMessage="Voer geldig email adress in"
ValidationExpression="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$"
ForeColor="Red"
ValidationGroup="Reset"
Display="Dynamic">
</asp:RegularExpressionValidator>
<label class="buttonplacement">
<asp:Button ValidationGroup="Reset" class="btn-draw" runat="server" Text="Reset" />
</label>
</label>
</asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<!--
<asp:RadioButton OnCheckedChanged="SelectedButtonChange" AutoPostBack="true" CssClass="Navigatieinlog" ID="RadioButton1" GroupName="Radiobtn" Text="Sign in" runat="server" />
<asp:RadioButton OnCheckedChanged="SelectedButtonChange" AutoPostBack="true" CssClass="Navigatieinlog" ID="RadioButton2" GroupName="Radiobtn" Text="Sign up" runat="server" />
<asp:RadioButton OnCheckedChanged="SelectedButtonChange" AutoPostBack="true" CssClass="Navigatieinlog" ID="RadioButton3" GroupName="Radiobtn" Text="Reset" runat="server" />
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList1" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
-->
</ContentTemplate>
</asp:UpdatePanel>
</form>
<!-- Navigatie -->
</asp:Content>
我认为您需要做的是,在执行
foreach
循环之前,始终将每个面板设置为其默认状态。因为ViewState将记住每个面板的Visible属性
lbllogin.Visible = true;
lblSignup.Visible = true;
lblReset.Visible = true;
if (RadioButtonList1.SelectedValue == "signin")
{
}
else if (RadioButtonList1.SelectedValue == "signup")
{
}
我认为您需要做的是,在执行
foreach
循环之前,始终将每个面板设置为其默认状态。因为ViewState将记住每个面板的Visible属性
lbllogin.Visible = true;
lblSignup.Visible = true;
lblReset.Visible = true;
if (RadioButtonList1.SelectedValue == "signin")
{
}
else if (RadioButtonList1.SelectedValue == "signup")
{
}
请尝试这个例子
protected void SelectedButtonChange(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex > -1)
{
lbllogin.Visible = false;
lblSignup.Visible = false;
lblReset.Visible = false;
string selected = RadioButtonList5.SelectedItem.Value;
switch (selected)
{
case "signin":
lbllogin.Visible = true;
break;
case "signup":
lblSignup.Visible = true;
break;
case "reset":
lblReset.Visible = true;
break;
}
}
}
并用aspx代码更改您的列表
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList1" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
更新
我已将代码更改为此。。它在我这边工作,请检查一下。并调整来自您的验证等消息
<form id="form1" runat="server">
<div>
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList1" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
<%-- <asp:Button runat="server" OnClick="Unnamed_Click1" Text="check" />--%>
</div>
<asp:Label runat="server" id="lbllogin" visible="true">
<label id="Login" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailinlog" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordlogin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Login" class="btn-draw" runat="server" Text="Log in" />
</label>
</label>
</asp:Label>
<!-- Signup -->
<asp:Label runat="server" id="lblSignup" visible="false">
<label id="Naam" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtNaam" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Voornaam"></asp:Label>
</label>
</label>
<label id="Signin" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailsignin" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtPasswordsignin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordsignrepeat" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Repeat password">
</asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Sigin" class="btn-draw" runat="server" Text="Sign in" OnClick="Unnamed16_Click" />
</label>
</label>
</asp:Label>
<!-- Reset -->
<asp:Label runat="server" id="lblReset" visible="false">
<label id="Reset" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailreset" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Reset" class="btn-draw" runat="server" Text="Reset" />
</label>
</label>
</asp:Label>
</form>
请尝试此示例
protected void SelectedButtonChange(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex > -1)
{
lbllogin.Visible = false;
lblSignup.Visible = false;
lblReset.Visible = false;
string selected = RadioButtonList5.SelectedItem.Value;
switch (selected)
{
case "signin":
lbllogin.Visible = true;
break;
case "signup":
lblSignup.Visible = true;
break;
case "reset":
lblReset.Visible = true;
break;
}
}
}
并用aspx代码更改您的列表
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList1" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
更新
我已将代码更改为此。。它在我这边工作,请检查一下。并调整来自您的验证等消息
<form id="form1" runat="server">
<div>
<asp:RadioButtonList CssClass="Navigatieinlog" ID="RadioButtonList1" OnSelectedIndexChanged="SelectedButtonChange" runat="server" AutoPostBack="true">
<asp:ListItem Text="Sign in" Value="signin"></asp:ListItem>
<asp:ListItem Text="Sign up" Value="signup"></asp:ListItem>
<asp:ListItem Text="Reset" Value="reset"></asp:ListItem>
</asp:RadioButtonList>
<%-- <asp:Button runat="server" OnClick="Unnamed_Click1" Text="check" />--%>
</div>
<asp:Label runat="server" id="lbllogin" visible="true">
<label id="Login" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailinlog" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordlogin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Login" class="btn-draw" runat="server" Text="Log in" />
</label>
</label>
</asp:Label>
<!-- Signup -->
<asp:Label runat="server" id="lblSignup" visible="false">
<label id="Naam" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtNaam" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Voornaam"></asp:Label>
</label>
</label>
<label id="Signin" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailsignin" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtPasswordsignin" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Password"></asp:Label>
</label>
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtpasswordsignrepeat" runat="server" TextMode="Password"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Repeat password">
</asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Sigin" class="btn-draw" runat="server" Text="Sign in" OnClick="Unnamed16_Click" />
</label>
</label>
</asp:Label>
<!-- Reset -->
<asp:Label runat="server" id="lblReset" visible="false">
<label id="Reset" class="Loginform">
<label class="inlog">
<asp:TextBox class="invoertype" ID="txtemailreset" runat="server"></asp:TextBox>
<asp:Label class="invoertext" runat="server" Text="Email"></asp:Label>
</label>
<label class="buttonplacement">
<asp:Button ValidationGroup="Reset" class="btn-draw" runat="server" Text="Reset" />
</label>
</label>
</asp:Label>
</form>
遗憾的是,没有完成工作仍然是相同的问题更新了我的答案。没有,尽管我得到了相同的结果而不是Foreach遗憾的是,没有完成工作仍然是相同的问题更新了我的答案。没有,仍然是不起作用尽管我得到了相同的结果而不是Foreach评论不适合进一步讨论;此对话已结束。评论不用于扩展讨论;这段对话已经结束。