Some points about postback and multi-language drop down list

师谦
2023-12-01
/*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 !


 类似资料:

相关阅读

相关文章

相关问答