Click here to Skip to main content
15,895,746 members

dropdown selection is not updating the textboxes correctly

Member 9680831 asked:

Open original thread
I have two dropdownlist that populate the textboxes. The first dropdown populates the second one and the second one populates the textboxes. The first dropdown has activity type and bases on that second dropdownlist shows the activity name. The activity name then populates read only textboxes. I am having trouble getting the textboxes to change when the dropdown selection is changed.

ASP.NET
<asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent" Runat="Server">
    <h2>Activity Type<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </h2>
<table>
<tr>
<td>Activity Type:</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged" 
        DataSourceID="EntityDataSource1" DataTextField="ACT_TypeName" 
        DataValueField="ACT_TypeID">
    </asp:DropDownList>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=PSEntities" DefaultContainerName="PSEntities" 
        EnableFlattening="False" EntitySetName="ACTIVITYTYPEs" 
        Select="it.[ACT_TypeID], it.[ACT_TypeName]">
    </asp:EntityDataSource>
</td>
</tr>
</table>
<br/>

<h3>Activity</h3>
<table>
<tr>
<td>Event Name</td>
<td>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
        DataSourceID="SqlDataSource1" DataTextField="INV_ActivityName" 
        DataValueField="INV_ActivityID">
        </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [INV_ActivityID], [INV_ActivityName] FROM [INVOLVEMENT] WHERE ([ACT_TypeID] = @ACT_TypeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList2" Name="ACT_TypeID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:TextBox ID="txtName" runat="server" Height="22px"></asp:TextBox>
    </td>
    <tr>
    <td>Event Date:</td>
    <td>
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>  </td>
    </tr>
    <tr>
    <td>Event Hours:</td>
    <td>
        <asp:TextBox ID="txtHours" runat="server"></asp:TextBox>  
    </td>
    </tr>
    <tr>
    <td>Description:</td>
    <td>
        <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" ></asp:TextBox>  
 </td>
 </tr>
 <tr>
 <td>
     <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" /></td>
    </tr>
 </table>
 <br />
</asp:Content>


Code behind
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PSISModel;

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

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);
        
        using (PSEntities myEntity = new PSEntities())
        {       
           var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID== selectedValue
                         select a).SingleOrDefault();

           txtDate.Text = query.INV_EventDate.ToString();
           txtHours.Text = query.INV_Hours.ToString();
           txtDescription.Text = query.INV_BriefDescript;
            
        }
    }



    protected void btnAdd_Click(object sender, EventArgs e)
    {
  // this part works
        using (PSEntities myEntity = new PSEntities())
        {
            INVOLVEMENT studInvolvement = new INVOLVEMENT();

            studInvolvement.ACT_TypeID = Convert.ToInt32(DropDownList2.SelectedValue);
            studInvolvement.INV_ActivityName = txtName.Text;
            studInvolvement.INV_BriefDescript = txtDescription.Text;
            studInvolvement.INV_Hours = Convert.ToInt32(txtHours.Text);
            studInvolvement.INV_EventDate = Convert.ToDateTime(txtDate.Text);

            var user = new STUDENT { STUD_CWID = Convert.ToInt32(Session["CWID"]) };

            myEntity.STUDENTs.Attach(user);

            studInvolvement.STUDENTs.Add(user);           
            myEntity.SaveChanges();
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);

        using (PSEntities myEntity = new PSEntities())
        {

            var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID == selectedValue
                         select a).SingleOrDefault();

            txtDate.Text = query.INV_EventDate.ToString();
            txtHours.Text =query.INV_Hours.ToString();
            txtDescription.Text = query.INV_BriefDescript;

        }


       

        if (DropDownList2.SelectedValue == "4")
        {
            DropDownList1.Visible = false;
            txtName.Visible = true;
            txtName.Text = null;
            txtName.Enabled = true;
            txtDate.Text = null;
            txtDate.Enabled = true;
            txtDescription.Text = null;
            txtDescription.Enabled = true;
            txtHours.Text = null;
            txtHours.Enabled = true;

        }
        else
        {
            DropDownList1.Visible = true;
            txtName.Visible = false ;
            txtDate.Enabled = false;
            txtHours.Enabled = false;
            txtDescription.Enabled = false;
            txtName.Enabled = false;
        }
            
        

    }


}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900