如何在asp.net中绑定数据?
假设一个表中有三个dropDownList,我想绑定它。假设在一个下拉列表中有天,在第二个下拉列表中有月,在第三个下拉列表中有年,现在我想绑定它们,以便我可以将其保存在数据库中。如何做到这一点?此代码将为您提供一个简单的1-31,1-12,从当前年份到80年前。 不确定这是否正是您要查找的内容,但这就是如何动态绑定它们,以便您不需要硬编码.aspx页面上的所有listitem值 vb c#如何在asp.net中绑定数据?,asp.net,Asp.net,假设一个表中有三个dropDownList,我想绑定它。假设在一个下拉列表中有天,在第二个下拉列表中有月,在第三个下拉列表中有年,现在我想绑定它们,以便我可以将其保存在数据库中。如何做到这一点?此代码将为您提供一个简单的1-31,1-12,从当前年份到80年前。 不确定这是否正是您要查找的内容,但这就是如何动态绑定它们,以便您不需要硬编码.aspx页面上的所有listitem值 vb c# int索引=0; 对于(index=0;index在我的项目中,我已经为日期创建了web用户控件,从中您可
int索引=0;
对于(index=0;index在我的项目中,我已经为日期创建了web用户控件,从中您可以考虑填充三个下拉列表供自己使用
这是页面(.ascx)的设计视图-
DD
嗯
YYYY
简
二月
前进
四月
也许
六月
七月
八月
九月
十月
十一月
12月
下面是web用户控件的ascx.cs代码-
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();
}
}
使用系统;
使用系统数据;
使用系统配置;
使用系统集合;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
公共部分类WUCDate:System.Web.UI.UserControl
{
//日期用户控件
int i;
私有字符串currentdate=“01”//这里我们可以定义最小日期
private string currentmount=“01”//这里我们可以定义最小月份
private string currentyear=“2006”//这里我们可以定义最小年份
私有字符串date=“01”;
私有字符串month=“01”;
私人字符串year=“2006”;
//在日期下拉列表中填写日期
作废日期()
{
字符串dd;
对于(i=1;i)如果他们已经有了数据,即天、月和年,那么你需要绑定它们做什么?naval,应该有一种方法来绑定每个月的确切天数。我们可以硬编码月和年,但每个月的天数不是31天。我同意你的看法
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));
}
<%@ 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>
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();
}
}