开发者

ASP.NET - User control custom data binding

开发者 https://www.devze.com 2023-01-20 22:10 出处:网络
I develop user control Date ofBirth, which contains 3 DropDownList. I need to make data binding, but I dunno how.

I develop user control Date of Birth, which contains 3 DropDownList. I need to make data binding, but I dunno how.

    public partial class D开发者_运维问答ofControl :   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
        }

    }

I use DofControl in a page Update.aspx inside asp:DetailsView.

<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>

I does not work, I get an error "ddlMonth' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value"

Am I miss anything?

update: I get an error: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"


When you set the SelectedValue of a DropDownList the value must be contained in the list's items.

Check where you are setting the SelectedValue that the value you are using is correct and contained in the DropDownList.Items

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号