How to use a Hyperlink in a DataList

<asp:DataList id='DataList1' runat='server'> <ItemTemplate> <asp:HyperLink Runat =server NavigateUrl =’<%#'webform1.aspx?id=' + DataBinder.Eval(Container.DataItem, 'productid').ToString()%>’ ID='Hyperlink1'> <%#DataBinder.Eval(Container.DataItem, 'ProductName')%> </asp:HyperLink> </ItemTemplate> </asp:DataList>

How to display checkbox in a DataList and highlight a row if the value of checkbox is true

<asp:datalist id='DataList1' OnItemDataBound ='ItemDB' runat='server'> <ItemTemplate > <asp:CheckBox id='chkDiscontinued' Runat =server checked=<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem , 'Discontinued').ToString())%>> </asp:CheckBox> <asp:label ID='lblName' runat=server text=<%#DataBinder.Eval(Container.DataItem , 'ProductName').ToString() %>></asp:label> </ItemTemplate> </asp:datalist> VB.NET ’Populate the DataList in the Page_Load Event Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then If CType(e.Item.FindControl('chkDiscontinued'), CheckBox).Checked Then e.Item.BackColor = Color.CadetBlue End If End If End Sub C# //Bind the DataList in the Page_Load Event protected void ItemDB(object sender , System.Web.UI.WebControls.DataListItemEventArgs e ) { if(( e.Item.ItemType == ListItemType.Item)||( e.Item.ItemType == ListItemType.AlternatingItem) ) { if( ((CheckBox)(e.Item.FindControl('chkDiscontinued'))).Checked ) { e.Item.BackColor = Color.CadetBlue; } } }

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; } } }

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 ; }