BINDING后对ASP Datalist进行排序

时间:2012-08-13 09:20:31

标签: datalist

我正在使用动态值绑定DataList(即距离特定位置的谷歌api的距离。)

即从x位置:

10公里以外 15公里以外如下

enter image description here

ItemDataBound

中使用此代码
private void bindDataList(string location)
{
  DataSet dstProperty = Tbl_PropertyMaster.getPropertiesByLocation(location);
  dlstNearbyProperties.DataSource = dstProperty;
  dlstNearbyProperties.DataBind();
}

protected void dlstNearbyProperties_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label lblPropId = (Label)e.Item.FindControl("lblPropId");
        Label lblKmAway = (Label)e.Item.FindControl("lblKmAway");
        Label lblPrice = (Label)e.Item.FindControl("lblPrice");
        DataSet dstEnabledStat = Tbl_PropertyMaster.GetPropertyDetailsbyId(Convert.ToInt32(lblPropId.Text));
        if (dstEnabledStat.Tables[0].Rows.Count > 0)
        {
            //string origin = "8.5572357 ,76.87649310000006";
            string origin = InitialOrigin;
            string destination = dstEnabledStat.Tables[0].Rows[0]["Latitude"].ToString() + "," + dstEnabledStat.Tables[0].Rows[0]["Longitude"].ToString();
            lblKmAway.Text = devTools.getDistance(origin, destination) + " Away";
        }
        lblPrice.Text = getMinnimumOfRoomPrice(Convert.ToInt32(lblPropId.Text));
   }
}

有没有办法在提升或降低w.r.t距离时对这些值进行排序。

注意:距离不是数据库值,而是动态的。

可以在Button1_Click中进行排序吗?

1 个答案:

答案 0 :(得分:0)

Alrite经过很长时间玩代码后我就做了。

以下内容适用于GRIDVIEW,DataList也可以遵循类似的步骤。

页面加载:我在已存在的数据表中添加了额外的列“Miles”

protected void Page_Load(object sender, EventArgs e)
{
    dtbl = Tbl_PropertyMaster.SelectAllPropertyAndUserDetails().Tables[0];
    dtbl.Columns.Add("Miles", typeof(int));
    //userId = devTools.checkAdminLoginStatus();
    if (!IsPostBack)
    {
        fillDlPhotoViewAll();
        FillGrProperty();

    }
}

行数据绑定:

protected void grProperty_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

    DataSet dstEnabledStat = Tbl_PropertyMaster.GetPropertyDetailsbyId(PropId);
    if (dstEnabledStat.Tables[0].Rows.Count > 0)
    {
        string origin = InitialOrigin;
            string destination = dstEnabledStat.Tables[0].Rows[0]["Latitude"].ToString() + "," + dstEnabledStat.Tables[0].Rows[0]["Longitude"].ToString();
            decimal Kilometre=0.00M;
            if(devTools.getDistance(origin, destination)!=0)
            {
            Kilometre=Convert.ToDecimal(devTools.getDistance(origin, destination))/1000;
            }
            lblmiles.Text = Kilometre.ToString() + "Kms";
            dtbl.Rows[inn]["Miles"] = Convert.ToInt32(devTools.getDistance(origin, destination));
            inn = inn + 1;
    }
}
ViewState["dtbl"] = dtbl;
}

按距离排序Button_Click:

protected void btnSort_Click(object sender, EventArgs e)
{
    DataTable dataTable;
    dataTable = (DataTable)ViewState["dtbl"];
    if (dataTable.Rows.Count > 0)
    {
        dataTable.DefaultView.Sort = "Miles DESC";
        dataTable.AcceptChanges();
        grProperty.DataSource = dataTable;
        grProperty.DataBind();
    }
}