1

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.

Serenity
  • 4,968
  • 19
  • 65
  • 104

2 Answers2

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

return is needed to break the process on cancel.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • where do I need to type return? – Serenity Jan 20 '11 at 13:37
  • ok 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 – Serenity Jan 20 '11 at 13:53
  • As @Andrew says, you need to register the script when initially showing the page. Not on the select list post back. In other words: Combine both answers to get it working. – jgauffin Jan 20 '11 at 14:05
1

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

Community
  • 1
  • 1
Andrew Johns
  • 705
  • 1
  • 6
  • 26