C# 如何在asp.net中动态显示记录?
我正在创建一个小的财产网站,但需要一些显示数据库记录的帮助 我想要这样的结果:- 这是我的代码,用于显示来自2个表的静态单记录:-C# 如何在asp.net中动态显示记录?,c#,css,asp.net,sql,vb.net,C#,Css,Asp.net,Sql,Vb.net,我正在创建一个小的财产网站,但需要一些显示数据库记录的帮助 我想要这样的结果:- 这是我的代码,用于显示来自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.Da
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!";
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统数据;
公共部分类结果:System.Web.UI.Page
{
SqlConnection-con;
sqlcommandcmd,cmd1;
SqlDataReader-dr;
字符串城市1,区域1,类型1;
最小整数,最大整数;
int id=0;
int id2=0;
受保护的无效页面加载(对象发送方、事件参数e)
{
city1=Request.QueryString[“city”];
area1=请求.查询字符串[“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=newsqlconnection(“集成安全性=true;数据库=data1;服务器=sudhir pc”);
con.Open();
//cmd=new-SqlCommand(“从属性中选择价格、面积、图像名称、用户id,其中城市=”“+city1+”,面积=”“+area1+”,属性类型=”“+type1+”,用户id=”+id+”,con);
//cmd1=新的SqlCommand(“从用户中选择frstname、laststname、contactno,其中用户的\u id='“+id+””,con);
cmd=new-SqlCommand(“选择价格、面积、图像名称、用户id,可从属性获取,其中城市=@city1,面积=@area1,属性类型=@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=(字符串)dr[“price”].ToString();
Label2.Text=(字符串)dr[“area”];
字符串imgstr=(字符串)dr[“imagename”];
INTID1=(int)dr[“用户id”];
Image1.ImageUrl=“~/upload/”+imgstr+”;
Label13.Text=城市1;
Label14.Text=type1;
Label16.Text=(string)dr[“available”].ToString();
id2=(int)dr[“用户id”];
}
dr.Dispose();
cmd1=新建SqlCommand(“从用户处选择firstname、laststname、contactno,其中用户\u id=“+id2+”,con);
dr=cmd1.ExecuteReader();
while(dr.Read())
{
Label4.Text=(字符串)dr[“firstname”];
Label5.Text=(字符串)dr[“laststname”];
如果(id>0)
Label6.Text=(字符串)dr[“contactno”];
}
if(id<1)
Label6.Text=“查看联系信息。注册!”;
}
}
但是我需要动态显示多个记录,我应该使用哪个控件?对于这种要求,最好的选择是数据列表和中继器。 您可以在这里使用这两个选项中的任何一个。两者都用于显示以固定模式重复的信息。Repeater比DataList更轻,因为DataList通过创建表来创建视图,而Repeater不创建。最终,您可以选择使用它们中的任何一种。您只需在这两种格式中编辑
ItemTemplate
,然后它将根据您拥有的记录数重复自身
希望这有帮助。很高兴它帮助了你。非常感谢。