Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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)
    {
  // 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 17-Dec-12 9:44am
Comments
jibesh at 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 at 17-Dec-12 18:19pm
   
Do you know how to use a debugger ? Is your event firing ? ?What is going wrong EXACTLY ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try add this code in event Page_Load
 
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.isposback) 
         {
         }
    }
  Permalink  
Comments
jibesh at 18-Dec-12 1:29am
   
nice work chester!!! 5ed
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks ! The post back worked.
  Permalink  
Comments
jibesh at 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 at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100