Im stuck in my project here and i really need some help!
I have 2 dropdownlists “DropDownPostal” and “DropDownCity”. When the user/employee logges into the system, he gets the list of Postals in the first dropdown list. Lets say he choose a postal and tabs over to the second dropdown “city”. I want city to be updated with the postal the user choose earlier. I have everything working with the login and retrieving data from the database to the DropDownPostal. But I cannot get the other DropDownCity updated with any data!
Here is my code for the event i made for the postaldropdown, Im trying to make a “onLeave” event.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public partial class WebForm1 : System.Web.UI.Page
{
// Entity
DanxWebsiteEntities dwe = new DanxWebsiteEntities();
protected void Page_Load(object sender, EventArgs e)
{
//Dropdown postback
//DropDownListPostal.AutoPostBack = false;
//Sessions
if (Session["UserID"] == null && Session["Name"] == null)
{
Response.Redirect("Error.aspx");
}
else
{
msg1.Text = "User id: "+Session["UserID"].ToString();
msg2.Text = "User Name: " + Session["Name"].ToString();
}
//Update dropdownpostal
if (!IsPostBack)
{
//Guid is structure type
Guid id =(Guid) Session["UserID"];
DropDownListPostal.DataSource = (from custumAdr in dwe.VW_CustumAddress
where custumAdr.UserID ==(Guid)id
select custumAdr).ToList();
DropDownListPostal.DataTextField = "Postal";
DropDownListPostal.DataValueField = "UserID";
DropDownListPostal.DataBind();
}
}
protected void DropDownListPostal_SelectedIndexChanged1(object sender, EventArgs e)
{
foreach (var data in dwe.VW_CustumAddress)
if (data.Postal == DropDownListPostal.SelectedItem.ToString())
{
DropDownListBy.Text = data.City;
}
else
{
}
}
}
}
Im not even sure if this code is close to correct.
Detailed help with code will be much appreciated.
Cheers 🙂
If I understood you right, you only need to change the value displayed in
dropDownListCityaccording to the value selected indropDownListPostal. To do so, you need to bind both dropdown lists inPage_Loadand then handle the selected index whenSelectedIndexChangedis triggered.Now, after you bound your controls to data you only need to handle
SelectedIndexChangedevent ofdropDownListPostal(don’t forget to setAutoPostback="true"in the markup ofdropDownListPostal).EDIT: Before selecting an item, clear the selection of your dropdown list to make sure only one item is selected.
That should do it. Hope it helps.