當我在下拉列表中選擇一個值時,我會得到一個回傳,並且即使在回發之後,我選擇的值也會被選中。我如何獲得默認值,< - 在回發之後再次選擇City - >作爲選定值?Dropdownlist回發到默認選定值?
HTML:
<asp:DropDownList ID="ddlCity" Width="200px" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"> </asp:DropDownList>
標記:
protected void Page_Load(object sender, EventArgs e)
{
DataTable CityMembersTable = GetCity();
ddlCity.DataSource = CityMembersTable;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("<--Choose City-->", ""));
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlCity.SelectedValue == "")
{
return;
}
ddlCity.SelectedValue == "0"; //Dose not work...
}
現在爲什麼你需要嗎?看看代碼它似乎不太可能它會被加載綁定,你不會得到您選擇的值 – V4Vendetta 2012-01-13 10:03:35
我懷疑'ddlCity_SelectedIndexChanged'永遠不會調用,因爲您正在page_load中_every_回發中重新綁定DropDownList。把它包裝在一個'if(!Page.IsPostBack){...}' – 2012-01-13 10:03:59