C# .NET - cannot get the dropdownlist selected item

Asked By muthuraman alexander on 14-Nov-11 12:26 AM
hi ALL
i'm using the dropdown list control in my page
when select some item in the drop down and press the submit button
i need to get the selected item from the drop down
but i getting only the first item not the selected item

i loading the dropown items dynamically from the database

my code is

aspx
------

<

asp:DropDownList runat="server" Width="120px" ID="drp_SubhurbList"

OnSelectedIndexChanged = "drp_SubhurbList_SelectedIndexChanged" >

</asp:DropDownList>

<asp:ImageButton runat="server" ID="btnCalculateShipping" OnClick="UpdateButton_Click"

SkinID="Continue" AlternateText="GO" Style="position:absolute" />


c#
---




protected void UpdateButton_Click(object sender, EventArgs e)

{






if (drp_SubhurbList != null)



if (drp_SubhurbList.SelectedItem != null && drp_SubhurbList.SelectedItem.Text != string.Empty)

{

Session[



"state"] = string.Empty;

Session[


"state"] = drp_SubhurbList.SelectedItem.Text.ToString();

}
}

public void bindDropDownList()
{

//dtCities is a datatable
drp_SubhurbList.Items.Clear();

drp_SubhurbList.DataSource = dtCities;

drp_SubhurbList.DataTextField =



"state";

drp_SubhurbList.DataValueField =


"zipcode";
}

it always getting the first item forexample
if i having the items like

chennai
bangalore
delhi
kashir
mumbai

and when i select delhi and press submit button
it returning the  chennai for always

please advice me

thanks in advance
MUTHU

Suchit shah replied to muthuraman alexander on 14-Nov-11 12:45 AM
Just enable the AutoPostback propery of the Dropdown list to True and check that out. just do it like below change

<

asp:DropDownList runat="server" Width="120px" ID="drp_SubhurbList" 

Autopostback="True" OnSelectedIndexChanged = "drp_SubhurbList_SelectedIndexChanged" >

</asp:DropDownList>


dipa ahuja replied to muthuraman alexander on 14-Nov-11 01:22 AM
while binding dropdownlist add the dropdownlist.DataBind() method

public void bindDropDownList()
  {
    //dtCities is a datatable
    drp_SubhurbList.Items.Clear();
    drp_SubhurbList.DataSource = dtCities;
    drp_SubhurbList.DataTextField =
 
 
    "state";
    drp_SubhurbList.DataValueField = "zipcode";
    drp_SubhurbList.DataBind();
  }

and on submit button click instead of SelectedItem.Text , try the SelectedValue


muthuraman alexander replied to dipa ahuja on 14-Nov-11 05:46 AM
hi
thank u for your reply
i do the same why u had prescriped
but it was not working

i populating the drop dynamically not in the page load

plesase  advice me

thanks in advance
MUTHU