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 question - dealing with values before they get posted 2

Status
Not open for further replies.

vpekulas

Programmer
Jan 8, 2002
154
CA
Hello All,

I'm wandering how I can work with a value pulled from a database before it gets flushed in to the DataList.
Here is an example:

Field fldActive in my DB is either 1 or 0, when it's 1 then I want to show it as 'Active'
and when it's 0 then I want to show it as 'Inactive'.

How can I do that before the DataList is filled ?

Here is the code I use to populate my Datalist:

Connect.ConnectionString = ConnStr
Adapter.SelectCommand = New OleDb.OleDbCommand(SQL, Connect)
Adapter.SelectCommand.Connection.Open()
Adapter.Fill(RQ_DS, "TABLE_Q")
DL_Q.DataSource = RQ_DS.Tables("TABLE_Q")
Page.DataBind()

The DataList looks like this:

<asp:DataList id=&quot;DL_Q&quot; runat=&quot;server&quot; Width=&quot;600px&quot;>
<ItemTemplate>
<table cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;600&quot; align=&quot;center&quot;><tr><td width=&quot;30&quot;>
<table cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;30&quot;><tr>
<td><IMG SRC=&quot;ico_edit.gif&quot; border=&quot;0&quot;></td>
<td><IMG SRC=&quot;ico_del.gif&quot; border=&quot;0&quot;></td>
</tr></table>
</td>
<td width=&quot;529&quot;><%# Container.DataItem(&quot;fldMEMO&quot;) %></td>
<td width=&quot;30&quot;> <%# Container.DataItem(&quot;fldACTIVE&quot;) %></td>
<td width=&quot;11&quot;><IMG SRC=&quot;ico_find.gif&quot; border=&quot;0&quot;></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
vp: I don't think vp that the DataList has the &quot;ItemDataBound&quot; event handler such as found in datagrids, et al. In that case, it is very easy to manipulate data prior to binding. The datagrid can by styled to appear very close to a DataList, etc.. and so I try to use it when I can.

One solution to your problem might be to create a temp table in code behind (for example, create a dataview) and Return that dataview as a binding source to the DataList. Then you could of course manipulate each record as it was populating your intermediate table.
 
Isadore: DataList control does have ItemDataBound event, which can be used to get a reference to binding values, while data binding. So in vpekulas' case it'll be the same as with a datagrid. First, it makes sence to set the DataKeyField of the list to the fldActive field:
Code:
<asp:DataList id=DataList1 runat=&quot;server&quot; DataKeyField=&quot;fldActive&quot;>
  <ItemTemplate>
    <asp:Label Runat=server />			
   </ItemTemplate>	
</asp:DataList>
Then in the code behind:
Code:
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
  {
    if(Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) > 0)
    {      
      Label lbl = (Label)e.Item.FindControl(&quot;lbl&quot;);
      if(Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) > 0)
      {
        lbl.Text = &quot;Active&quot;;
      }  
      else
      {
         lbl.Text = &quot;Inactive&quot;;
      }  
    }
  }
}

private void InitializeComponent()
{
  this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
  this.Load += new System.EventHandler(this.Page_Load);
}

Or even simplier, without using DataKeyField property and ItemDataBound event:
Code:
<asp:DataList id=DataList1 runat=&quot;server&quot;>	
  <ItemTemplate>
    <asp:Label Runat=server Text='<%# setText((int)DataBinder.Eval(Container.DataItem, &quot;fldActive&quot;)) %>'/>	
  </ItemTemplate>			
</asp:DataList>

in code behind:
protected string setText(int status)
{
  string buttonText = &quot;Inactive&quot;;
  if(status > 0)
  {
    buttonText = &quot;Active&quot;;
  }
  return buttonText;
}
 
Isadore, you are too kind :). This definetly wasn't worth a star.
 
Thanks guys for your help. I was able to use the following:

<%# IIF(Container.DataItem(&quot;fldACTIVE&quot;) = 1 , &quot;Active&quot; , &quot;Inactive&quot;) %>

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top