How to change the colors of a row to indicate its the Birthday of the Employee (for example) in a DataList
<asp:datalist id=’DataList1′ OnItemDataBound =’ItemDB’ GridLines=’Both’ RepeatColumns=’2′ runat=’server’ ShowHeader=’true’> <HeaderStyle Font-Bold=’True’ HorizontalAlign=’Center’ Font-Name=’verdana’ Font-Size=’10pt’></HeaderStyle> <ItemStyle Font-Size=’10pt’></ItemStyle> <Headertemplate> Employee Details </Headertemplate> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem,’LastName’)%> , <%#DataBinder.Eval(Container.DataItem,’FirstName’)%> <br> HireDate : <%#DataBinder.Eval(Container.DataItem,’HireDate’)%> <br> </ItemTemplate> </asp:datalist> 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 ’Populate the DataList with DataSet End Sub Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) Dim BDate As DateTime If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then ’To check if the BirthDate has null values If Not drv.Row(‘BirthDate’).ToString = DBNull.Value.ToString Then ’Assign the BirthDate to variable BDate BDate = DateTime.Parse(drv.Row(‘BirthDate’).ToString) End If ’If the Employee BirthDay is Today ’Change the row Text Color to blue If BDate.ToString(‘dd/MM’) = DateTime.Now.ToString(‘dd/MM’) Then e.Item.ForeColor = Color.Blue e.Item.Font.Bold = True End If End If End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here DataList1.DataSource =BindDataClass.BindData (); DataList1.DataBind (); } protected void ItemDB(Object sender , System.Web.UI.WebControls.DataListItemEventArgs e ) { DataRowView drv = (DataRowView)e.Item.DataItem; DateTime BDate=Convert.ToDateTime (null) ; if ((e.Item.ItemType == ListItemType.Item )||( e.Item.ItemType == ListItemType.AlternatingItem)) { //To check if the BirthDate has null values if( drv.Row[‘BirthDate’].ToString() != DBNull.Value.ToString()) { //Assign the BirthDate to variable BDate BDate = DateTime.Parse(drv.Row[‘BirthDate’].ToString()); } //If the Employee BirthDay is Today //Change the row Text Color to blue if (BDate.ToString(‘dd/MM’) == DateTime.Now.ToString(‘dd/MM’)) { e.Item.ForeColor = Color.Blue; e.Item.Font.Bold = true; } } }
I have a function inside of the .ascx file. How can I call it from the web application page(the .aspx file)
All you need to do is give your user control an ID in the aspx. e.g. <myTagTest:MarcTest id=myUC runat=’server’> </myTagTest:MarcTest> Then in your aspx code, you can simply use the id to call public methods (and properties) defined by the ascx. e.g. VB.NET myUC.writeData(…) C# myUC.writeData(…)
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>