Dinav Ahire
Dinav Ahire

Reputation: 583

SelectedIndex is always 0 in dropdownlist(asp.net c#)

I am retrieving data from table Technology in dropdownlist ddlTechnology .

I have TechnologyId as primary key in the table and values are 1,2,3,4

Now as per the technology,i have to add questions in question bank. but when i select any item in dropdownlist, my SelectedIndex is always 0.

i want TechnologyId from dropdownlist.

i have tried following code but its not working

using (dbDataContext dt = new dbDataContext())
        {
            var qry = from i in dt.Technologies
                      select i;

            ddlTechnology.DataSource = qry;
            ddlTechnology.DataValueField = "TechnologyId";
            ddlTechnology.DataTextField = "TechnologyName";
            ddlTechnology.DataBind();
            ddlTechnology.Items.Insert(0, new ListItem("Select Technology", ""));
        }

Add button to add question according to selected technology.

protected void btnAdd_Click(object sender, EventArgs e)
{
    using (dbDataContext dt = new dbDataContext())
    {
        Question objQtn = new Question();
        objQtn.Question1 = txtQuestion.Text;
        objQtn.Option1 = txtOption1.Text;
        objQtn.Option2 = txtOption2.Text;
        objQtn.Option3 = txtOption3.Text;
        objQtn.Answer = txtAnswer.Text;
        // below here selectedIndex is always zero..
        objQtn.TechnologyId = ddlTechnology.SelectedIndex;
        dt.Questions.InsertOnSubmit(objQtn);
        dt.SubmitChanges();
        txtAnswer.Text = "";
        txtOption1.Text = "";
        txtOption2.Text = "";
        txtOption3.Text = "";
        txtQuestion.Text = "";
    }
}

Upvotes: 3

Views: 11226

Answers (3)

Imad
Imad

Reputation: 7490

Reason1:

Seems like you are binding dropdownlist on every postback. If that is a problem then keeping your load code in !IsPostBack shou work.

if(!IsPostBack)
{
   using (dbDataContext dt = new dbDataContext())
        {
            var qry = from i in dt.Technologies
                      select i;

            ddlTechnology.DataSource = qry;
            ddlTechnology.DataValueField = "TechnologyId";
            ddlTechnology.DataTextField = "TechnologyName";
            ddlTechnology.DataBind();
            ddlTechnology.Items.Insert(0, new ListItem("Select Technology", ""));
        }
}

Reason 2:

In some cases if programmer disables ViewState property of any control/page then also control looses it's value on postback.

Upvotes: 10

MehulThummar
MehulThummar

Reputation: 11

Need to bind dropdown list in the below event.

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    // Bind your dropdown list
  }
} 

Upvotes: 1

VCody
VCody

Reputation: 516

Please find the code for ref. hope it will help you: (objQtn.TechnologyId = ddlTechnology.SelectedIndex;) one of these two is of string type check once.

Index.aspx

    <asp:DropDownList ID="ddlCloth" runat="server"></asp:DropDownList>
    <asp:Button  runat="server" ID="btnSave" OnClick="btnSave_Click" />

Index.aspx.cs

         protected void Page_Load(object sender, EventArgs e)
         {
         if (!Page.IsPostBack)
         {


            DataTable dt1 = new DataTable();
            dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Name") });
            dt1.Rows.Add(1, "Shirt");
            dt1.Rows.Add(2, "Jeans");

            ddlCloth.DataSource = dt1;
            ddlCloth.DataTextField = "Name";
            ddlCloth.DataValueField = "Id";
            ddlCloth.DataBind();
        }


    protected void btnSave_Click(object sender, EventArgs e)
    {


        int id = Convert.ToInt32(ddlCloth.SelectedItem.Value);
        string text = Convert.ToString(ddlCloth.SelectedItem.Text);
        int index=Convert.ToInt32(ddlCloth.SelectedIndex);
    }

Upvotes: 0

Related Questions