opening popup window on selecting from a dropdown List .... urgent

hi ,

Lookz like i have a simple probelm ...I would like to open a popup window on selecting a particualr item in a dropdown menu ...

I tried doing this in code behind

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if (DropDownList1.SelectedItem.Text== "test1")
                  {
                        DropDownList1.Attributes.Add("onchange","window.open('hidden2.aspx',null,'height=250, width=250,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no ');");
                        
                  }
                  else{
                        //do something else
                  }
            }

"test" and "test1" are the two values in the drop down list ....but the popup opens even when i select "test" ..i want it to work only when the user selects "test1"

Hope u guys can help me out

Cheers
Ashwin
Ashwin_shastryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nauman_ahmedCommented:
Try comparing the values.

if (DropDownList1.SelectedItem.Value == "Value")
{
 
}

HTH, Nauman.
Kevin HaysIT AnalystCommented:
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
      if (this.comboBox1.SelectedItem.ToString() == "test1") {
            MessageBox.Show(this.comboBox1.SelectedItem.ToString());
      } else {
            MessageBox.Show("Failed");
            }
}

Tested it on my system and it works fine.
Kevin HaysIT AnalystCommented:
Well, I was using a combobox though.

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ashwin_shastryAuthor Commented:
Hey guys ,..iam usin web forms ... not winodws application ,...i want to display a new aspx page ,...


Hope u can help me out ..
cheers
Ashwin
Kevin HaysIT AnalystCommented:
LOL, I realized that after I already posted.  Sorry :(

Anyway your code looks fine as far as the logic goes for testing the condition.  I would use either SelectedItem.Text or SelectedItem.ToString(), SelectedItem.Text() like you have already.

I don't see any reason why it's opening a window up everytime.

Can you place breakpoints in the code like in windows ??

DivinityCommented:
Your problem is the fact that you are mixing JavaScript and .NET code. The way you implemented it, the "onchange" code gets rendered the first time you select "test1" as the option. And since the control probably saves its state in the ViewState the "onchange" attribute will always be rendered from that point on. The problem lies with the fact that the JavaScript "onchange" command executes everytime you change the value of the select box, even before it is posted back to the server. So your "onchange" code executes everytime you change its value. It doesn't check, what the new value is, because that would have to be done by JavaScript, not in your ASP.NET code.
There are two ways to fix the problem:

Number 1: You convert your code to check which item is selected to JavaScript and you render it to the page. You can then remove the check from your ASP.NET code.

Number 2: You keep the checking code in your ASP.NET code, but you place the code that adds the "onchange" in the Page_Load event. When you have checked which item is selected you instead render something like the following (not tested for spelling):
Literal lit = new Literal();
l.Text = @"<script language=""JavaScript"">window.open('hidden2.aspx',null,'height=250, width=250,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no ')</script>";
this.Controls.Add(lit);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.