如何循环通过单选按钮列表以及是否显示内容

时间:2019-03-07 07:35:46

标签: c# asp.net radiobuttonlist

嗨,我的代码遇到了麻烦。我有一个ASP单选按钮列表,想在选择一个项目时显示内容。但是,当我选择一个单选按钮并切换回旧的按钮时,我的内容不再显示。

这是我的Asp代码整页,此处的javascript被注释掉,因此它不是工作代码的一部分。

在我拥有ASP单选按钮之前,我的按钮确实可以令人满意地工作,但是出于某种原因,我不得不切换到ASP按钮。

<%@ 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>

这是我在C#中的后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Security;

namespace DomoticaProject
{
    public partial class Login : System.Web.UI.Page
    {



        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Unnamed16_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = CS;

            try
            {
                SqlCommand cmd = new SqlCommand("RegisterUser", con);
                cmd.CommandType = CommandType.StoredProcedure;

                string EncryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPasswordsignin.Text, "SHA1");

                SqlParameter Naam = new SqlParameter("@Naam", txtNaam.Text);

                SqlParameter Email = new SqlParameter("@Email", txtemailsignin.Text);
                SqlParameter Wachtwoord = new SqlParameter("@Wachtwoord", EncryptedPassword);

                cmd.Parameters.Add(Naam);
                cmd.Parameters.Add(Email);
                cmd.Parameters.Add(Wachtwoord);


                con.Open();
                int ReturnCode = (int)cmd.ExecuteScalar();
                if(ReturnCode == -1)
                    {
                        Signinemailgebuik.IsValid = false;                        
                    }
            }
            catch
            {

            }
            finally
            {
                con.Close();
            }
            }
        }

        protected void SelectedButtonChange(object sender, EventArgs e)
        {
            if (RadioButtonList55.SelectedIndex > -1)
            {
                lbllogin.Visible = false;
                lblSignup.Visible = false;
                lblReset.Visible = false;
                string selected = RadioButtonList55.SelectedItem.Text;
                switch (selected)
                {
                    case "Sign in":
                        lbllogin.Visible = true;
                        break;
                    case "Sign up":
                        lblSignup.Visible = true;
                        break;
                    case "Reset":
                        lblReset.Visible = true;
                        break;

                }

            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为您需要做的是,在执行foreach循环之前,始终将每个面板设置为其默认状态。因为ViewState会记住每个面板的Visible属性。

lbllogin.Visible = true;
lblSignup.Visible = true;
lblReset.Visible = true;

if (RadioButtonList1.SelectedValue == "signin")
{

}
else if (RadioButtonList1.SelectedValue == "signup")
{

}

答案 1 :(得分:0)

请尝试使用此示例

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>