1

I am using datalist control an asp.net using C# and sqlserver 2008.I have a dropdown in datalist and need to display it's current value from database on page load.

I have tried this so far,

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item)
  {
        DropDownList ddlshowit = (DropDownList)e.Item.FindControl("DropDownList4");
        ddlshowit.DataSource = ds;
        ddlshowit.DataTextField = "showit";
        ddlshowit.DataValueField = "showit"; //showit is my column name
        ddlshowit.DataBind();
  }
}

I also tried adding the following declaration in markup: SelectedValue='<%#Eval("showit")%>' but it also didn't work. Please Help

deostroll
  • 11,661
  • 21
  • 90
  • 161
R P
  • 348
  • 2
  • 15
  • possible duplicate of [how to bind a dropdownlist in gridview?](http://stackoverflow.com/questions/7329224/how-to-bind-a-dropdownlist-in-gridview) – deostroll Jul 31 '14 at 07:53

1 Answers1

0

You can find your dropdownlist from your datalist in this way,

Protected void Page_load(object sender,Eventargs e)
{
    foreach(DataList dl in DataList1.Items)
    {
        DropDownList ddlshowit  = (DropDownList)dl.FindControl("DropDownList4");
    }
}

Let me know the output.

gkrishy
  • 756
  • 1
  • 8
  • 33