//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>
每当用户从英语更改为西班牙语时,我想显示一个警告框。 这是一个奇怪的行为,这个代码没有工作,它没有显示我选择的索引更改的任何警告框,但如果我粘贴警报框代码在页面加载事件,它的工作原理。页面加载是否必须对此做任何事情? 其次是可以记住答案,即如果用户选择了一个记住我的复选框,我应该记住用户是否在整个会话中选择了“是”或“否”。 对第二个问题的任何建议都会有所帮助。但请帮我找出上述代码无法正常工作的原因。
答案 0 :(得分:1)
在dropdownSelected index changed event
中尝试以下代码以显示提示框
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);
}
答案 1 :(得分:1)
使用onchange="return CheckLanguage(this);"
而不是ClientScript.RegisterStartupScript
注册javascript活动。
<强> HTML 强>
<asp:DropDownList ID="ddlLanguage" onchange="return CheckLanguage(this);" 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>
<强>的Javascript 强>
<script Type="text/javascript>
function CheckLanguage(ddl)
{
return confirm("Are you sure to change language");
}
</script>
答案 2 :(得分:1)
由于此ligne
,您的事件处理程序无法正常工作Server.Transfer(Request.Path);
尽可能将其移除或尝试工作,一切都会好的
编辑:
要解决由Server.Transfer
引起的问题,请尝试使用此功能,看看是否有感觉
在你的aspx文件中
<asp:HiddenField runat="server" ID="hide" />
中的代码
protected void Page_Load(object sender, EventArgs e)
{
if (hide.Value == "true")
{
Server.Transfer(Request.Path);
}
hide.Value = "";
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
...
sb.Append("if(confirm('")
.Append(message)
.Append("')){")
.AppendFormat("document.getElementById('{0}').value = 'true'", hide.ClientID)
.Append("}");
...
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
}
答案 3 :(得分:1)
您在有机会确认之前将用户发送到新页面。 Server.Transfer
请求一个新页面(即使它是相同的URL),所以你的js永远不会被写入页面。你需要在回发发生之前运行确认js。
我认为你也不能阻止SelectedIndexChanged
回发,因为dot net会添加它自己的js事件处理程序来完成它。我认为这里最不突兀的方法是创建一个新按钮,将语言处理程序代码移动到按钮的单击处理程序,然后使用客户端js在用户确认其选择时单击该按钮。
<asp:DropDownList ID="ddlLanguage" class="langpnl" runat="server" AutoPostBack="False">
<asp:ListItem Value="en-US">Eng</asp:ListItem>
<asp:ListItem Value="es-ES">Esp</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="myHiddenButton" runat="server" Style="display: none;" OnClick="myHiddenButton_Click" />
void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
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("if confirm('");
sb.Append(message);
sb.Append("') ");
sb.AppendFormat("document.getElementById('{0}').click();", this.myHiddenButton.ClientID);
//Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "alert", sb.ToString());
// write your js as the client-side onchange handler on the ddl
this.ddlLanguage.Attributes["onchange"] = sb.ToString();
//alert end
}
this.myHiddenButton.Click += new EventHandler(myHiddenButton_Click);
}
void myHiddenButton_Click(object sender, EventArgs e)
{
// only ever called if user confirms the js prompt
//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);
}