错误:名称' RadioButtonList1'在当前上下文中不存在

时间:2015-05-25 07:12:18

标签: asp.net

首先请原谅我的坏英语

这是我的代码,当我想运行我的网站时,我收到了这个错误:名称' RadioButtonList1'在当前上下文中不存在

Session.Add("Age", TextBox1.Text.ToString());
Session.Add("Sex", RadioButtonList1.SelectedItem.Value.ToString());
Session.Add("Marr", RadioButtonList2.SelectedItem.Value.ToString());
Session.Add("Work", RadioButtonList3.SelectedItem.Value.ToString());

我删除他们并使用单选按钮但是再次出现此错误

名称' RadioButton1'在当前上下文中不存在

本代码

Session.Add("Age", TextBox1.Text.ToString());
if(RadioButton1.Checked)
{
    Session.Add("Sex","Male");
}
else if(RadioButton2.Checked){
    Session.Add("Sex","Female");
}
if(RadioButton3.Checked)
{
    Session.Add("Marr","No");
}
else if(RadioButton4.Checked){
    Session.Add("Marr","Yes");
}
if(RadioButton5.Checked)
{
    Session.Add("Work","Yes");
}
else if(RadioButton6.Checked){
    Session.Add("Work","No");
}
  • 不要在文本框中出错(抱歉我的英文不好:|)

3 个答案:

答案 0 :(得分:0)

我尝试了一个RadioButtonList和一个按钮并尝试了这个:

  protected void Button1_Click(object sender, EventArgs e)
    {
        Session["keySession"] = RadioButtonList1.SelectedItem.ToString();
        Response.Write((string)Session["keySession"]);
    }

现在这是我的设计来源:

<body>
    <form id="form1" runat="server">
    <div style="height: 182px">

        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    </div>
    </form>
</body>

你可以交叉检查并告诉我它是否有效吗?

Session["keySession"] = RadioButtonList1.SelectedItem.ToString();
Session.Add("keySession",RadioButtonList1.SelectedItem.ToString());

这两个陈述将做同样的工作:)

答案 1 :(得分:0)

我完成了C#Code Whit Auto Complete,这意味着无线电按钮列表已定义!!!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>DPI|پرسشنامه سنجش شخصیت وابسته</title>
    <link runat="server" rel="shortcut icon" href="~/Images/favicon.ico" type="image/x-icon"/>
    <style type="text/css">
        *{
            box-sizing: border-box;
        }
        body{
            background: #44829B;
            direction: rtl;
            margin: 20px;
        }
        form {
            background: rgba(250, 250, 250, 0.9);
            box-shadow: 0 0 13px #9C9C9C,0 0 15px #F3F3F3 inset;
            margin: 0 auto;
            width: 60%;
            border-radius: 4px;
            height:auto;
            overflow:hidden;
            padding:10px 30px 30px 30px;
        }
        table {
            width:100%;
            border-radius: 4px;
            overflow:hidden;
            font-family: 'B Lotus';
            font-size: large;
            line-height: 50px;
            vertical-align: middle;
            text-align: right;
        }
        #Button1{
            border-radius: 4px;
            border-color:#666666;
            border-style:Solid;
            border-width:3px;
            width:150px;
            font-weight:800;
            font-family:'B Lotus';
            font-size:18px; 
        }
        #Button1:hover {
            cursor:pointer;
            border-color:#44829B;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr style="text-align:center;">
                <td colspan="3" style="font-family: &quot;B Titr&quot;; font-size: x-large">پرسشنامه شخصیت وابسته</td>
            </tr>
            <tr style="text-align:center;">
                <td colspan="3">مشارکت کننده گرامی، قبل از پاسخ به سوالات پرسشنامه، لطفا به پرسش های زیر جواب دهید.</td>
            </tr>
            <tr>
                <td class="auto-style5">سن :</td>
                <td class="auto-style3" style="padding-right:10px;">
                    <asp:TextBox ID="TextBox1" runat="server" Width="42px"></asp:TextBox>
                </td>
                <td class="auto-style1">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="لطفا سن خود را وارد نمایید." ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style5">جنسیت :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Width="195px">
                        <asp:ListItem Value="Male">مرد</asp:ListItem>
                        <asp:ListItem Value="Female">زن</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="RadioButtonList1" ErrorMessage="لطفا جنسیت خودرا انتخاب نمایید." ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style5">وضعیت تاهل :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal" Width="205px">
                        <asp:ListItem Value="Mojarad">مجرد</asp:ListItem>
                        <asp:ListItem Value="Moteahel">متاهل</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="RadioButtonList2" ErrorMessage="لطفا وضعیت تاهل خود را مشخص نمایید." ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style5">شاغل :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" Width="218px">
                        <asp:ListItem Value="Yes">بله</asp:ListItem>
                        <asp:ListItem Value="No">خیر</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RadioButtonList3" ErrorMessage="لطفا وضعیت شغل خود را انتخاب کنید." ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style4" colspan="3">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="100px" />
                    <br />
                </td>
            </tr>
            <br />
            <tr>
                <td class="auto-style2" colspan="3" style="text-align:center;">
                    <asp:Button ID="Button1" runat="server" Text="شروع ازمون" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

答案 2 :(得分:0)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr style="text-align:center;">
                <td colspan="3" style="font-family: &quot;B Titr&quot;; font-size: x-large">پرسشنامه شخصیت وابسته</td>
            </tr>
            <tr style="text-align:center;">
                <td colspan="3">مشارکت کننده گرامی، قبل از پاسخ به سوالات پرسشنامه، لطفا به پرسش های زیر جواب دهید.</td>
            </tr>
            <tr>
                <td class="auto-style5">سن :</td>
                <td class="auto-style3" style="padding-right:10px;">
                    <asp:TextBox ID="TextBox1" runat="server" Width="42px"></asp:TextBox>
                </td>
                <td class="auto-style1">

                </td>
            </tr>
            <tr>
                <td class="auto-style5">جنسیت :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Width="195px">
                        <asp:ListItem Value="Male">مرد</asp:ListItem>
                        <asp:ListItem Value="Female">زن</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td class="auto-style5">وضعیت تاهل :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal" Width="205px">
                        <asp:ListItem Value="Mojarad">مجرد</asp:ListItem>
                        <asp:ListItem Value="Moteahel">متاهل</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td class="auto-style5">شاغل :</td>
                <td class="auto-style3">
                    <asp:RadioButtonList ID="RadioButtonList3" runat="server" RepeatDirection="Horizontal" Width="218px">
                        <asp:ListItem Value="Yes">بله</asp:ListItem>
                        <asp:ListItem Value="No">خیر</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>

                </td>
            </tr>
            <tr>
                <td class="auto-style4" colspan="3">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                    <br />
                </td>
            </tr>
            <br />
            <tr>
                <td class="auto-style2" colspan="3" style="text-align:center;">
                    <asp:Button ID="Button1" runat="server" Text="شروع ازمون" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

和C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["keyText"] = TextBox1.Text;
        Session["keyGender"] = RadioButtonList1.SelectedItem.ToString();
        Session["keyTwo"] = RadioButtonList2.SelectedItem.ToString();
        Session["keyThree"] = RadioButtonList3.SelectedItem.ToString();
    }
}