绑定DataList的步骤
经常用到的绑定方法,但是每次都会忘掉。所以记录一下。
前台的绑定:
普通的绑定:<%#DataBinder.Eval(Container.DataItem,"zph_region")%>
有格式的绑定:<%#DataBinder.Eval(Container.DataItem,"release_time","{0:yyyy-MM-dd}")%>
后台的绑定:
private void zphList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//招聘会连接
HyperLink linkTitle = (HyperLink)e.Item.FindControl("linkTitle");
//必须设置DataKeyField
DataRowView zphDataRowView = (DataRowView)e.Item.DataItem;
//或者用Convert.ToString(DataBinder.Eval(e.Item.DataItem , "Sitename"))取
//招聘会连接
string strTitle = zphDataRowView[ ZphData.ZPH_TITLE_FIELD ].ToString();
if ( strTitle.Length > ZPH_TITLE_LENGTH )
{
linkTitle.Text = strTitle.Substring( 0 , ZPH_TITLE_LENGTH );
}
else
{
linkTitle.Text = strTitle;
}
linkTitle.NavigateUrl = PageBase.UrlBase + "/ZphManager/PreViewZph.aspx?zph_code=" + zphDataRowView[ ZphData.ZPH_CODE_FIELD ].ToString();
}
}
本文地址:http://www.45fan.com/a/question/70214.html