首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在asp.net中绑定数据?

如何在asp.net中绑定数据?
EN

Stack Overflow用户
提问于 2010-09-20 15:39:31
回答 2查看 1.1K关注 0票数 1

假设一个表中有三个dropDownList,我想要绑定它。假设在一个dropDown中有天,在第二个dropDown中有月,在第三个dropDown中有年,现在我想绑定它们,这样我就可以将其保存在DataBase中。如何做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-20 17:39:32

在我的项目中,我已经为日期创建了web用户控件,您可以从该日期开始考虑填充三个下拉列表以供自己使用。

这是页面的设计视图(.ascx) -

代码语言:javascript
运行
复制
<%@ 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>

以下是web用户控件的ascx.cs代码-

代码语言:javascript
运行
复制
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();
    }
}

在此之后,我们会根据您的要求找到已填充的下拉列表。

票数 2
EN

Stack Overflow用户

发布于 2010-09-20 16:26:30

这段代码会给你一个简单的1-31,1-12,当前年份到80年前。我不确定这是否是您想要的,但是这是如何动态绑定它们的,这样您就不需要在.aspx页面上硬编码所有的列表项值。

vb

代码语言:javascript
运行
复制
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#

代码语言:javascript
运行
复制
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));
}

你可以用这个更“花哨”一点,有日期,月份的名字,或者01,02,03而不是1,2,3,等等……此外,您还可以先放入month下拉菜单,然后从month选项中动态绑定Day下拉菜单,这样您就不允许某人在例如2月30日输入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3749460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档