DropDownList in FormView binding

后端 未结 3 1883
情深已故
情深已故 2021-02-13 23:34

I want to bind dropdownlist to List, in code behind.

 

        
3条回答
  •  情话喂你
    2021-02-13 23:47

    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
        
        
            
    One Two Three

    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"));
            }
        }
    }
    

提交回复
热议问题