简体   繁体   中英

Implementing javascript confirm box in code behind on drop down box's selected index change event firing

Inside my boxLang_OnSelectedIndexChanged() event I got this :-

 if (txtbox1.Text != "" && txtbox2.Text  != "")
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);
            txtbox1.Text = "";
            txtbox2.Text = "";
        }

Now what is happening is that. When I select a different item from the drop down list this confirm box does show up BUT it first clears the content of the text boxes and then this confirm box appears. What I want is that the textboxes content should be cleared ONLY when OK is clicked.

What am I doing wrong? Please help.Thanks.

edit @jgauffin ** I typed return where you have. Now what is happening is that its just clearing the textboxes right away..didn't even show the confirm box this time! Whats wrong now?? Please reply..thnx**

edit 2 I tried as follows:-

Inside my BindDropDown() method I added :-

dropdownbox1.Attributes.Add("onChange","DisplayConfirmation()");

then in my aspx page I have added the following script as follows:-

<script type="css/javascript">
function DisplayConfirmation() {
  if (confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.')) {
    __doPostback('dropdownbox1','');
  }
}

</script>

This function is not getting called when drop down's selected index is changed. What's wrong?

edit 3:- ok I changed the line of code as follows:-

 dropdownbox1.Attributes.Add("onchange", "javascript:return DisplayConfirmation()");

Still wont work. I am new to JavaScript and all so having problems. Help will be much appreciated.

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true);

需要返回才能在取消时中断进程。

That's not going to work. You're posting back to the server, then registering the javascript confirm code, and then clearing content. The javascript confirm box will only actually appear once the page has reloaded, by which time you've already cleared the textbox values. It's like you're confusing clientside and server side programming. Client side code like javascript will only execute when the server has finished doing it's thing and the page is loaded.

What you need is this answer, probably:

DropdownList autoposback after client confirmation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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