ASP.NET - 用户控件自定义数据绑定

时间:2010-10-19 07:54:35

标签: asp.net data-binding user-controls

我开发了用户控制出生日期,其中包含3个DropDownList。 我需要进行数据绑定,但我不知道如何。

    public partial class DofControl :   System.Web.UI.UserControl {

        public const int YearsCount = 100;
        int _year;
        Months _month;

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack ) {
                 Bind();

            }
            if (SessionWrapper.JustInserted)
            {
                Bind();
                SessionWrapper.JustInserted = false;
            }

        }

        public object DataSource {
            get; set;
        }

        private void Bind() {
            int[] years = new int[YearsCount];
            int from = DateTime.Now.Year - 5;
            int to = from - 100;


            for (int i = 0; i < YearsCount; i++) {
                years[i] = to;
                to++;
            }
            ddlYear.DataSource = years;
            ddlYear.DataBind();

            ddlMonth.DataSource = Enum.GetNames(typeof(Months));
            ddlMonth.DataBind();


            _year = Int32.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);

            BindDays(_year, _month);
            if (DataSource==null)
            {
                ddlYear.SelectedValue = years.Max().ToString();
                ddlMonth.SelectedValue = Months.January.ToString();
                ddlDay.SelectedValue = "1";
            }
            else
            {
                ddlYear.SelectedValue = Convert.ToDateTime(DataSource).Year.ToString();
                ddlMonth.SelectedValue = Enum.GetName(typeof(Months), Convert.ToDateTime(DataSource).Month);
                ddlDay.SelectedValue = Convert.ToDateTime(DataSource).Day.ToString();
            }
         }


        enum Months { January = 1    //.......    }



//Is this right?
        [Bindable(true,BindingDirection.TwoWay)]
        public DateTime Date {
            private get {
                return DateTime.Parse(string.Format("{0}/{1}/{2}", ddlDay.Text, ddlMonth.Text, ddlYear.Text));
            }
            set
            {
                ddlYear.SelectedValue = value.Year.ToString();
                ddlMonth.SelectedValue = value.Month.ToString();
                ddlDay.SelectedValue = value.Day.ToString();
            }
        }




        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e) {


            _year = int.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);
            BindDays(_year, _month);
        }


        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e) {
            _year = int.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);
            BindDays(_year, _month);
        }




        public bool IsLeapYear(int year) { //.....  }


        private void BindDays(int year, Months month) {
            List<int> days = new List<int>();
            switch (month) {
                case  Months.January:
                case Months.March:
                    for (int i = 1; i <= 31; i++)
                        days.Add(i);
                    break;
                case Months.February:
               //does not matter
        }

    }

我在asp:DetailsView中的页面Update.aspx中使用DofControl。

<asp:ObjectDataSource ID="odsOneStudent" runat="server" 
        SelectMethod="GetStudentById"
..////
<asp:DetailsView 
//......

<Fields>

   <asp:BoundField DataField="SurName" HeaderText="SurName" />
   <asp:TemplateField HeaderText="Dof">
       <EditItemTemplate>
        <uc:Dof runat="server" ID="dfDof"  Date='<%#Eval("Dof") %>'  />
                    </EditItemTemplate>
                </asp:TemplateField>
 </Fields>
</asp:DetailsView>

我不工作,我收到错误“ddlMonth”有一个无效的SelectedValue,因为它在项目列表中不存在。 参数名称:值“

我错过了什么吗?

更新:我收到错误:“数据绑定方法(如Eval(),XPath()和Bind()只能在数据绑定控件的上下文中使用”

1 个答案:

答案 0 :(得分:0)

当您设置SelectedValue的{​​{1}}时,该值必须包含在列表的项目中。

检查您设置DropDownList的位置,您所使用的值是否正确并包含在SelectedValue