I want to bind dropdownlist to List
,
in code behind.
Made an example which will set the dropdown in the DataBound event.
Here is the markup
The way to use the ddl, is to find it with findcontrol() during DataBound event.
When you have the control in the DataBound event, you can also bind the dropdown to your List<>
Hope this helps.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
Untitled Page
Here is the code behind:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List list = new List();
list.Add("Some");
list.Add("Other");
FormView1.DataSource = list; //just to get the formview going
FormView1.DataBind();
}
protected void FormView1_DataBound(object sender, EventArgs e)
{
DropDownList ddl = null;
if(FormView1.Row != null)
ddl = (DropDownList) FormView1.Row.FindControl("DropDownList1");
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Two"));
}
}
}