Solved

ItemDataBound using datalist

Posted on 2006-07-03
9
1,952 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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