I have a problem and I am not sure why this is happening. I have a Detailsview that users will be filling out. I have several dropdownlists and all of them work as they should accept the one with the ID of typeOfEmployee. When that is selected the value does not stay selected and it clears all the information in the textboxes/dropdownlists before it. The purpose of the dropdownlist is to show other controls depending on what is selected. The new controls that are supposed to show up do show up but everything else gets cleared out. Here’s my code for the detailsview:

<asp:DetailsView ID="NewEmployeeDetailsView1" runat="server" Height="110px" Width="731px"
        AutoGenerateRows="False" DataKeyNames="rAuid" DataSourceID="NewEmployeeDS" DefaultMode="Insert"
        OnItemInserting="NewEmployeeDetailsView1_ItemInserting" 
        ForeColor="#333333" GridLines="None"
        Style="margin-top: 0px">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <Fields>
            <asp:TemplateField HeaderText="Employee First Name" SortExpression="Employee First Name">
                <InsertItemTemplate>
                    <asp:TextBox ID="firstNameTxt" runat="server" Text='<%# Bind("employeeFirstName") %>'></asp:TextBox><asp:RequiredFieldValidator
                        ID="firstNameval" runat="server" ErrorMessage="Employee First Name is a required field."
                        ControlToValidate="firstNameTxt">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Middle Name" SortExpression="Employee Middle Name">
                <InsertItemTemplate>
                    <asp:TextBox ID="middleNameTxt" runat="server" Text='<%# Bind("employeeMiddleName") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Last Name" SortExpression="Employee Last Name">
                <InsertItemTemplate>
                    <asp:TextBox ID="lastNameTxt" runat="server" Text='<%# Bind("employeeLastName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="lastNameval" runat="server" ErrorMessage="Employee Last Name is a required field."
                        ControlToValidate="lastNameTxt">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Credentials" SortExpression="Credentials">
                <InsertItemTemplate>
                    <asp:TextBox ID="credentialsTxt" runat="server" Text='<%# Bind("credentials") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Start Date" SortExpression="Employee Start Date">
                <InsertItemTemplate>
                    <asp:TextBox ID="startDateTxt" runat="server" AutoPostBack="false" Text='<%# Bind("startDate") %>'></asp:TextBox>
                    <asp:ImageButton ID="calendarImageBtn" runat="server" ImageUrl="~/Images/Calendar_schedule.png"
                        OnClick="calendarImageBtn_Click" CausesValidation="False" />
                    <asp:Calendar ID="startDateCalendar" runat="server" Visible="false" SelectedDate=""
                        SelectionMode="Day" OnSelectionChanged="startDateCalendar_SelectionChanged" BackColor="#99CCFF"
                        TitleStyle-BackColor="White"></asp:Calendar>
                    <asp:RequiredFieldValidator ID="startDateVal" runat="server" ErrorMessage="Start Date is a required field."
                        ControlToValidate="startDateTxt">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Facility">
                <InsertItemTemplate>
                    <asp:DropDownList runat="server" AutoPostBack="True" ID="facility" EnableViewState="True"
                        Text='<%# Bind("facility") %>'>
                        <asp:ListItem Value="-1">Facility</asp:ListItem>
                        <asp:ListItem Text="Kish Hospitial" Value="1">Kish Hospitial</asp:ListItem>
                        <asp:ListItem Text="KishHealth System" Value="2">KishHealth System</asp:ListItem>
                        <asp:ListItem Text="DCH - Hospise" Value="3">DCH - Hospise</asp:ListItem>
                        <asp:ListItem Text="Valley West" Value="4">Valley West</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Type Of User">
                <InsertItemTemplate>
                    <asp:DropDownList runat="server"  ID="typeOfEmployee" OnSelectedIndexChanged="typeOfEmployee_SelectedIndexChanged"
                        EnableViewState="True" AutoPostBack="True">
                        <asp:ListItem Value="-1">Type Of User</asp:ListItem>
                        <asp:ListItem Text="Employee" Value="1">Employee</asp:ListItem>
                        <asp:ListItem Text="Student Nurse" Value="2">Student Nurse</asp:ListItem>
                        <asp:ListItem Text="Volunteer" Value="3">Volunteer</asp:ListItem>
                        <asp:ListItem Text="Physician" Value="4">Physician</asp:ListItem>
                        <asp:ListItem Text="Physician Office Staff" Value="5">Physician Office Staff</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Department Code" SortExpression="Department Code">
                <InsertItemTemplate>
                    <asp:DropDownList ID="departmentCodeDDL" runat="server" DataValueField="deptCode"
                        AppendDataBoundItems="true" AutoPostBack="True" DataSourceID="DeptSqlDS" DataTextField="deptCode"
                        OnSelectedIndexChanged="departmentCodeDDL_SelectedIndexChanged" EnableViewState="True">
                        <asp:ListItem Value="-1">Select Dept Code</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Department Name" SortExpression="Department Name">
                <InsertItemTemplate>
                    <asp:Label runat="server" ID="depatmentNameLabel"></asp:Label>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Job Code" SortExpression="Job Code">
                <InsertItemTemplate>
                    <asp:DropDownList ID="jobCodeDDL" runat="server" AutoPostBack="True" DataTextField="jobTitle"
                        AppendDataBoundItems="true" DataValueField="jobCode" DataSourceID="jobSqlDS"
                        EnableViewState="True">
                        <asp:ListItem Value="-1">Select Job Title</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="manager" HeaderText="Manager" ShowHeader="False" SortExpression="manager"
                InsertVisible="False" />
            <asp:BoundField DataField="managerEmail" HeaderText="Manager Email" ShowHeader="False"
                SortExpression="managerEmail" InsertVisible="False" />
            <asp:TemplateField HeaderText="Standard Student" SortExpression="standardStudent">
                <InsertItemTemplate>
                    <asp:CheckBox ID="standardStudentCheckBox" runat="server" Checked='<%# Bind("standardStudent") %>' />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Fundamental Student" SortExpression="fundamentalStudent">
                <InsertItemTemplate>
                    <asp:CheckBox ID="fundamentalStudentCheckBox" runat="server" Checked='<%# Bind("fundamentalStudent") %>' />
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Student Expiration Date:" SortExpression="expDate">
                <InsertItemTemplate>
                    <asp:TextBox ID="expDateTxt" runat="server" AutoPostBack="false" Text='<%# Bind("expDate") %>'></asp:TextBox>
                    <asp:ImageButton ID="expDatecalendarImageBtn" runat="server" ImageUrl="~/Images/Calendar_schedule.png"
                        OnClick="expDatecalendarImageBtn_Click" CausesValidation="False" />
                    <asp:Calendar ID="expDateCalendar" runat="server" Visible="false" SelectedDate=""
                        SelectionMode="Day" OnSelectionChanged="expDateCalendar_SelectionChanged" BackColor="#99CCFF"
                        TitleStyle-BackColor="White"></asp:Calendar>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Meditech Provider:" SortExpression="meditechProvider">
            <InsertItemTemplate>
            <asp:TextBox ID="meditechProvider" runat="server" AutoPostBack="false" Text='<%# Bind("meditechProvider") %>'></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Meditech User Name:" SortExpression="meditechUserName">
            <InsertItemTemplate>
            <asp:TextBox ID="meditechUserName" runat="server" AutoPostBack="false" ></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Meditech One Time Password:" SortExpression="meditechUserPassword">
            <InsertItemTemplate>
            <asp:TextBox ID="meditechUserPassword" runat="server" AutoPostBack="false" ></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Physician Group:" SortExpression="physicianGroup">
            <InsertItemTemplate>
            <asp:TextBox ID="physicianGroup" runat="server" AutoPostBack="false" Text='<%# Bind("physicianGroup") %>'></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <InsertItemTemplate>
                    <asp:Button ID="submitBtn" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="Submit & Select Applications --->" />
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
        </Fields>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    </asp:DetailsView>
    <asp:SqlDataSource ID="DeptSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:UserAccessConnectionString %>"
        SelectCommand="SELECT DISTINCT [deptCode] FROM [DeptJobCode]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="jobSqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:UserAccessConnectionString %>"
        SelectCommand="SELECT DISTINCT [jobCode], [jobTitle] FROM [DeptJobCode] where [deptCode] = @deptcode">
        <SelectParameters>
            <asp:ControlParameter ControlID="NewEmployeeDetailsView1$departmentCodeDDL" Name="deptcode"
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="NewEmployeeDS" runat="server" ConnectionString="<%$ ConnectionStrings:UserAccessConnectionString %>"
        SelectCommand="SELECT * FROM [Requests]" DeleteCommand="DELETE FROM [Requests] WHERE [rAuid] = @rAuid"
        InsertCommand="INSERT INTO [Requests] ([employeeLastName], [employeeFirstName], [employeeMiddleName], [credentials], [facility], [typeOfEmployee], [departmentCode], [departmentName], [jobCode], [jobTitle], [startDate], [manager], [managerEmail], [standardStudent], [fundamentalStudent], [expDate], [meditechProvider], [physicianGroup], [emailSentToManager]) VALUES (@employeeLastName, @employeeFirstName, @employeeMiddleName, @credentials, @facility, @typeOfEmployee, @departmentCode, @departmentName, @jobCode, @jobTitle, @startDate, @manager, @managerEmail, @standardStudent, @fundamentalStudent, @expDate, @meditechProvider, @physicianGroup, @emailSentToManager); SELECT @NewID = @@Identity"
        UpdateCommand="UPDATE [Requests] SET [employeeLastName] = @employeeLastName, [employeeFirstName] = @employeeFirstName, [employeeMiddleName] = @employeeMiddleName, [credentials] = @credentials, [facility] = @facility, [typeOfEmployee] = @typeOfEmployee, [departmentCode] = @departmentCode, [departmentName] = @departmentName, [jobCode] = @jobCode, [jobTitle] = @jobTitle, [startDate] = @startDate, [manager] = @manager, [managerEmail] = @managerEmail, [standardStudent] = @standardStudent, [fundamentalStudent] = @fundamentalStudent, [expDate] = @expDate, [meditechProvider] = @meditechProvider, [physicianGroup] = @physicianGroup, [emailSentToManager] = @emailSentToManager WHERE [rAuid] = @rAuid"
        OnInserted="NewEmployeeDS_Inserted">
        <DeleteParameters>
            <asp:Parameter Name="rAuid" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="employeeLastName" Type="String" />
            <asp:Parameter Name="employeeFirstName" Type="String" />
            <asp:Parameter Name="employeeMiddleName" Type="String" />
            <asp:Parameter Name="credentials" Type="String" />
            <asp:Parameter Name="facility" Type="String" />
            <asp:Parameter Name="typeOfEmployee" Type="String" />
            <asp:Parameter Name="departmentCode" Type="String" />
            <asp:Parameter Name="departmentName" Type="String" />
            <asp:Parameter Name="jobCode" Type="String" />
            <asp:Parameter Name="jobTitle" Type="String" />
            <asp:Parameter Name="startDate" Type="DateTime" />
            <asp:Parameter Name="manager" Type="String" />
            <asp:Parameter Name="managerEmail" Type="String" />
            <asp:Parameter Name="standardStudent" Type="Boolean" />
            <asp:Parameter Name="fundamentalStudent" Type="Boolean" />
            <asp:Parameter Name="expDate" Type="DateTime" />
            <asp:Parameter Name="meditechProvider" Type="String" />
            <asp:Parameter Name="physicianGroup" Type="String" />
            <asp:Parameter Name="emailSentToManager" Type="Boolean" />
            <asp:Parameter Direction="Output" Name="NewId" Type="Int16" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="employeeLastName" Type="String" />
            <asp:Parameter Name="employeeFirstName" Type="String" />
            <asp:Parameter Name="employeeMiddleName" Type="String" />
            <asp:Parameter Name="credentials" Type="String" />
            <asp:Parameter Name="facility" Type="String" />
            <asp:Parameter Name="typeOfEmployee" Type="String" />
            <asp:Parameter Name="departmentCode" Type="String" />
            <asp:Parameter Name="departmentName" Type="String" />
            <asp:Parameter Name="jobCode" Type="String" />
            <asp:Parameter Name="jobTitle" Type="String" />
            <asp:Parameter Name="startDate" Type="DateTime" />
            <asp:Parameter Name="manager" Type="String" />
            <asp:Parameter Name="managerEmail" Type="String" />
            <asp:Parameter Name="standardStudent" Type="Boolean" />
            <asp:Parameter Name="fundamentalStudent" Type="Boolean" />
            <asp:Parameter Name="expDate" Type="DateTime" />
            <asp:Parameter Name="meditechProvider" Type="String" />
            <asp:Parameter Name="physicianGroup" Type="String" />
            <asp:Parameter Name="emailSentToManager" Type="Boolean" />
            <asp:Parameter Name="rAuid" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

Here's some of the C# code on the page:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*DropDownList typeEmployee = (DropDownList)NewEmployeeDetailsView1.FindControl("typeOFEmployee");

                typeEmployee.Items.Add(new ListItem("Type Of Employee", ""));
                typeEmployee.Items.Add(new ListItem("Employee", "Employee"));
                typeEmployee.Items.Add(new ListItem("Nursing Student", "Nursing Student"));
                typeEmployee.Items.Add(new ListItem("Volunteer", "Volunteer"));
                typeEmployee.Items.Add(new ListItem("Physician", "Physician"));
                typeEmployee.Items.Add(new ListItem("Physician Staff Office", "Physician Staff Office"));

                }*/
                NewEmployeeDetailsView1.Fields[7].Visible = false;
                NewEmployeeDetailsView1.Fields[8].Visible = false;
                NewEmployeeDetailsView1.Fields[9].Visible = false;
                NewEmployeeDetailsView1.Fields[12].Visible = false;
                NewEmployeeDetailsView1.Fields[13].Visible = false;
                NewEmployeeDetailsView1.Fields[14].Visible = false;
                NewEmployeeDetailsView1.Fields[15].Visible = false;
                NewEmployeeDetailsView1.Fields[16].Visible = false;
                NewEmployeeDetailsView1.Fields[17].Visible = false;
                NewEmployeeDetailsView1.Fields[18].Visible = false;
            }
        }
protected void typeOfEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropDownList typeOfEmployee = (DropDownList)NewEmployeeDetailsView1.FindControl("typeOfEmployee");
            
                
                if (typeOfEmployee.SelectedValue == "1")
                {
                    NewEmployeeDetailsView1.Fields[7].Visible = true;
                    NewEmployeeDetailsView1.Fields[8].Visible = true;
                    NewEmployeeDetailsView1.Fields[9].Visible = true;
                    NewEmployeeDetailsView1.Fields[12].Visible = false;
                    NewEmployeeDetailsView1.Fields[13].Visible = false;
                    NewEmployeeDetailsView1.Fields[14].Visible = false;
                    NewEmployeeDetailsView1.Fields[15].Visible = false;
                    NewEmployeeDetailsView1.Fields[16].Visible = false;
                    NewEmployeeDetailsView1.Fields[17].Visible = false;
                    NewEmployeeDetailsView1.Fields[18].Visible = false;
                }
                if (typeOfEmployee.SelectedValue == "2")
                {
                    NewEmployeeDetailsView1.Fields[12].Visible = true;
                    NewEmployeeDetailsView1.Fields[13].Visible = true;
                    NewEmployeeDetailsView1.Fields[14].Visible = true;
                    NewEmployeeDetailsView1.Fields[7].Visible = false;
                    NewEmployeeDetailsView1.Fields[8].Visible = false;
                    NewEmployeeDetailsView1.Fields[9].Visible = false;
                    NewEmployeeDetailsView1.Fields[15].Visible = false;
                    NewEmployeeDetailsView1.Fields[16].Visible = false;
                    NewEmployeeDetailsView1.Fields[17].Visible = false;
                    NewEmployeeDetailsView1.Fields[18].Visible = false;
                }
                if (typeOfEmployee.SelectedValue == "4")
                {
                    NewEmployeeDetailsView1.Fields[15].Visible = true;
                    NewEmployeeDetailsView1.Fields[16].Visible = true;
                    NewEmployeeDetailsView1.Fields[17].Visible = true;
                    NewEmployeeDetailsView1.Fields[18].Visible = true;
                    NewEmployeeDetailsView1.Fields[7].Visible = false;
                    NewEmployeeDetailsView1.Fields[8].Visible = false;
                    NewEmployeeDetailsView1.Fields[9].Visible = false;
                    NewEmployeeDetailsView1.Fields[12].Visible = false;
                    NewEmployeeDetailsView1.Fields[13].Visible = false;
                    NewEmployeeDetailsView1.Fields[14].Visible = false;
                }
                if (typeOfEmployee.SelectedValue == "5")
                {
                    NewEmployeeDetailsView1.Fields[18].Visible = true;
                    NewEmployeeDetailsView1.Fields[7].Visible = false;
                    NewEmployeeDetailsView1.Fields[8].Visible = false;
                    NewEmployeeDetailsView1.Fields[9].Visible = false;
                    NewEmployeeDetailsView1.Fields[12].Visible = false;
                    NewEmployeeDetailsView1.Fields[13].Visible = false;
                    NewEmployeeDetailsView1.Fields[14].Visible = false;
                    NewEmployeeDetailsView1.Fields[15].Visible = false;
                    NewEmployeeDetailsView1.Fields[16].Visible = false;
                    NewEmployeeDetailsView1.Fields[17].Visible = false;
                }
            }

I have tried change the Autopostback to false and putting the code for the selectedindexchange event in an if (!IsPostBack) block. But that did not work. I do not know why it is functioning this way and not the way it is supposed to. I looked all over the web and none of the solutions people gave helped. :(

Have you enabled the viewstate true for the dropdown

Yes

Sir you write the whole code of your project.
Just use command name and command Argument inside Detail View and bind Command Argument with Id.
This will open the selected Record use select.

if (e.CommandName == "change")
    {
        Response.Redirect("editworker.aspx?sid=" + e.CommandArgument);
    }

here command name is Functioning and with query string going to next page with query string. And Command Argument is bind with ID so it will show only selected record.
use to receive whole query string with this code

private void Data_Senior()
{
    SqlDataAdapter adp = new SqlDataAdapter("select * from tbsenior where id=@id", con);
    adp.SelectCommand.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = Convert.ToString(Request.QueryString["sid"]);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    DetailsView1.DataSource = ds;
    DetailsView1.DataBind();
}

"Necessity is the mother of Invention "

:icon_idea:Sir you write the whole code of your project here. Actually no need for this
Just use command name and command Argument inside Detail View and bind Command Argument with Id.
This will open the selected Record use select.

if (e.CommandName == "change")
        {
            Response.Redirect("editworker.aspx?sid=" + e.CommandArgument);
        }

here command name is Functioning and with query string going to next page with query string. And Command Argument is bind with ID so it will show only selected record.
use to receive whole query string with this code

private void Data_Senior()
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from tbsenior where id=@id", con);
        adp.SelectCommand.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = Convert.ToString(Request.QueryString["sid"]);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }

"Necessity is the mother of Invention "

I do not think you are understanding what I am trying to do. I do not need a query string to go to the next page. I need my dropdown value to stay selected and not have the other boxes that are filled in with information become blank after the selection. I also need other controls to show up in the detailsview depending on what is selected from the dropdown. This has nothing to do with a query string. But thank you.

SqlCommand cmd = new SqlCommand("select workername,uniqueid from tbexecutive", con);
SqlDataReader ad = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(ad);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "workername";
DropDownList3.DataValueField = "uniqueid";
DropDownList3.DataBind();
ad.Close();
cmd.Dispose();

I don't have a problem with the SQL I have a problem with the dropdownlist not staying selected and it removes all of the information in the other information int the controls that have been filled out. I am not sure what you are trying to tell me but this has nothing to do with a SQL connection. Anyone please help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.