我有一个自定义控件,通过给自定义某个属性赋值然后需要在调用该自定义控件的页面设置下拉框的值并触发下拉框选择事件。
自定义控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Area.ascx.cs" Inherits="Web.UserControl.Area" %> <asp:UpdatePanel ID="upAddr" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:DropDownList ID="ddlProvice" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlProvice_SelectedIndexChanged"> </asp:DropDownList> <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged" Style="width: auto;"> </asp:DropDownList> <asp:DropDownList ID="ddlArea" runat="server" Style="width: auto;"> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace Web.UserControl { public partial class Area : System.Web.UI.UserControl { Model.Users user = new Model.Users(); Model.ShippingAddress sa = new Model.ShippingAddress(); private Model.Users _usermodel; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Province(); City(); Region(); } } #region 省市县(区) /// <summary> /// 省 /// </summary> private void Province() { ddlProvice.Items.Clear(); DataTable TemDt = BLL.HSSM_Public_DB.PblicQuery("*", "Province"); ddlProvice.Items.Clear(); for (int i = 0; i < TemDt.Rows.Count; i++) { ListItem li = new ListItem(); li.Text = TemDt.Rows[i]["ProvinceName"].ToString(); li.Value = TemDt.Rows[i]["Code"].ToString(); if (UserModel.ProvinceCode.HasValue && (li.Value == UserModel.ProvinceCode.Value.ToString()))//判断当前的是否等于用户的 { li.Selected = true; } ddlProvice.Items.Add(li); } } /// <summary> /// 市 /// </summary> private void City() { ddlCity.Items.Clear(); DataTable TemDt = new DataTable(); TemDt = BLL.HSSM_Public_DB.PblicQuery("*", "City", "ProvinceCode=" + ddlProvice.SelectedValue); for (int i = 0; i < TemDt.Rows.Count; i++) { ListItem li = new ListItem(); li.Text = TemDt.Rows[i]["CityName"].ToString(); li.Value = TemDt.Rows[i]["Code"].ToString(); if (UserModel.CityCode.HasValue && (li.Value.Trim() == UserModel.CityCode.Value.ToString())) { li.Selected = true; } ddlCity.Items.Add(li); } } /// <summary> /// 县/区 /// </summary> private void Region() { ddlArea.Items.Clear(); if (ddlCity.Items.Count <= 0) { return; } DataTable TemDt = new DataTable(); TemDt = BLL.HSSM_Public_DB.PblicQuery("*", "Area", "CityCode=" + ddlCity.SelectedValue); for (int i = 0; i < TemDt.Rows.Count; i++) { ListItem li = new ListItem(); li.Text = TemDt.Rows[i]["AreaName"].ToString(); li.Value = TemDt.Rows[i]["Code"].ToString(); if (UserModel.AreaCode.HasValue && (li.Value.Trim() == UserModel.AreaCode.Value.ToString())) { li.Selected = true; } ddlArea.Items.Add(li); } } /// <summary> /// 省 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlProvice_SelectedIndexChanged(object sender, EventArgs e) { City(); Region(); upAddr.Update(); } /// <summary> /// 市 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) { Region(); upAddr.Update(); } #endregion #region 属性 /// <summary> /// 会员实体 /// </summary> public Model.Users UserModel { set { _usermodel = value; } get { return _usermodel; } } /// <summary> /// 省代码 /// </summary> public int? ProvinceCode { get { return int.Parse(ddlProvice.SelectedValue); } } /// <summary> /// 市代码 /// </summary> public int? CityCode { get { return int.Parse(ddlCity.SelectedValue); } } /// <summary> /// 县/区代码 /// </summary> public int? AreaCode { get { return int.Parse(ddlArea.SelectedValue); } } /// <summary> /// 省名称 /// </summary> public string ProvinceName { set { ddlProvice.SelectedItem.Text = value; } get { return ddlProvice.SelectedItem.Text; } } /// <summary> /// 市名称 /// </summary> public string CityName { set { ddlCity.SelectedItem.Text = value; } get { return ddlCity.SelectedItem.Text; } } /// <summary> /// 县/区名称 /// </summary> public string AreaName { set { ddlArea.SelectedItem.Text = value; } get { return ddlArea.SelectedItem.Text; } } #endregion } }
调用自定义控件的页面:
<%@ Register Src="../UserControl/Area.ascx" TagName="Area" TagPrefix="uc5" %>
<asp:ScriptManager ID="smAdd" runat="server">
</asp:ScriptManager>
<li class="left">所在地址:</li> <li class="right"><uc5:Area ID="Addr" runat="server" /></li>
后台绑定的时候,点击repeter中的“编辑”按钮把当前行的数据读取到另一个表单:
/// <summary> /// 收货地址列表绑定事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void rptAddrList_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { HtmlAnchor aEdit = e.Item.FindControl("aEdit") as HtmlAnchor; aEdit.Attributes.Add("onclick", string.Format("javascript:SetAddrInfo('{0}','{1}','{2}','{3}','{4}');", 1, “5555555”,“15889898989”, “浙江省,杭州市,西湖区”,"教工路29号569")); } }
前台脚本:
//编辑时设置卡信息 function SetAddrInfo(AddrID, Consignee, Mobile,Area ,Address) { $("#divAdd").show(); $("#txtConsignee").val(Consignee); $("#txtMobile").val(Mobile); $("#txtAddress").val(Address); $("#hidID").val(AddrID); var area = Area.split(','); $("#ddlProvice").attr("text", area[0]); $("#ddlCity").attr("text", area[1]); $("#ddlArea").attr("text", area[2]); //$("#btnSetArea").click(); }
我用:
$("#ddlProvice").attr("text", area[0]); $("#ddlCity").attr("text", area[1]); $("#ddlArea").attr("text", area[2]);
不行
我希望在设置ddlProvice的值得时候可以同步触发下拉框选择事件使得
ddlCity的值改变。。。
如果我用:$("#btnSetArea").click();去后台处理,但是整个页面会刷新体验不好。。
可以加上<asp:ScriptManager/>
这个有的。。。
最终还是决定在后台处理:
去掉自定义控件的ajax标记,在使用该控件的页面加:
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional"> <ContentTemplate> 。。。。 </ContentTemplate> </asp:UpdatePanel>
后台处理的时候更新:
up.Update();