Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (DropDownList1.SelectedIndex >= 0 )
       {
           Literal2.Text  = DropDownList1.SelectedIndex .ToString ();
       }

i am not able to show a selected index value in a literal within the if loop, in the above code its showing the selected index value as 0, any alternative please suggest me.
Posted
Updated 6-Dec-11 23:08pm
v2
Comments
D K N T H 7-Dec-11 5:08am    
code tag added
Addy Tas 7-Dec-11 6:23am    
Are you sure that the Selected index has changed? Are you using the code in the SelectedIndexChanged event handler?

Just tested the same and "it works on my PC" :)
Vivek Shankar 7-Dec-11 6:30am    
zero is getting passed to literal, i tried in SelectedIndexChanged event, its working in page load event but within a if loop it does not working

try this

if (DropDownList1.SelectedIndex != 0 )
       {
           Literal2.Text  = DropDownList1.SelectedIndex .ToString ();
       }
 
Share this answer
 
Comments
Vivek Shankar 7-Dec-11 6:20am    
nope, it's not working,
Your code is correct just write it into index change or set AutopostBack property of dropdownlist to True.

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex >= 0)
            lbl.Text = DropDownList1.SelectedIndex.ToString();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900