0
//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>

每当用户从英语更改为西班牙语时,我都想显示一个警告框。这是一种奇怪的行为,此代码无法正常工作,它不会在所选索引更改时向我显示任何警报框,但是如果我将警报框代码粘贴到页面加载事件中,它会起作用。页面加载与此有关吗?其次,是否可以记住答案,即如果用户选择了一个复选框说记住我,我应该记住用户在整个会话期间选择的是还是否。关于第二个问题的任何建议都会有所帮助。但是请帮我找出上述代码没有按预期工作的原因。

4

4 回答 4

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);
            }
于 2012-09-18T15:09:34.337 回答
1

onchange="return CheckLanguage(this);"使用而不是注册 javascript 事件ClientScript.RegisterStartupScript

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>
于 2012-09-18T15:14:28.763 回答
1

由于这种情况,您的事件处理程序无法按预期工作

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);                
 }
于 2012-09-18T15:25:38.413 回答
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);
}
于 2012-09-18T15:31:35.253 回答