我有两个下拉框,一个是州(State)的,另一个是城市(City)的。同时,为了添加额外的城市,有另一个表单会在新标签页中打开。
我希望的是,当我在新标签页中为相应的州添加额外的城市后,我想刷新州(State)下拉框,这样当我从下拉框中选择相应的州时,就可以获取到添加的附加城市。
请查看HTML代码:
<tr>
<td class="td">Location/State</td>
<td>
<asp: DropDownList CssClass="txtfld-popup" ID="ddlState" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:RequiredFieldValidator CssClass="error_msg" ID="RequiredFieldValidator1" ControlToValidate="ddlState" runat="server" ErrorMessage="Please enter State" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
</tr>
有人建议使用UpdatePanel
,但是我无法使用它。请帮忙。
城市下拉框的HTML:
<tr>
<td class="td">Location/City</td>
<td>
<asp:DropDownList CssClass="txtfld-popup" ID="ddlCity" runat="server" AutoPostBack="true"></asp:DropDownList>
<a id="aExtraCity" href="AddCity.aspx" runat="server">Add City</a>
<asp:RequiredFieldValidator CssClass="error_msg" ID="reqLocation" ControlToValidate="ddlCity" runat="server" ErrorMessage="Please enter City" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>
</td>
此外,还需查看下拉列表的后台代码:
public void LoadDropDowns()
{
string country = "India";
ddlCountry.SelectedValue = country;
ddlCountry.Enabled = false;
ddlMinExpYr.DataSource = Years;
ddlMinExpYr.DataBind();
ddlMaxExpYr.DataSource = Years;
ddlMaxExpYr.DataBind();
//populate states
var states = _helper.GetStates(country);
states.Insert(0, "--Select--");
ddlState.DataSource = states;
ddlState.DataBind();
}
添加城市代码后端:
protected void btnAddDropDown_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Add_CityforLocation";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
cmd.Parameters.Add("@CountryName", SqlDbType.VarChar).Value = "India";
cmd.Parameters.Add("@StateName", SqlDbType.VarChar).Value = ddlState.SelectedItem.ToString();
cmd.Parameters.Add("@CityName", SqlDbType.VarChar).Value = txtCity.Text.Trim();
cmd.Connection = con;
try
{
cmd.ExecuteNonQuery();
// BindContrydropdown();
}
catch (Exception ex)
{
Response.Write(ex.Message);//You Can Haave Messagebox here
}
finally
{
con.Close();
}
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ScriptKey", "alert('Your City has been Added.');window.location='Career_Job.aspx'; ", true);
}