如何将Sql存储的数据显示到Datalist中 [英] How Can I Display Sql Stored Data Into Datalist

查看:266
本文介绍了如何将Sql存储的数据显示到Datalist中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有

在我的第一页中,我将值(名称,城市和图像)插入到sql数据库中,第二页从数据库中读取所有这些值并显示它,

但我的datalist不会显示数据。任何人都可以告诉我datalist如何显示数据库中的数据。我在数据专家中使用了两个标签和一个图像控件

default.aspx页面:

< html xmlns =http://www.w3.org/1999/ xhtml>

< head runat =server>

< title>< / title>

< / head>

< body>

< form id =form1runat =server>



  名称:

< asp:TextBox ID =TextBox1runat =server>




 城市:   

< asp:TextBox ID =TextBox2runat =server>




 照片:

< asp:FileUpload ID =FileUpload1runat =server/>




   ;             

< asp:Label ID =Label1runat =server ForeColor =红色

文字=请插入你的图片Visible =False>




   ;          

< asp:Button ID =Button1runat =serveronclick =Button1_ClickText =保存

宽度=125px/>

   






< / form>

< / body>

< / html>



Default.aspx.cs页面:

public partial class _Default:System.Web.UI.Page

{

SqlConnection con = new SqlConnection(Data Source = NITIN; Initial Catalog = test; Integrated Security = True);

protected void Page_Load(object sender,EventArgs e)

{
}

protected void Button1_Click(object sender,EventArgs e)

{

if(FileUpload1.FileName == )

{

Label1.Visible = true;

}

其他

{

string fpath =〜\\Upload\\+ FileUpload1.FileName;

FileUpload1.PostedFile.SaveAs(Server.MapPath(fpath) ));

string query =插入fst值('+ TextBox1.Text +','+ TextBox2.Text +','+ fpath +');

SqlDataAdapter da = new SqlDataAdapter(query,con);

DataSet ds = new DataSet();

da.Fill(ds);

Response.Write(值已插入);

DisplayNextPage();

}

}

public void DisplayNextPage()

{

con.Open();

SqlCommand cmd = new SqlCommand(select *来自fst,con);

SqlDataReader dr = cmd.ExecuteReader();

while(dr.Read())

{

if(TextBox1.Text == dr [0] .ToString())

{

Session [name] = dr [0] .ToString();

Session [city] = dr [1] .ToString();

Session [img] = dr [2] .ToString();

响应。重定向(Default2.aspx);

}

}

con.Close();

}

}



default2.aspx页

< html xmlns =http://www.w3 .org / 1999 / xhtml>

< head runat =server>

< title>< / title>

< / head>

< body>

< form id =form1runat =server>



欢迎

< asp:标签ID =Label1runat =serverText =Label>

 


你的城市是

< asp:标签ID =Label2runat =serverText =Label>







< asp:图片ID =Image1runat =server身高=121px宽度=108px />







数据绑定控制


<如p:DataList ID =DataList1runat =serverWidth =280px>

< itemtemplate>

< asp:Label ID =Label3 runat =serverText =Label>

       

< asp:Label ID = Label4runat =serverText =Label>

       

< asp:Image ID =Image2runat =serverHeight =70pxWidth =54px/>
















< / form>

< / body>

< / html>



default2.aspx.cs

public partial class Default2:System.Web.UI.Page

{

SqlConnection con = new SqlConnection(Data Source = NITIN; Initial Catalog = test; Integrated Security =真的);



protected void Page_Load(object sender,EventArgs e)

{

Label1.Text = Session [name]。ToString();

Label2.Text = Session [city ] .ToString();

Image1.ImageUrl = Session [img]。ToString();

display();

}

public void display()

{

SqlDataAdapter da = new SqlDataAdapter(select * from fst,con);

DataSet ds = new DataSet();

da.Fill(ds);

DataList1.DataSource = ds;

DataList1.DataBind();

}

}



我的Sql数据查询

创建数据库测试

使用测试

创建表格fst(sname varchar(50),城市varchar(50),snap varchar(100))

select * from fst

解决方案

我认为您需要在代码中的html部分使用Eval函数来将datalist与您的数据绑定。使用类似下面的内容





 <   asp:label     id   =  Label3    runat   =  server    text   =  <% #Eval(  ColumnName)%> ;    xmlns:asp   = #unknown >  


&升t; / asp:label >


你需要为你的datalist定义itemtemplate然后绑定数据。



这里有一些参考的样本。



http:// www.webcodeexpert.com/2013/06/how-to-bind-datalist-using.html#.U1efpOmKDcs [ ^ ]



http://msdn.microsoft.com/en-us/library/aa719635(v = vs.71).aspx [ ^ ]


没有错误
i已解决它先生正确的代码是
< asp:标签 ID = Label3 runat = server 文字 =' <% #Eval( 名称%> ' > < / asp:标签 >
< asp:标签 ID = Label4 runat = server 文字 < span class =code-keyword> =' <% #Eval( city%> ' < span class =code-keyword>> < / asp:Label < span class =code-keyword>>
< asp:Image ID = Image2 runat = server ImageUrl =' <% #Eval( snap%> ' 高度 = 70px 宽度 = 54px / >
您的建议对我有很大帮助,谢谢Sir先生


Dear all
In my first page i am inserting values (name,city and image) into sql database , and second page fatch all those values from database and display it ,
but my datalist will not display the data . can anybody tell me how can datalist show data from database. I have use two label and one image control into datalist
default.aspx page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">


  Name:
<asp:TextBox ID="TextBox1" runat="server">


  City:   
<asp:TextBox ID="TextBox2" runat="server">


  Photo:
<asp:FileUpload ID="FileUpload1" runat="server" />


               
<asp:Label ID="Label1" runat="server" ForeColor="Red"
Text="Please insert your Image" Visible="False">


            
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save"
Width="125px" />
   



</form>
</body>
</html>

Default.aspx.cs page:
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=NITIN;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.FileName == "")
{
Label1.Visible = true;
}
else
{
string fpath = "~\\Upload\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(fpath));
string query = "insert into fst values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + fpath + "')";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
Response.Write("Values Inserted");
DisplayNextPage();
}
}
public void DisplayNextPage()
{
con.Open();
SqlCommand cmd = new SqlCommand("select*from fst", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (TextBox1.Text == dr[0].ToString())
{
Session["name"] = dr[0].ToString();
Session["city"] = dr[1].ToString();
Session["img"] = dr[2].ToString();
Response.Redirect("Default2.aspx");
}
}
con.Close();
}
}

default2.aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">


Welcome
<asp:Label ID="Label1" runat="server" Text="Label">
 

Your City is
<asp:Label ID="Label2" runat="server" Text="Label">




<asp:Image ID="Image1" runat="server" Height="121px" Width="108px" />




Data Bound Control

<asp:DataList ID="DataList1" runat="server" Width="280px">
<itemtemplate>
<asp:Label ID="Label3" runat="server" Text="Label">
       
<asp:Label ID="Label4" runat="server" Text="Label">
       
<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" />









</form>
</body>
</html>

default2.aspx.cs
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=NITIN;Initial Catalog=test;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
Label2.Text = Session["city"].ToString();
Image1.ImageUrl = Session["img"].ToString();
display();
}
public void display()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from fst", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}

My Sql data query
create database test
use test
create table fst (sname varchar(50),city varchar(50),snap varchar(100))
select*from fst

解决方案

i think you need to use Eval function on the html part in your code to bind datalist with your data. use something like below


<asp:label id="Label3" runat="server" text="<%#Eval("ColumnName")%>" xmlns:asp="#unknown">


</asp:label>


you need to define itemtemplate for your datalist and then bind the data.

Here are some sampels for reference.

http://www.webcodeexpert.com/2013/06/how-to-bind-datalist-using.html#.U1efpOmKDcs[^]

http://msdn.microsoft.com/en-us/library/aa719635(v=vs.71).aspx[^]


no error
i have solve it Sir the correct code is
<asp:Label ID="Label3" runat="server" Text='<%# Eval("name") %>' ></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("city") %>'></asp:Label>
<asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("snap") %>' Height="70px" Width="54px" />
Your advice help me a lot , thank you Sir


这篇关于如何将Sql存储的数据显示到Datalist中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆