Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Been trying to test a drop down list according to selected value.The else part does work since the code inside is executed, and I am 102% sure that the condition is met, I have that name in my dropdown.Its confusing,i need help.

XML
<asp:DropDownList ID="ddlStaff"  runat="server" AutoPostBack="true"
                 BackColor="#CCCCCC" ForeColor="Black"  Height="18px" Width="167px"
                    onselectedindexchanged="ddlStaff_SelectedIndexChanged">
             </asp:DropDownList>


Below is the code behind the drop down

C#
if (ddlStaff.SelectedIndex.ToString() == "Anele ngqandu")
       {
           MessageBox.Show("ys");
       }
       else
       {
           radDoctors.Checked = true;
       }
Posted
Updated 16-Jun-11 1:49am
v2
Comments
karan joshua 16-Jun-11 6:33am    
yeah . selected index doesn't work sometime when page_load occurs.

the best way to do is add one more button at the DDl and try to handle the selecte d item of the combo box.
RakeshMeena 16-Jun-11 6:41am    
Yes, but SelectedIndex will always give you an integer value (index of the selected item) not a string (value/text) as Anele is trying to use.

You are using SelectedIndex, it should be either SelectedItem.Text or SelectedItem.Value (whichever is applicable).
 
Share this answer
 
v2
Comments
Anele Ngqandu 16-Jun-11 6:27am    
I have tried that and still its not workn,The weird thing when i took that code and used it as a code behind a button, it works.Y is that?
RakeshMeena 16-Jun-11 6:39am    
So as of now where you are putting this code? I mean which event handler/method? one more thing, are you binding your DDl on each request?
Anele Ngqandu 16-Jun-11 6:49am    
the code is ruuning at the back of my button and no binding i did its pure code
RakeshMeena 16-Jun-11 6:52am    
Can you post your markup and code-behind.
Anele Ngqandu 16-Jun-11 6:55am    
<asp:Button ID="btnAddStaff" runat="server" Text="..." Height="24px"
Width="64px" onclick="btnAddStaff_Click" />


protected void btnAddStaff_Click(object sender, EventArgs e)
{
if (ddlStaff.SelectedItem.Text == "Anele ngqandu")
{
MessageBox.Show("ys");
}
else
{
MessageBox.Show("no");
}
}

There u go Sir
hello friend
MIDL
Lable1.Text = DropDownList1.SelectedValue.ToString();
if( label1.text == "selected value of DDBL")
{
       respose.write("done");
}        



try this code its help u.
 
Share this answer
 
Comments
RakeshMeena 16-Jun-11 6:43am    
Why are you taking the value in a label then using it do the comparison. I mean how does it help apart from the fact that you are using SelectedValue not SelectedIndex.
Anele Ngqandu 16-Jun-11 6:51am    
True Rakesh but also the SelectedValue and SelectedIndex is not working.This is strange guys...
you may use DropDownList1.SelectedItem.Text or DropDownList1.SelectedValue for example

C#
if (ddlStaff.SelectedItem.Text == "Anele ngqandu")
       {
           MessageBox.Show("ys");
       }
       else
       {
           radDoctors.Checked = true;
       }
 
Share this answer
 
First you have to check the loading in not inside the postback event then you have to to for ur selectedindexchange event,means that u should not load it on its postback event.


if (ddlStaff.SelectedItem.Text.Tostring() == "Anele ngqandu"")
{
MessageBox.Show("ys");
}
else
{
radDoctors.Checked = true;
}
 
Share this answer
 
Comments
Anele Ngqandu 16-Jun-11 10:59am    
but the dropdown is loaded on postback sir
amit28august 17-Jun-11 0:21am    
it has to load initially, then when u fire any other event, it should get reload, otherwise it will lost its current state, check it up, or u can send me ur whole page code

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