用三个dropdownlist分别显示日期的年月日。
现在已经能显示日期,但是 希望 pageload的时候没有显示准确日期 ,而是YYYY MM DD显示日期的时候,格式是 6/7/2008
希望显示的格式是: 06/07/2008
下面是我的代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim AlYear As New ArrayList
Dim i As Integer
For i = 2002 To 2010
AlYear.Add(i)
Next
Dim AlMonth As New ArrayList
For i = 1 To 12
AlMonth.Add(i)
Next
If Not Me.IsPostBack Then
DropDownList3.DataSource = AlYear
DropDownList3.DataBind() DropDownList2.DataSource = AlMonth
DropDownList2.DataBind() Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month)
End If End Sub Private Function CheckLeap(ByVal year As Integer) As Boolean
If (year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0) Then
Return True
Else
Return False
End If
End Function
Private Sub BindDays(ByVal year As Integer, ByVal month As Integer)
Dim i As Integer
Dim AlDay As New ArrayList Select Case month
Case 1, 3, 5, 7, 8, 10, _
12
For i = 1 To 31
AlDay.Add(i)
Next
Exit Select
Case 2
If CheckLeap(year) = False Then
For i = 1 To 29
AlDay.Add(i)
Next
Else
For i = 1 To 28
AlDay.Add(i)
Next
End If
Exit Select
Case 4, 6, 9, 11
For i = 1 To 30
AlDay.Add(i)
Next
Exit Select
End Select
DropDownList1.DataSource = AlDay
DropDownList1.DataBind()
End Sub Private Sub DropDownList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month)
End Sub Private Sub DropDownList3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month) End Sub
现在已经能显示日期,但是 希望 pageload的时候没有显示准确日期 ,而是YYYY MM DD显示日期的时候,格式是 6/7/2008
希望显示的格式是: 06/07/2008
下面是我的代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim AlYear As New ArrayList
Dim i As Integer
For i = 2002 To 2010
AlYear.Add(i)
Next
Dim AlMonth As New ArrayList
For i = 1 To 12
AlMonth.Add(i)
Next
If Not Me.IsPostBack Then
DropDownList3.DataSource = AlYear
DropDownList3.DataBind() DropDownList2.DataSource = AlMonth
DropDownList2.DataBind() Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month)
End If End Sub Private Function CheckLeap(ByVal year As Integer) As Boolean
If (year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0) Then
Return True
Else
Return False
End If
End Function
Private Sub BindDays(ByVal year As Integer, ByVal month As Integer)
Dim i As Integer
Dim AlDay As New ArrayList Select Case month
Case 1, 3, 5, 7, 8, 10, _
12
For i = 1 To 31
AlDay.Add(i)
Next
Exit Select
Case 2
If CheckLeap(year) = False Then
For i = 1 To 29
AlDay.Add(i)
Next
Else
For i = 1 To 28
AlDay.Add(i)
Next
End If
Exit Select
Case 4, 6, 9, 11
For i = 1 To 30
AlDay.Add(i)
Next
Exit Select
End Select
DropDownList1.DataSource = AlDay
DropDownList1.DataBind()
End Sub Private Sub DropDownList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month)
End Sub Private Sub DropDownList3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim year As Integer, month As Integer
year = Int32.Parse(DropDownList3.SelectedValue)
month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(year, month) End Sub
解决方案 »
- 寻找调用word模板实现直接打印的代码 或系统
- 网站中的个别生成的静态html文件,访问提示:HTTP 错误 401.3。服务器上用记事本方式打开这个文件,提示“拒绝访问”,内容是空的,文件显示的大小是正常的
- 关于如何不提交服务器 选择radiobutton让panel控件现实或隐藏的问题
- Webcontrol 取不到更新后的值
- 急,各位会用vs.net2005的请进
- 如何设置 ASP.NET用户权限?
- 关于登陆的一个问题,很困扰!!!!!请大家帮忙!!!
- 送分求解!!!!速度啊
- 怎么样把图片用程序存入数据(SQL)库?
- 访问web service 报错Server was unable to process request
- 如何做一个像SINA发送邮件时的效果?
- aspx文件编译错误!!!!
格式化下吧……
string year = DropDownList1.SelectedValue;
string month = DropDownList2.SelectedValue;
string day = DropDownList2.SelectedValue;Response.Write(Convert.ToDateTime(year + "-"+month+"-"+day).ToString("dd/MM/yyyy"))
我是要在dropdownlist 里面显示日期是DD07 ,MM07
就是在dropdown list 下拉的时候就显示的格式是两位数的
string month = DropDownList2.SelectedValue;
string day = DropDownList2.SelectedValue;
string dateValue=String.Format("{0:d2}/{1:d2}/{3:d2}",day,month,year);
Response.Write(dateValue);
而是 string “YYYY” “MM” ”DD“
Dim AlMonth As New ArrayList
For i = 1 To 12
AlMonth.Add(i)
Next
绑定时判断i,如果i<10,添加:0
for (int i = 1; i <= 12; i++)
{
DropDownList2.Items.Add(i.ToString().length>2?i.ToString():"0"+i.ToString);
}
而是 string “YYYY” “MM” ”DD“
我的意思是 pageload 的时候 在dropdownlist 中默认显示的是字符串
“YYYY” “DD” “MM” 方便别人知道要填入的是日期还是月份
不是将数字tostring.
Dim month As String = DropDownList2.SelectedValue
Dim day As String = DropDownList2.SelectedValue
Dim dateValue As String = [String].Format("{0:d2}/{1:d2}/{3:d2}", day, month, year)
DropDownList3.Items.Add(New ListItem(year.ToString(), year.ToString()))
Next For month As Integer = 1 To 12
DropDownList2.Items.Add(New ListItem(month.ToString("00"), month.ToString()))
Next另外你的BindDays方法根本不用这么麻烦,建议你以后多查阅一下MSDN; Private Sub BindDays(ByVal year As Integer, ByVal month As Integer)
Dim days = Date.DaysInMonth(year, month) For day As Integer = 1 To days
DropDownList1.Items.Add(New ListItem(day.ToString("00"), day.ToString()))
Next End Sub
DropDownList2.Items.Insert("MM", 0);
DropDownList1.Items.Insert("DD", 0);
我已经试过很多方法都没有用呢?
DropDownList3.Items.Insert(0, New ListItem("YYYY"))
DropDownList3.Items.FindByText("YYYY").Selected = True
DropDownList2.Items(0).Selected = True
DropDownList1.SelectedValue = "DD"
每次显示的都是 1 1 2002
year = Int32.Parse(DropDownList3.SelectedValue)的时候出错啊
显示:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format.Source Error:
Line 92: Dim year As Integer, month As Integer
Line 93: year = Int32.Parse(DropDownList3.SelectedValue)
Line 94: month = Int32.Parse(DropDownList2.SelectedValue)
Line 95: BindDays(year, month)
Line 96: End Sub
看看DropDownList3.SelectedValue值
我的代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If RadioButton1.Checked Then
DropDownList1.Enabled = True
DropDownList2.Enabled = True
DropDownList3.Enabled = True
End If If RadioButton2.Checked Then DropDownList4.Enabled = True
DropDownList5.Enabled = True
DropDownList6.Enabled = True End If
For year As Integer = 2002 To 2010
DropDownList3.Items.Add(New ListItem(year.ToString(), year.ToString()))
Next For month As Integer = 1 To 12
DropDownList2.Items.Add(New ListItem(month.ToString("00"), month.ToString()))
Next
End Sub Private Sub BindDays(ByVal year As Integer, ByVal month As Integer)
Dim days = Date.DaysInMonth(year, month) For day As Integer = 1 To days
DropDownList1.Items.Add(New ListItem(day.ToString("00"), day.ToString()))
Next End Sub Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Dim year As Integer, month As Integer
year = Convert.ToInt32(DropDownList3.SelectedValue)
month = Convert.ToInt32(DropDownList2.SelectedValue)
BindDays(year, month)
End Sub Private Sub DropDownList3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList3.SelectedIndexChanged
Dim year As Integer, month As Integer
year = Convert.ToInt32(DropDownList3.SelectedValue)
month = Convert.ToInt32(DropDownList2.SelectedValue)
BindDays(year, month)
End Sub
If Not Me.IsPostBack Then
End If
里面
If Not Me.IsPostBack Then
For year As Integer = 2002 To 2010
DropDownList3.Items.Add(New ListItem(year.ToString(), year.ToString()))
Next For month As Integer = 1 To 12
DropDownList2.Items.Add(New ListItem(month.ToString("00"), month.ToString()))
Next End If
所以我选了月份的时候,就会出错说类型不正确。
但是我要在它 没有选择的时候显示 DD MM YYYY
请问这个怎么解决啊?
谢谢各位了!
'Put user code to initialize the page here
If RadioButton1.Checked Then
DropDownList1.Enabled = True
DropDownList2.Enabled = True
DropDownList3.Enabled = True
End If If RadioButton2.Checked Then DropDownList4.Enabled = True
DropDownList5.Enabled = True
DropDownList6.Enabled = True End If
If Not Me.IsPostBack Then
For year As Integer = 2002 To 2010
DropDownList3.Items.Add(New ListItem(year.ToString(), year.ToString()))
Next For month As Integer = 1 To 12
DropDownList2.Items.Add(New ListItem(month.ToString("00"), month.ToString()))
Next
End If End Sub Private Sub BindDays(ByVal year As Integer, ByVal month As Integer)
Dim days = Date.DaysInMonth(year, month) For day As Integer = 1 To days
DropDownList1.Items.Add(New ListItem(day.ToString("00"), day.ToString()))
Next End Sub
Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Try
DropDownList1.Items.Clear()
DropDownList1.Items.Add("DD")
Dim year As Integer, month As Integer
year = Integer.Parse(DropDownList3.SelectedValue)
month = Integer.Parse(DropDownList2.SelectedValue)
BindDays(year, month) Catch ex As Exception End Try
End Sub Private Sub DropDownList3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList3.SelectedIndexChanged
Dim year As Integer, month As Integer
year = Integer.Parse(DropDownList3.SelectedValue)
End Sub
这样总算是勉强做出来了,谢谢各位个我的帮助了!!
非常感谢ojlovecd !!