tags:

views:

72

answers:

2

Suppose there are three dropDownList in a table and I want to bind it . Let say in one dropDown there is Days ,in second dropDown there is Month and in third dropDown there is year and now i want to bind them so that I can save it in DataBase. How to do that?

A: 

This code will give you a simple 1-31, 1-12, current year to 80 years ago. Not sure if this is exactly what you are looking for, but this is how to dynamically bind them so that you do not need to hardcode all the listitem values on the .aspx page.

vb

Dim index As Integer = 0
For index = 0 To 30
    Me.ddlDay.Items.Insert(index, New ListItem(index + 1, index + 1))
Next
For index = 0 To 11
    Me.ddlMonth.Items.Insert(index, New ListItem(index + 1, index + 1))
Next
For index = DateTime.Now.Year To DateTime.Now.Year - 80 Step -1
    Me.ddlYear.Items.Insert((DateTime.Now.Year - index), New ListItem(index, index))
Next

c#

int index = 0;
for (index = 0; index <= 30; index++) {
this.ddlDay.Items.Insert(index, new ListItem(index + 1, index + 1));
} 
for (index = 0; index <= 11; index++) {
this.ddlMonth.Items.Insert(index, new ListItem(index + 1, index + 1));
}
for (index = DateTime.Now.Year; index >= DateTime.Now.Year - 80; index += -1) {
this.ddlYear.Items.Insert((DateTime.Now.Year - index), new ListItem(index, index));
}

You could be a bit more "fancier" with this and have the day, month names, or have 01, 02, 03 rather than 1, 2, 3, etc... Also you could put the month drop down first, then dynamically bind the Day drop down from the month selection so that you do not allow someone to enter in Feburary 30th for example.

Tim B James
+3  A: 

In my project I had created the web user control for the date from where you can take an idea to fill the three drop down list for your own use.

Here is the design view of the page (.ascx) -

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WUCDate.ascx.cs" Inherits="WUCDate" %>
<table>
    <tr>
        <td>
            <span style="font-size: 10pt" class="ElearningTotalLabel">
            DD</span></td>
        <td style="width: 53px">
            <span style="font-size: 10pt" class="ElearningTotalLabel">
            MM</span></td>
        <td style="width: 67px" >
            <span style="font-size: 10pt" class="ElearningTotalLabel">
            YYYY</span></td>
    </tr>
    <tr>
        <td style="height: 24px" >
            <asp:DropDownList ID="ddldate" runat="server" Height="22px" Width="39px" CssClass="ElearningDropdownbox" Font-Bold="True">
            </asp:DropDownList></td>
        <td style="width: 53px; height: 24px">
            <asp:DropDownList ID="ddlmonth" runat="server" Width="55px" CssClass="ElearningDropdownbox" Font-Bold="True">
                <asp:ListItem Value="01">Jan</asp:ListItem>
                <asp:ListItem Value="02">Feb</asp:ListItem>
                <asp:ListItem Value="03">March</asp:ListItem>
                <asp:ListItem Value="04">April</asp:ListItem>
                <asp:ListItem Value="05">May</asp:ListItem>
                <asp:ListItem Value="06">June</asp:ListItem>
                <asp:ListItem Value="07">July</asp:ListItem>
                <asp:ListItem Value="08">Aug</asp:ListItem>
                <asp:ListItem Value="09">Sept</asp:ListItem>
                <asp:ListItem Value="10">Oct</asp:ListItem>
                <asp:ListItem Value="11">Nov</asp:ListItem>
                <asp:ListItem Value="12">Dec</asp:ListItem>
            </asp:DropDownList></td>
        <td style="width: 67px; height: 24px;">
            <asp:DropDownList ID="ddlyear" runat="server" Width="68px" CssClass="ElearningDropdownbox" Font-Bold="True">
            </asp:DropDownList></td>
    </tr>
</table>

Here is the ascx.cs code for the web user control -

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WUCDate : System.Web.UI.UserControl
{
    //Date user control

    int i;
    private string currentdate = "01";// Here we can define the minimum date 
    private string currentmonth = "01";// Here We Can define the minimum month
    private string currentyear = "2006";// Here we can define the minimum year
    private string date = "01";
    private string month = "01";
    private string year = "2006";

    // fill the date in date dropdown list
    void fillDate()
    {
        string dd;
        for (i = 1; i <= 31; i++)
        {
            if (i <= 9)
            {
                dd = "0" + i.ToString();

            }
            else
            {
                dd = i.ToString();

            }
            ddldate.Items.Add(dd);
        }

    }

    // fill the date in month dropdown list
    void fillMonth()
    {
        string mm;
        for (i = 1; i <= 12; i++)
        {
            if (i <= 9)
            {
               mm ="0" + i.ToString();

            }
            else
            {
                mm = i.ToString();

            }
            //ddlmonth.Items.Add(mm);

        }

    }
    // fill the date in date dropdown list
    void fillYear()
    {
        string yy;
        for (i = 1900; i <= DateTime.Now.Year+5 ; i++)
        {
            yy = i.ToString();
            ddlyear.Items.Add(yy);
        }
    }


    // create the property for get the date
    public string Date
    {
        get
        {
            return date;
        }
        set
        {
            date = value;
        }

    }

    // create the property for get the current date
    public string CurrentDate
    {
        get
        {
            return currentdate;
        }
        set
        {
            currentdate = value;
        }
    }


    // create the property for get the month
    public string Month
    {
        get
        {
            return month;
        }
        set
        {
            month = value;
        }

    }
    // create the property for get the current month
    public string CurrentMonth
    {
        get
        {
            return currentmonth;
        }
        set
        {
            currentmonth = value;
        }
    }

    // create the property for get the year
    public string Year
    {
        get
        {
            return year;
        }
        set
        {
            year = value;
        }


    }

    // create the property for get the current year
    public string CurrentYear
    {
        get
        {
            return currentyear;
        }
    }

    //bind the control on page load
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {

            this.Date = ddldate.SelectedValue;
            this.Month = ddlmonth.SelectedValue;
            this.Year = ddlyear.SelectedValue;
            currentdate = this.Date;
            currentmonth = this.Month;
            currentyear = this.Year;
            display();

         }
        else
        {
            fillDate();
            fillMonth();
            fillYear();
            currentdate = this.Date;
            currentmonth = this.Month;
            currentyear = this.Year;
            this.Date = ddldate.SelectedValue;
            this.Month = ddlmonth.SelectedValue;
            this.Year = ddlyear.SelectedValue;
            display();
        }


    }

    // set current date in control
    protected void display()
    {
        ddldate.SelectedValue = this.CurrentDate.ToString();
        ddlmonth.SelectedValue = this.CurrentMonth.ToString();
        ddlyear.SelectedValue = this.CurrentYear.ToString();
    }
}

After this we find the filled drop down list as your requirement.

naval
naval , there should be a method to bind exact number of days with each month. we can hardcode month and year , but days are not 31 for every month.
AsifQadri
Ya tahnks i amagree with with you
naval