Solved

ItemDataBound using datalist

Posted on 2006-07-03
9
1,946 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now