Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ItemDataBound using datalist

Posted on 2006-07-03
9
Medium Priority
?
1,956 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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