Solved

ItemDataBound using datalist

Posted on 2006-07-03
9
1,950 Views
Last Modified: 2008-02-01
Hello Experts,

I have the following, I am trying to bind a bit of text to a label in my datalist;-

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
      Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
      lblRegDate.Text = "sdhkgsdhkf";
    }

This doesn't work, the Label lblReg does not take any values.

What do I need to do to get this to work.

Many Thanks.

Also, how would I calculate the differnce between two years..

I have a date in format 12/01/1999 and 31/03.2006. I need to find the differnce beween the two in years and display it in my lblRegDate. I have the following, but it doesn't work...

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
      DateTime regDate = DateTime.Parse(((DataRowView)e.Item.DataItem)["DateOfRegistration"].ToString());
      DateTime nowDate = DateTime.Now;
      double dateResult;

      TimeSpan tp = nowDate.Subtract(regDate);

      Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
      dateResult = tp.TotalDays / 365;
      lblRegDate.Text = dateResult.ToString();


      ((Label)e.Item.FindControl("lblRegDate")).Text = Math.Abs(DateAndTime.DateDiff(DateInterval.Year, nowDate, regDate, Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System)).ToString();

    }

 Plus it uses VB.NET which I don't really like, how do I achieve this strictly in c#.

Many Thanks.
0
Comment
Question by:claracruz
  • 5
  • 4
9 Comments
 
LVL 1

Expert Comment

by:DerivcoIT
ID: 17036883
Hi there

In answer to your first question - if you have headers or footers in your datalist, then it will fail - because your header and footer do not have your label in it.

Try the following:

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
      if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
      {
      Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
      lblRegDate.Text = "sdhkgsdhkf";
      }
    }

To answer your second prolem - you can't divide a value type of double by a value type of integer - which is exactly what you're trying to do when you write:


dateResult = tp.TotalDays / 365;

Try the following:

dateResult = tp.TotalDays / Convert.ToDouble(365);

Hope this helps
0
 
LVL 4

Author Comment

by:claracruz
ID: 17037161
iI am still not getting the value passed to  lblRegDate.Text ...

No error message either, what could be the problem
0
 
LVL 1

Expert Comment

by:DerivcoIT
ID: 17037286
I can't seem to see in your code where you're trying to get data from the label text -all of them are setting vales.

Is the new value not being set?

Otherwise - if you're trying to get the value out you'd need something like:

string strLabelValue = ((Label)e.Item.FindControl("lblRegDate")).Text;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 4

Author Comment

by:claracruz
ID: 17037992
Sorry, I meant, that the data

lblRegDate.Text = "sdhkgsdhkf";


 isn't being displayed in the label control
0
 
LVL 1

Expert Comment

by:DerivcoIT
ID: 17040508
Are you calling a rebind anywhere? Or a calling the grid bind if the page_load without wrapping it in a if(!IsPostBack)?
0
 
LVL 4

Author Comment

by:claracruz
ID: 17041283
how do I call a rebind, all  I have is this bit of code;-

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
            lblRegDate.Text = "sdhkgsdhkf";
        }
    }
0
 
LVL 1

Accepted Solution

by:
DerivcoIT earned 500 total points
ID: 17041430
By rebind I mean the method you call to bind data to the grid
0
 
LVL 1

Expert Comment

by:DerivcoIT
ID: 17041435
Might be better is you posted the code for the whole page - that way i can see whats going on from top to bottom, and determine what the problem could be
0
 
LVL 4

Author Comment

by:claracruz
ID: 17042643
yep you are right
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question