How to change the Calendar Dates using a Dropdownlist
<asp:DropDownList id=’ddlMonth’ style=’Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 24px’ runat=’server’ AutoPostBack=’True’></asp:DropDownList> <asp:Label id=’Label1′ style=’Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 24px’ runat=’server’>Month</asp:Label> <asp:DropDownList id=’ddlYear’ style=’Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 24px’ runat=’server’ AutoPostBack=’True’></asp:DropDownList> <asp:Label id=’Label2′ style=’Z-INDEX: 104; LEFT: 152px; POSITION: absolute; TOP: 24px’ runat=’server’>Year</asp:Label> <asp:Calendar id=’Calendar1′ style=’Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 72px’ runat=’server’></asp:Calendar> VB.NET 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 Not Page.IsPostBack Then ’Populate month in the dropdownlist Dim strMonth As String = ” Dim i As Integer For i = 1 To 12 If i.ToString().Length < 2 Then strMonth = ‘0’ + i.ToString() ddlMonth.Items.Add(New ListItem(strMonth, strMonth)) Else ddlMonth.Items.Add(New ListItem(strMonth, strMonth)) End If Next ddlMonth.Items.FindByValue(DateTime.Now.ToString(‘MM’)).Selected = True ’Populate year in the dropdownlist Dim j As Integer For j = 1900 To 2050 ddlYear.Items.Add(New ListItem(j.ToString(), j.ToString())) Next ddlYear.Items.FindByText(DateTime.Now.ToString(‘yyyy’)).Selected = True End If End Sub ’Page_Load Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged SetCalendarDate() End Sub ’ddlMonth_SelectedIndexChanged Private Sub ddlYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged SetCalendarDate() End Sub ’ddlYear_SelectedIndexChanged Sub SetCalendarDate() Dim dtNewDate As DateTime dtNewDate = DateTime.Parse((Int16.Parse(ddlMonth.SelectedItem.Text) & ‘/1/’ & Int16.Parse(ddlYear.SelectedItem.Text))) Calendar1.TodaysDate = dtNewDate End Sub ’SetCalendarDate C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { //Populate month in the dropdownlist string strMonth=”; for(int i = 1 ;i<=12;i++) { if (i.ToString().Length <2 ) { strMonth =’0′ + i.ToString (); ddlMonth.Items.Add (new ListItem(strMonth,strMonth )) ; } else { ddlMonth.Items.Add (new ListItem(strMonth,strMonth )) ; } } ddlMonth.Items.FindByValue ( DateTime.Now.ToString (‘MM’)).Selected =true; //Populate year in the dropdownlist for(int j = 1900 ;j<=2050;j++) { ddlYear.Items.Add (new ListItem(j.ToString(),j.ToString () )) ; } ddlYear.Items.FindByText (DateTime.Now.ToString (‘yyyy’)).Selected =true; } } private void ddlMonth_SelectedIndexChanged(object sender, System.EventArgs e) { SetCalendarDate(); } private void ddlYear_SelectedIndexChanged(object sender, System.EventArgs e) { SetCalendarDate(); } void SetCalendarDate() { DateTime dtNewDate; dtNewDate =DateTime.Parse (Int16.Parse(ddlMonth.SelectedItem.Text) + ‘/1/’ + Int16.Parse( ddlYear.SelectedItem.Text)); Calendar1.TodaysDate=dtNewDate ; }
How to display all System Colors in a DataList
<asp:DataList id=’DataList1′ runat=’server’ > <HeaderTemplate ><table></HeaderTemplate> <ItemTemplate > <tr> <td bgcolor =<%#Container.DataItem%> runat=server id=’td’> <%#Container.DataItem%> </td> </tr> </ItemTemplate> <FooterTemplate></table></FooterTemplate> </asp:DataList> VB.NET Dim arrlist As New ArrayList Dim enumColor As New KnownColor Dim Colors As Array = [Enum].GetValues(enumColor.GetType()) Dim clr As Object For Each clr In Colors If Not Color.FromKnownColor(CType(clr, KnownColor)).IsSystemColor Then arrlist.Add(clr.ToString()) End If Next DataList1.DataSource = arrlist DataList1.DataBind() C# ArrayList arrlist = new ArrayList (); KnownColor enumColor = new KnownColor(); Array Colors = Enum.GetValues(enumColor.GetType()); foreach(object clr in Colors) { if (!Color.FromKnownColor((KnownColor)clr).IsSystemColor) arrlist.Add ( clr.ToString()); } DataList1.DataSource = arrlist; DataList1.DataBind();
Why is the DataList not displayed on the web page
You must have missed the <ItemTemplate> tag <asp:DataList id=’DataList1′ runat=’server’> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, ‘FieldName’).ToString()%> </ItemTemplate> </asp:DataList>
How to display the Directory names in the Repeater Control
Use namespace System.IO <asp:Repeater id=’Repeater1′ runat=’server’> <ItemTemplate > <%# DataBinder.Eval(Container.DataItem, ‘Name’).ToString() %> </ItemTemplate> </asp:Repeater> VB.NET Dim dir As New DirectoryInfo(Server.MapPath(‘.’)) Repeater1.DataSource = dir.GetDirectories Repeater1.DataBind() C# DirectoryInfo dir = new DirectoryInfo(Server.MapPath(‘.’)); Repeater1.DataSource = dir.GetDirectories(); Repeater1.DataBind();
How to display alphabetically sorted data in Repeater
<asp:Repeater id=’Repeater1′ runat=’server’> <ItemTemplate > <b> <u><p> <%#GetFirstAlphabet(DataBinder.Eval(Container.DataItem, ‘LastName’).ToString())%> </p></u> </b> <%#DataBinder.Eval(Container.DataItem, ‘LastName’).ToString()%> ,<%#DataBinder.Eval(Container.DataItem, ‘FirstName’).ToString()%><br> </ItemTemplate> </asp:Repeater> VB.NET If Not Page.IsPostBack Then ’Populate the Data in the Repeater End If protected function GetFirstAlphabet(ByVal strval As String) As String Dim alphabet As String = ViewState(‘alphabet’) If alphabet = Left(strval, 1) Then Return ” Else alphabet = Left(strval, 1) ViewState(‘alphabet’) = alphabet Return alphabet End If End Function C# if(!Page.IsPostBack) { //Populate the Data in the Repeater } protected string GetFirstAlphabet(string strval) { string alphabet =(string) ViewState[‘alphabet’]; if( alphabet == strval.Substring(0,1) ) { return ”; } else { alphabet = strval.Substring(0,1); ViewState[‘alphabet’] = alphabet; return alphabet; } }