Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datalist item conditional formating 1

Status
Not open for further replies.

oakgrove

Programmer
Sep 22, 2002
51
US
I have an sql server table of items which have expiration dates.
I have a datareader 'dtrlist' filled by an sql sever select .executereader command.
I have a datalist 'dlstlist' defined as:
dlstlist.datasource = datalist
dlstlist.DataKeyField = 'listingid' (a unique column from the sql table)
dlstlist.databind()

The datalist has a template with a column that shows the listingid, the item name, the expire date and a button labled 'Extend'. Clicking on this button will add time to the expire date. If the expire date is in the future the button is disabled other wise it needs to be enabled.

I need to know how to do the following.
When the page is rendered it needs to test each expire date against the system date.
If the item has expired the 'Extend' button will become enabled.

Where do I put the if...then and how do I reference each of the expire dates and extend buttons.

Thanks for any help.



Oakgrove Computer Grouper
Lansing, MI
 
Code:
page html:
<asp:DataList Runat=server ID="myDataList">
 <ItemTemplate>
   <asp:Button Runat=server Text="Extend" Enabled='<%# enableButton(Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "ExpirationDate"))) %>' />
 </ItemTemplate>
</asp:DataList>

code behind:
protected bool enableButton(DateTime expDate)
{
  bool isEnabled = true;
  if(expDate < DateTime.Today) isEnabled = false;

  return isEnabled;
}
This should do the trick. Rename "ExpirationDate" to the name of your recordset item.
 
Probably the more standard way of doing it is to examine the data in the ItemDataBound event handler and setting the properties there.
 
Well, I think the "standart" is just a matter of personal preference. But it sure can be done in the ItemDataBound handler with exactly the same output.
Code:
page html:
<asp:DataList Runat=server ID="myDataList">		
  <ItemTemplate>
    <asp:Button Runat=server ID="myButton" Text="Extend" />
  </ItemTemplate>
</asp:DataList>

code behind:
private void myDataList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
  Button b = (Button)e.Item.FindControl("myButton");
  DateTime dt = (DateTime)((System.Data.Common.DbDataRecord)e.Item.DataItem)["ExpirationDate"];
  if(dt < DateTime.Today) b.Enabled = false;
}
 
Well, I think the "standart" is just a matter of personal preference."

Not if you ask Microsoft! [lol]

I know what you're saying though.

I think the advantage of the ItemDataBound approach is that it takes server-side code out of the markup, which better separates the business logic from the page. This can arguably make things easier to maintain and debug, though the alternate approach gets the job done elegantly, too.
 
Agree. Separation of html and code behind - can't beat that.
 
Thanks much. You guys are great.

Oakgrove Computer Grouper
Lansing, MI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top