无法在页面上显示数据 [英] unable to display data on the page
本文介绍了无法在页面上显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,能不能帮帮我。
我试图在代码后面绑定网格视图,但数据没有显示在页面上。我检查了它正在运行的查询,问题是显示数据。这是代码。我是网络编程的新手
< asp:GridView ID = GridView1 runat = server < span class =code-attribute> AutoGenerateColumns = False 宽度 = 100% >
< 列 &g t;
< asp:BoundField DataField = PropertyType HeaderText = < span class =code-keyword>属性类型 SortExpression = PropertyType / >
< asp:BoundField DataField = 区域 HeaderText = 区域 SortExpression = 区域 / >
< asp:BoundField DataField = 价格 HeaderText = 价格 SortExpression = 价格 / >
< asp:ImageField HeaderText = Photo DataImageUrlField = 照片 / >
< asp:BoundField DataField = < span class =code-keyword> PostedDate HeaderText = PostedDate SortExpression = 已发布日期 / >
< / Columns >
< / asp:GridView >
属性cObject = new Property();
SqlConnection con = cObject.connection();
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID =SqlDataSource1;
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = cObject.connection()。ToString();
string query =SELECT PropertyType,Area,Price,Image,PostedDate FROM TB_Property WHERE PropertyType like'+%+ propertyType +'AND Area like'+%+ area + '和'之间的价格'+ minPrice +'AND'+ maxPrice +';
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
con.Close();
解决方案
尝试声明SQLDataSource
在Web表单代码中,而不是在后面的代码中执行。
然后使用DataSourceID
属性将数据源设置为GridView。 br />
如果您以后需要修改查询,只需直接从SqlDataSource
对象更改它并调用<$ c网格上的$ c> Rebind()更新结果,无需打开/关闭任何连接。
< asp:SqlDataSource
< span class =code-attribute> id = SqlDataSource1
runat = server
DataSourceMode = DataReader
ConnectionString = < span class =code-keyword> <%
ConnectionStrings:YourConnectionString %>
SelectCommand = SELECT FirstNam e,LastName,Title FROM Employees >
< / asp:SqlDataSource >
< asp:GridView
id = GridView1
runat = server
DataSourceID = SqlDataSource1 >
your这里的列..
< / asp:GridView > 跨度>
HI。这样可以正常工作..
这可能对你有所帮助..
属性cObject = new Property();
SqlConnection con = cObject.connection();
string query = SELECT PropertyType,Area,Price,Image,PostedDate FROM TB_Property WHERE PropertyType like' + % + propertyType + 'AND Area like' + % + area + '和价格'' + minPrice + 'AND' + maxPrice + ';
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
DataTable dt = new DataTable();
new SqlDataAdapter(cmd).Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
Hello, can you please help me.
I am trying to bind the grid view in code behind, but the data is not displayed on the page. I checked the query it is working, the problem is displaying the data. here is the code. I am new to web programming
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField DataField="PropertyType" HeaderText="Property Type" SortExpression="PropertyType" />
<asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:ImageField HeaderText="Photo" DataImageUrlField="Photo" />
<asp:BoundField DataField="PostedDate" HeaderText="PostedDate" SortExpression="PostedDate" />
</Columns>
</asp:GridView>
Property cObject = new Property(); SqlConnection con = cObject.connection(); SqlDataSource SqlDataSource1 = new SqlDataSource(); SqlDataSource1.ID = "SqlDataSource1"; this.Page.Controls.Add(SqlDataSource1); SqlDataSource1.ConnectionString = cObject.connection().ToString(); string query = "SELECT PropertyType, Area, Price, Image, PostedDate FROM TB_Property WHERE PropertyType like '" + "%" + propertyType + "' AND Area like '" + "%" + area + "' AND Price between'" + minPrice + "' AND '" + maxPrice + "'"; SqlCommand cmd = new SqlCommand(query, con); con.Open(); SqlDataReader dReader = cmd.ExecuteReader(); GridView1.DataSource = SqlDataSource1; GridView1.DataBind(); con.Close();
解决方案
try declaring theSQLDataSource
in the web form code, instead of doing it in the code behind.
then set the datasource to the GridView using theDataSourceID
property.
if you need to later modify the query, just change it directly from theSqlDataSource
object and callRebind()
on the grid to update results, no need for you to open/close any connections.
<asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataReader" ConnectionString="<%
ConnectionStrings:YourConnectionString%>" SelectCommand="SELECT FirstName, LastName, Title FROM Employees"> </asp:SqlDataSource> <asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1"> your columns here.. </asp:GridView>
HI. this will work fine..
This might help you..
Property cObject = new Property(); SqlConnection con = cObject.connection(); string query = "SELECT PropertyType, Area, Price, Image, PostedDate FROM TB_Property WHERE PropertyType like '" + "%" + propertyType + "' AND Area like '" + "%" + area + "' AND Price between'" + minPrice + "' AND '" + maxPrice + "'"; SqlCommand cmd = new SqlCommand(query, con); con.Open(); DataTable dt = new DataTable(); new SqlDataAdapter( cmd).Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); con.Close();
这篇关于无法在页面上显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文