Some points about postback and multi-language drop down list

30 篇文章 0 订阅
13 篇文章 0 订阅
/*By Jiangong SUN*/


Cultures.ascx.cs:


public partial class Cultures : System.Web.UI.UserControl
    {
		public string SelectedCultureCode
		{
			get
			{
				return ddlCultures.SelectedValue;
			}
		}
	
        private void Bind()
        {
            using (var context = new Showroom_Entities())
            {
                var cultures = (from culture in context.Cultures
                                where culture.Country.CountryId == CookiesManager.IdCountry
                                select new
                                {
                                    Label = culture.Code + "/" + culture.Country.Label,
                                    Code = culture.Code
                                }).ToList();


                ddlCultures.DataTextField = "Label";
                ddlCultures.DataValueField = "Code";
                ddlCultures.DataSource = cultures;
                ddlCultures.DataBind();
                ddlCultures.Enabled = (cultures.Count > 1);
                //Get selected culture in cookie
                if (!string.IsNullOrEmpty(CookiesManager.SelectedCulture))
                    ddlCultures.SelectedValue = CookiesManager.SelectedCulture;
            }
        }
	    protected void ddlCultures_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Update selected culture in cookie only when it's different from selected value in cultures drop down list
            if (CookiesManager.SelectedCulture != ddlCultures.SelectedValue)
                CookiesManager.SelectedCulture = ddlCultures.SelectedValue;
            OnSelectionChanged(sender, e);
        }


        private void OnSelectionChanged(object sender, EventArgs e)
        {
            if (SelectionChanged != null)
                SelectionChanged(sender, e);
        }
		
        public event EventHandler SelectionChanged;
		
		protected void Cultures_Init(object sender, EventArgs e)
        {
            Bind();
        }
	}	




Inserts.aspx: 


<UserControls:Cultures ID="ucCultures" runat="server" OnSelectionChanged="ucCultures_SelectionChanged" />
<form method="post" id="myForm" action="Inserts.aspx">
<input type="submit" value="<%=SaveLabel%>" id="btnSave" οnclick="setHiddenSubmit()" />
<!-- The hidden field must be placed in form, or else its value won't be updated when you click submit button in IE -->
<asp:HiddenField ID="hiddenSubmit" runat="server" />
</form>




Javascript:


<script type="text/javascript">
    function setHiddenSubmit() {
        $("#<%=hiddenSubmit.ClientID %>").val("Submitted");
    }
</script>




Inserts.aspx.cs:


public partial class CommercialInserts : System.Web.UI.Page
    {
		protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                //Modify selected culture in cookie
                CookiesManager.SelectedCulture = ucCultures.SelectedCultureCode;
                //Save data if submit button is clicked
                if (hiddenSubmit.Value == "Submitted")
                {
                    SaveData();
                    hiddenSubmit.Value = string.Empty;
                }
            }
        }


        protected void ucCultures_SelectionChanged(object sender, EventArgs e)
        {
            LoadResources();
            LoadData();
        }
	}


Enjoy coding !


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值