Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
        }
            
        

    }


}
Posted
Comments
Jibesh 17-Dec-12 17:52pm    
Please add the real problem what you are facing. like query not working, or selection change not working.

textBox fields update may not happen because of so many reasons you need to spend time on debugging to find what really causing trouble here. If the selection change event are working fine means the reason could be the query execution.

Check and comback
Christian Graus 17-Dec-12 18:19pm    
Do you know how to use a debugger ? Is your event firing ? ?What is going wrong EXACTLY ?

try add this code in event Page_Load

C#
protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.isposback)
     {
     }
}
 
Share this answer
 
Comments
Jibesh 18-Dec-12 1:29am    
nice work chester!!! 5ed
Thanks ! The post back worked.
 
Share this answer
 
Comments
Jibesh 18-Dec-12 1:35am    
you may mark the solution as resolved. also in future please use the Reply/Have a Question or Comment link to post comment. Solution field is suppose to provide solution. Thanks
chester_it21 18-Dec-12 2:09am    
here i am ,wellcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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