Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I use DisplayBusRouteID() assign item from the database. When I use ddlLocation_SelectedIndexChanged() method to select the item from the ddlLocation, the lblBusTicketPrice does not work. What is the problem ?

C#
protected void DisplayBusRouteID()
{
   DataClassesDataContext db = new DataClassesDataContext();

   var query = from s in db.BusRoutes
         select s;

   ddlLocation.DataSource = query;
   ddlLocation.DataTextField = "BusRouteID";
   ddlLocation.DataBind();
       
}
 protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
   lblBusTicketPrice.Text = "abc";
}
Posted
Updated 21-Dec-13 0:06am
v3
Comments
Jignesh Khant 21-Dec-13 5:20am    
Is autopostback of ddlLocation set to true? Check That.
OriginalGriff 21-Dec-13 6:23am    
"does not work" is not a very helpful error message.
What does it do that you didn't expect, or not do that you did?

Set
C#
autopostback="true"
of dropdown...:)

C#
protected void DisplayBusRouteID()
{
   DataClassesDataContext db = new DataClassesDataContext();
 
   var query = from s in db.BusRoutes
         select s;
 
   ddlLocation.DataSource = query;
   ddlLocation.DataTextField = "BusRouteID";
  ddlLocation.DataValueField = "BusRouteID";
   ddlLocation.DataBind();
       
}
 protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
   lblBusTicketPrice.Text = "abc";
}
 
Share this answer
 
make sure your dropdownlist tag has the attribute AutoPostBack="true" and page load method as below..
C#
<asp:dropdownlist id="ddlLocation" autopostback="true" runat="server" onselectedindexchanged="ddlLocation_SelectedIndexChanged" xmlns:asp="#unknown"></asp:dropdownlist>




C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               // make sure all your initialisation andloading methods are called is in this block.
               // not outside this block

           }
       }</pre>
 
Share this answer
 
Set autopostback property of ddlLocation to true

SQL
<asp:DropDownList ID="ddlLocation" runat="server" AutoPostBack="True"                onselectedindexchanged="ddlLocation_SelectedIndexChanged">
</asp:DropDownList>
 
Share this answer
 
Hi You should set Autopostback=True.
Yours Farhad.
 
Share this answer
 
check the autopostback
then


C#
DataClassesDataContext db = new DataClassesDataContext();
 
   var query = from s in db.BusRoutes
         select s;
 
   ddlLocation.DataSource = query;
   ddlLocation.DataTextField = "BusRouteID";
   ddlLocation.DataValueField = "BusRouteID";
   ddlLocation.DataBind();
 
Share this answer
 
v2
You can also use Jquery to get the value of dropdown then passes it to hidden field,
and play with that hidden field on another events.

Syntax:

$('#dd').val();
 
Share this answer
 

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