//master.cs
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
//alert box
string message = "Some Content of the Site are only in English. Do you want to continue?";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("return confirm('");
sb.Append(message);
sb.Append("');");
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
//alert end
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = ddlLanguage.SelectedValue;
Response.Cookies.Add(cookie);
//Set the culture and reload the page for immediate effect.
//Future effects are handled by Global.asax
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
Server.Transfer(Request.Path);
}
//master Page
<asp:DropDownList ID="ddlLanguage" class="langpnl" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged">
<asp:ListItem Value="en-US">Eng</asp:ListItem>
<asp:ListItem Value="es-ES">Esp</asp:ListItem>
</asp:DropDownList>
每当用户从英语更改为西类牙语时,我想显示一个警报框。 这是一种奇怪的行为,此代码不起作用,它没有向我显示有关所选索引更改的任何警报框,但如果我将警报框代码粘贴到页面加载事件中,它就会起作用。页面加载与此有什么关系吗? 其次,是否可以记住答案,即如果用户选择了一个表示“记住我”的复选框,我应该记住用户在整个 session 中是否选择了"is"或“否”。 关于第二个问题的任何建议都会有帮助。但请帮我找出上述代码未按预期工作的原因。
最佳答案
在您的dropdownSelected索引更改事件
中尝试以下代码以显示警报框
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
//give the exception details in a alert box
string sb = string.Format(@"<script>alert('{0}');</script>'", "Message to be shown");
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb);
}
关于c# - 下拉菜单 SelctIndexChange 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12479755/