很菜的问题,关于Datalist的使用

zhanmincode 2003-12-12 09:06:45
本人很菜,刚学asp。net,datalist控件搞了半天,很不知道它怎么用,查资料好像都是html代码,很烦,我想通过图形界面的操作完成初始化,及数据绑定,不过cs的也行,就是怕html代码:),哪位仁兄能帮我找的一步一步完成datalist操作的资料啊??包含在实例中的最好,不过最好例子短一点,先谢了,很无奈啊!老板要搞bs构架的
...全文
20 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanmincode 2003-12-13
  • 打赏
  • 举报
回复
谢谢大家,特别是 hnsmxzb(飞翔鸟)。:)散分
hnsmxzb 2003-12-12
  • 打赏
  • 举报
回复
html页面
Label控件里面
<asp:Label id="Label1" runat="server"><%# DataBinder.Eval(Container.DataItem, "你的数据库字段") %></asp:Label>
hnsmxzb 2003-12-12
  • 打赏
  • 举报
回复
1.先添加个datalist控件,然后右击编辑项模版,在itemtemplate项增加个Label控件.
2.添加sqlConnection1和sqlCommand1
代码
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=MYHOME;packet size=4096;user id=sa;data source=MYHOME;persis" +
"t security info=True;initial catalog=mywebdata;password=****";
//
// sqlCommand1
//
this.sqlCommand1.CommandText = "SELECT Newslist.* FROM Newslist";
this.sqlCommand1.Connection = this.sqlConnection1;
this.Load += new System.EventHandler(this.Page_Load);

}
3.在Page_Load里
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

sqlConnection1.Open();
ds1=sqlCommand1.ExecuteReader();
DataList1.DataSource=ds1;
if (!IsPostBack)
{
DataList1.DataBind();
}
ds1.Close();
sqlConnection1.Close();
}
html页面
Label控件里面
<asp:Label id="Label1" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:Label>
foxmail 2003-12-12
  • 打赏
  • 举报
回复
<asp:DataList id="DataList1" runat="server">
<ItemTemplate> <%#Container.DataItem("id")%>
</ItemTemplate>
</asp:DataList>

数据绑定
Sub BindData
Dim myConnection As SqlConnection
myConnection=New SqlConnection("Server=LocalHost;uid=sa;pwd=;Database=pubs")
myCommand=New SqlCommand("Select id From emt Where BigType=@BigType ",myConnection)
myCommand.Parameters.Add(New SqlParameter("@BigType",SqlDbType.nVarChar,100))
myCommand.Parameters("@BigType").Value=BigType
myConnection.Open()
DataList1.DataSource=myCommand.ExecuteReader()
DataList1.DataBind()
myConnection.Close()

End Sub
沈逸 2003-12-12
  • 打赏
  • 举报
回复
右 击 编辑 想莫板
自己看看 试验 一下
很容易的

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧