如何循环显示单选按钮列表,如果选择显示内容

问题描述 投票:1回答:2

嗨,我的代码有问题。我有一个ASP radiobuttonlist,并希望在选择项目时显示内容。但是,当我选择一个单选按钮,然后切换回旧按钮时,我的内容就不会再出现了。

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

在我使用ASP radiobuttons之前,我有按钮确实很高兴,但我不得不切换到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;

                }

            }
        }
    }
}
c# asp.net radiobuttonlist
2个回答
0
投票

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

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

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

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

}

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>
© www.soinside.com 2019 - 2024. All rights reserved.