简体   繁体   English

下拉菜单SelctIndexChange无法正常工作

[英]Dropdown menu SelctIndexChange not working as expected

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

Whenever the user changes from English to Spanish I want to display an alert box. 每当用户从英语更改为西班牙语时,我都想显示一个警报框。 Its a weird behavior, this code is not working its not showing me any alert box on selected index change but if I paste the alert box code in the page load event it works. 这是一种怪异的行为,此代码无法正常工作,在选择的索引更改上没有显示任何警报框,但是如果我将警报框代码粘贴到页面加载事件中,它将起作用。 Is the pageload has to do anything with this? 页面加载与此有关吗? Secondly is it possible to remember the answer ie if the user selects an checkbox saying remember me, I should remember whether the user selected YES or NO throughout the session. 其次,可以记住答案,即,如果用户选中一个复选框“记住我”,则我应该记住用户在整个会话中是选择是还是否。 any suggestions on the second questions will help. 关于第二个问题的任何建议都将有所帮助。 But please help me find the reason why the above code is not working as expected. 但是,请帮助我找到上述代码无法按预期运行的原因。

Try the following code in your dropdownSelected index changed event to show the alert box 在您的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);
            }

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

HTML 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 使用Javascript

<script Type="text/javascript>

function CheckLanguage(ddl)
{
    return confirm("Are you sure to change language");
}

</script>

your event handler is not working as expected due to this ligne 由于此问题,您的事件处理程序无法正常工作

Server.Transfer(Request.Path);

remove it if possible or try a work arround, and everything will be ok 如果可能的话,将其删除或尝试工作,一切都会好的

EDIT : 编辑:

To get by the issue caused by Server.Transfer try this and see if it makes sens 要解决由Server.Transfer引起的问题,请尝试此操作,看看它是否有意义

in your aspx file 在你的aspx文件中

<asp:HiddenField runat="server" ID="hide" />

in your code behind 在你的代码后面

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);                
 }

You are sending the user to a new page before they have a chance to confirm. 您正在将用户发送到新页面,然后他们才有机会进行确认。 Server.Transfer requests a new page (even though it's the same url) so your js never gets written to the page. Server.Transfer请求一个新页面(即使它是相同的URL),因此您的js永远不会写入该页面。 You need to run the confirm js before the postback ever happens. 您需要在回发发生之前运行确认js。

I don't think you can prevent the SelectedIndexChanged postback either, as dot net adds it's own js event handlers to accomplish that. 我认为您也不能阻止SelectedIndexChanged回发,因为点网会添加自己的js事件处理程序来完成此操作。 I think the least obtrusive method here to make a new button, move the language handler code to the button's click handler, then use client side js to click the button when the user confirms their choice. 我认为这里最不麻烦的方法是制作一个新按钮,将语言处理程序代码移至该按钮的单击处理程序,然后在用户确认选择后使用客户端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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM