Reputation: 871
My main purpose, populating the ddlCity DropDownList according to the ddlMedicalName selection. But my main problem is, although I try almost every solution to that problem, I couldnt make it firing ddlMedicalName_OnSelectedIndexChanged .
I know, there is plenty of questions and answers about that problem, I have tried almos every one of them but still it doesn't work. I have read that EnableStateView=True should be applied not only to DropDownList but also to the whole page, So I have added the following code to the Web.Config:
<system.web>
<pages styleSheetTheme="MainTheme" enableViewState="true"/>
And here is my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace MedicalBootStrap
{
public partial class WebForm1 : System.Web.UI.Page
{
Entity.medicaldbEntities context = new Entity.medicaldbEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlBind();
}
}
protected void ddlBind()
{
var orders = from order in context.Medicals
select new { order.MedicalID, order.medicalName, order.CityFK };
var lstsrc = orders.ToList();
ddlMedicalName.DataSource = lstsrc;
ddlMedicalName.DataTextField = "medicalname";
ddlMedicalName.DataValueField = "cityfk";
ddlMedicalName.DataBind();
}
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
int slc = Convert.ToInt32(ddlMedicalName.SelectedItem.Value);
var orders = from order in context.Cities
where order.CityID == slc
select new { order.CityID, order.CityName};
var lstsrc = orders.ToList();
ddlMedicalName.DataSource = lstsrc;
ddlMedicalName.DataTextField = "medicalname";
ddlMedicalName.DataValueField = "cityfk";
ddlMedicalName.DataBind();
}
protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void SearchButton_Click(object sender, EventArgs e)
{
}
}
}
And here is aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MedizinischeLeistungen.aspx.cs" Inherits="MedicalBootStrap.WebForm1" EnableViewState="true" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" EnableViewState="true">
<table id="SearchTable">
<tr>
<td>Medizinische Leistung</td>
<td>Region</td>
<td>Stadt</td>
</tr>
<tr id="SearchDropDown">
<td>
<asp:DropDownList ID="ddlMedicalName" runat="server" EnableViewState="true"
AutoPostBack="True" AppendDataBoundItems="true"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true" EnableViewState="true"
onselectedindexchanged="ddlRegion_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" EnableViewState="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> - Product - </asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />
</td>
</tr>
</table>
</asp:Content>
Any help would be appreciated
Upvotes: 1
Views: 15059
Reputation: 184
Kindly change as my suggestion and try.You have to remove the below line
ddlMedicalName.DataValueField = "cityfk";
or
Make a change cityfk to medicalname and try.
ddlMedicalName.DataValueField = "medicalname";
Upvotes: 0
Reputation: 62301
I tested your code. Here are what I found so far -
ddlMedicalName_OnSelectedIndexChanged event is fired correctly.
ddlMedicalName.SelectedValue is a string value; string is not a valid datasource for ddlCity.DataSource
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
// Problem is the following 3 line of codes.
ddlCity.DataSource = ddlMedicalName.SelectedValue;
ddlCity.DataTextField = ddlMedicalName.SelectedValue;
ddlCity.DataValueField = ddlMedicalName.SelectedValue;
ddlCity.DataBind();
}
Here is how I test -
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlBind();
}
}
public class Medical
{
public int MedicalID { get; set; }
public string MedicalName { get; set; }
public int CityFK { get; set; }
}
public class City
{
public int CityID { get; set; }
public string CityName { get; set; }
}
protected void ddlBind()
{
var medicals = new List<Medical>()
{
new Medical {MedicalID = 1, MedicalName = "One", CityFK = 11},
new Medical {MedicalID = 2, MedicalName = "Two", CityFK = 22},
new Medical {MedicalID = 3, MedicalName = "Three", CityFK = 33},
new Medical {MedicalID = 4, MedicalName = "Four", CityFK = 44},
};
ddlMedicalName.DataSource = medicals;
ddlMedicalName.DataTextField = "medicalname";
ddlMedicalName.DataValueField = "cityfk";
ddlMedicalName.DataBind();
}
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
var cities = new List<City>()
{
new City {CityID = 11, CityName = "City One"},
new City {CityID = 22, CityName = "City Two"},
new City {CityID = 33, CityName = "City Three"},
new City {CityID = 44, CityName = "City Four"},
};
int slc = Convert.ToInt32(ddlMedicalName.SelectedItem.Value);
var orders = from order in cities
where order.CityID == slc
select new { order.CityID, order.CityName };
var lstsrc = orders.ToList();
ddlCity.DataSource = lstsrc;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
}
protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void SearchButton_Click(object sender, EventArgs e)
{
}
Upvotes: 2
Reputation: 1593
Change:
protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
{
to:
protected void ddlMedicalName_SelectedIndexChanged(object sender, EventArgs e)
{
OR the opposite:
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" EnableViewState="true" onselectedindexchanged="ddlCity_SelectedIndexChanged">
to:
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" EnableViewState="true" onselectedindexchanged="ddlCity_OnSelectedIndexChanged">
You are currently pointing to a handle named '..._SelectedIndexChanged' and are providing '..._OnSelectedIndexChanged'.
Upvotes: -1