Aan
Aan

Reputation: 12910

DropDownList1 does not exist in the current context

I have added DropDownList1 in Default.aspx page my ASP.Net project, and I have the below code in Default.cs:

if(DropDownList1.SelectedItem.ToString()=="")

I got this error:

Error 2 The name 'DropDownList1' does not exist in the current context

Edit: In *.cs:

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {

           string username = Login1.UserName;
            string pwd = Login1.Password;


            var connstring = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; ;
            var conn = new SqlConnection(connstring);
            conn.Open();
            SqlCommand command;
         if(DropDownList1.SelectedItem.ToString()=="")
             command = new SqlCommand("Select [ID] from [Inspector] WHERE [ID] =" + username + " AND [Password] ='" + pwd + "';", conn);

            SqlDataReader dr = command.ExecuteReader();
            if (dr.Read())
            {
                if (dr[0].ToString() == username)
                {
                    Session["UserAuthentication"] = username;
                    Session.Timeout = 1;
                    Response.Redirect("MainInspector.aspx");
                }
                else
                {
                    Session["UserAuthentication"] = "";
                }
            }
}

In *.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent" >
    <style type="text/css">
        .style1
        {
            height: 26px;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
    <h2>
        الصفحة الرئيسية</h2>
    <p>
        الرجاء تسجيل الدخول:</p>
    <p>
        &nbsp;<asp:Login ID="Login1" runat="server" 
            FailureText="لم يتم تسجيل دخولك، الرجاء المحاولة ثانية." 
            LoginButtonText="تسجيل الدخول" onauthenticate="Login1_Authenticate" 
            PasswordLabelText="كلمة المرور:" RememberMeText="تذكرني في المرات القادمة" 
            TitleText="نسترعي إنتباهك أن كلمة المرور حساسة لحالة الأحرف" 
            UserNameLabelText="رقم الهوية:">
            <LayoutTemplate>
                <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                    <tr>
                        <td>
                            <table cellpadding="0">
                                <tr>
                                    <td align="center" colspan="2">
                                        نسترعي إنتباهك أن كلمة المرور حساسة لحالة الأحرف</td>
                                </tr>
                                <tr dir="rtl">
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">رقم الهوية:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                            ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                            ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" class="style1">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">كلمة المرور:</asp:Label>
                                    </td>
                                    <td class="style1">
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                            ControlToValidate="Password" ErrorMessage="Password is required." 
                                            ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">

                                        تسجيل الدخول بوصفك:<br />

                                        <asp:CheckBox ID="RememberMe" runat="server" Text="تذكرني في المرات القادمة" />
                                        <br />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color:Red;">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                        <br />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" 
                                            Text="تسجيل الدخول" ValidationGroup="Login1" />
                                        <asp:DropDownList ID="DropDownList1" runat="server">
                                            <asp:ListItem>فاحص</asp:ListItem>
                                            <asp:ListItem>مُدرب</asp:ListItem>
                                            <asp:ListItem>مُتدرب</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>
    </p>
</asp:Content>

Upvotes: 1

Views: 7200

Answers (3)

endjinn
endjinn

Reputation: 1

DropDownList1 does not exist in the current context

The solution for me was to place the DropDownList outside of of the LoginView.

Try moving the DropDownList to another section of your page and see if this works.

Upvotes: 0

hutchonoid
hutchonoid

Reputation: 33306

You need to check your markup to confirm that the ID matches i.e.

<asp:DropDownList id="DropDownList1".....

If it is a web application the designer.cs file could be messed up as this has the reference within it.

Something like this:

    protected global::System.Web.UI.WebControls.DropDownList DropDownList1;

If it is missing try adding the above.

Upvotes: 2

Tasos K.
Tasos K.

Reputation: 8087

Such errors might appear when the markup in the .aspx page (or .ascx control) is not well formed. In cases where the markup is bad, when you add a new control its declaration is not added in the designer file and leads to this error.

If this is the case, one common trick to fix this is to cut and paste the markup of the control. This way you re-add the control and make Visual Studio to re-add the declaration of the control in the designer file.

Upvotes: 0

Related Questions