Предположим, что в таблице есть три dropDownList, и я хочу связать их. Скажем, в одном dropDown есть Дни, во втором dropDown есть месяц, а в третьем dropDown - год, и теперь я хочу связать их, чтобы сохранить его в DataBase. Как это сделать?Как связать данные в asp.net?
ответ
В моем проекте я создал веб-пользовательский элемент управления на дату, откуда вы можете принять идею, чтобы заполнить три выпадающих списка для вашего собственного использования.
Вот вид дизайна страницы (.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>
Вот 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();
}
}
После этого мы находим заполненные выпадающий список как ваше требование.
Этот код предоставит вам простой 1-31, 1-12, текущий год до 80 лет назад. Не уверен, что это именно то, что вы ищете, но это то, как динамически связывать их, так что вам не нужно жестко кодировать все значения listitem на странице .aspx.
В.Б
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));
}
Вы могли бы быть немного более "любитель" с этим и есть день, названия месяцев, или 01, 02, 03, а не 1, 2, 3 и т. Д. ... Также вы можете сначала убрать месяц, а затем динамически привязать дату «День» вниз от выбора месяца, чтобы вы не позволили кому-то войти в 30-е февраля, например.
- 1. Как связать данные с меткой в asp.net
- 2. Связать данные в таблице данных в ASP.NET
- 3. Как связать данные в HTML таблицы в asp.net веб-формы
- 4. Как связать данные в шаблон макета в ASP.NET MVC Razor
- 5. Как связать данные, возвращенные json, в jquery gridview в asp.net?
- 6. Как связать данные в Webgrid usig PartialView в ASP.net MVC
- 7. Как связать данные в asp.net C# treeview control
- 8. как я могу связать данные случайно в radioobuttonlist asp.net
- 9. как связать данные с маркировать в asp.net mvc3
- 10. Как связать данные с последовательностью в asp.net stacked coulmn chart
- 11. как связать данные с ListView в ASP.NET с помощью JQuery
- 12. Как связать данные модели с пользователем в ASP.NET MVC4?
- 13. Как связать данные с ретранслятором в asp.net C#?
- 14. ASP.NET Как связать данные в DropDownList вне GridView?
- 15. Как связать диаграмму asp.net?
- 16. Как связать метку в GridView в asp.net
- 17. Как связать данные в MongoDB?
- 18. Как связать данные в представлении
- 19. Как связать данные в yii
- 20. Как связать данные JSon
- 21. Как связать данные объекта
- 22. AngularJS Как связать данные
- 23. как связать эти данные?
- 24. Как связать динамические данные?
- 25. Как связать привязанные данные с ASP.Net к мобильному?
- 26. Как связать дополнительные данные с элементом управления списком asp.net?
- 27. Связать данные в группах
- 28. как связать JSON массива в ASP.NET MVC
- 29. Связать данные с аутентифицированным пользователем в ASP.NET MVC-оптимизации
- 30. Как связать многоуровневые данные json с ретранслятором в asp.net или преобразовать данные json в таблицу данных
, если у них уже есть данные в них. Дни, месяц и год - тогда для чего вам нужно их привязать? – InSane