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: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
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)
{
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;
}
}
}