繁体   English   中英

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

[英]ASP.NET - User control custom data binding

我开发了用户控件的“出生日期”,其中包含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()之类的数据绑定方法只能在数据绑定控件的上下文中使用”

设置DropDownListSelectedValue ,该值必须包含在列表的项目中。

检查您要在何处设置SelectedValue ,以确保所使用的值正确并且包含在DropDownList.Items

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM