如何在asp.net中动态显示记录?

时间:2014-06-21 06:16:05

标签: c# css asp.net sql vb.net

我正在创建小型房产网站,但在显示数据库记录时需要一些帮助。

我想要这样的结果: -

http://www.99acres.com/property-in-mumbai-harbour-15-lakhs-to-20-lakhs-ffid?search_type=QS&search_location=HP&lstAcn=HP_R&src=CLUSTER&np_search_type=NP%2CR2M&isvoicesearch=N&keyword_suggest=mumbai%20harbour%3B&class=O%2CB%2CA&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsibXVtYmFpIGhhcmJvdXIiLCJDSVRZXzEzLCBQUkVGRVJFTkNFX1MsIFJFU0NPTV9SIl19XQ%3D%3D&texttypedtillsuggestion=mumbai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_13%2C%20PREFERENCE_S%2C%20RESCOM_R&property_type=1%2C4%2C2%2C3%2C90%2C5%2C22%2C80

这是我从2个表中显示静态单个记录的代码: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Result : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd,cmd1;
    SqlDataReader dr;

    string city1, area1, type1;
    int min, max;
    int id = 0;
    int id2 = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        city1 = Request.QueryString["city"];
        area1 = Request.QueryString["area"];
        type1 = Request.QueryString["propertytype"];
       // type1= "1bhk";
        min = Convert.ToInt32(Request.QueryString["minprice"]);
        max = Convert.ToInt32(Request.QueryString["maxprice"]);
        id = Convert.ToInt32(Request.QueryString["uid"]);
       // id = 1;
        con = new SqlConnection("integrated security=true; database=data1; server=sudhir-pc");
        con.Open();
        //cmd = new SqlCommand("select price,area,imagename,users_id from property where city='" + city1 + "' and area='" + area1 + "' and propertytype='" + type1 + "' and users_id=" + id + "", con);
        //  cmd1 = new SqlCommand("select frstname,laststname,contactno from users where users_id='"+id+"'", con);
        cmd = new SqlCommand("select price,area,imagename,users_id,available from property where city=@city1 and area=@area1 and propertytype=@type1", con);
        cmd.Parameters.AddWithValue("@city1",city1);
        cmd.Parameters.AddWithValue("@area1",area1);
        cmd.Parameters.AddWithValue("@type1",type1);

      //  cmd.Parameters.AddWithValue("@id",id);
        dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            Label1.Text = (string)dr["price"].ToString();
            Label2.Text= (string)dr["area"];
            string imgstr=(string)dr["imagename"];
            int id1 = (int)dr["users_id"];
            Image1.ImageUrl = "~/upload/"+imgstr+"";
            Label13.Text = city1;
            Label14.Text = type1;
            Label16.Text = (string)dr["available"].ToString();

            id2 = (int)dr["users_id"];
        }
        dr.Dispose();
        cmd1 = new SqlCommand("select firstname,laststname,contactno from users where users_id=" + id2 + "", con);
        dr = cmd1.ExecuteReader();
        while (dr.Read())
        {
            Label4.Text = (string)dr["firstname"];
            Label5.Text = (string)dr["laststname"];

            if (id > 0)
                Label6.Text = (string)dr["contactno"];


        }
        if (id < 1)
            Label6.Text = "To see contact information. Register!";
    }
}

但是我需要动态显示多个记录,我应该使用哪种控件?

1 个答案:

答案 0 :(得分:1)

此类要求的最佳选择是DataList和Repeater。

您可以在此处使用其中任何一个。两者都用于显示以固定模式重复的信息。 Repeater比DataList更轻,因为Datalist通过创建表来创建视图,而Repeater则不然。最终,您可以选择使用其中任何一种。您只需编辑两者中的ItemTemplate,然后它将重复您自己的记录数。

希望这有帮助。