Solved

ItemDataBound using datalist

Posted on 2006-07-03
9
1,951 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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