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

本文关键字:动态显示 记录 net asp | 更新日期: 2023-09-27 18:09:44

我正在创建一个小物业网站,但需要一些帮助从数据库显示记录。

我想要这样的结果:-

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 % 2 cr2m& isvoicesearch = N& keyword_suggest = % 20孟买港口% 3 b&类= O % 2 cb % 2 ca& strEntityMap = W3sidHlwZSI6ImNpdHkifSx7IjEiOlsibXVtYmFpIGhhcmJvdXIiLCJDSVRZXzEzLCBQUkVGRVJFTkNFX1MsIFJFU0NPTV9SIl19XQ % 3 d % 3 d& texttypedtillsuggestion = mumbai& refine_results = Y& Refine_Localities =细化% 20 localities& action = % 2 fdo % 2 fquicksearch % 2 fsearch&建议= CITY_13 % 2 c % 20 preference_s % 2 c % 20 rescom_r& property_type = 1% 2 c4 % 2 c2 % 2 c3 % 2人私下偷偷收藏盒式% 2 c5 % 2 c22 % 2 c80

这是我的代码显示静态单一记录从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!";
    }
}

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

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

满足这种需求的最佳选择是DataList和Repeater。

您可以在这里使用其中任何一个。两者都用于显示以固定模式重复的信息。Repeater比DataList更轻,因为DataList通过创建表来创建视图,而Repeater则不是。最终,您可以选择使用它们中的任何一个。你只需要在两者中编辑ItemTemplate,然后它会根据你拥有的记录数量重复自己。