怎么样才能读到datalist里面的数据?

zhang911 2005-07-09 03:19:31
rt,
foreach(DataListItem dli in DataList1.Items )
{
DataRowView dr = (DataRowView)dli.DataItem;
int sn = (int)dr["AtrId"];
}
读不到数据,sn = null ?!
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang911 2005-07-09
  • 打赏
  • 举报
回复
谢谢,,,,,,,
boytomato 2005-07-09
  • 打赏
  • 举报
回复
private void Button1_Click(object sender, System.EventArgs e)
{
if (DataList1.Items.Count > 0)
{
Label1.Text = "The Items collection contains: <br>";

foreach(DataListItem item in DataList1.Items)
{

Label1.Text += ((DataBoundLiteralControl)item.Controls[0]).Text +
"<br>";
}
}


}

主要是这,,你好好看看吧....

boytomato 2005-07-09
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication12
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Label Label1;

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

if (!IsPostBack)
{
DataList1.DataSource = CreateDataSource();
DataList1.DataBind();
}



}

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = "Item " + i.ToString();
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}





#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
if (DataList1.Items.Count > 0)
{
Label1.Text = "The Items collection contains: <br>";

foreach(DataListItem item in DataList1.Items)
{

Label1.Text += ((DataBoundLiteralControl)item.Controls[0]).Text +
"<br>";
}
}


}
}
}


<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication12.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataList id="DataList1" runat="server" BorderColor="black" CellPadding="3" Font-Name="Verdana"
Font-Size="8pt">
<HeaderStyle BackColor="#aaaadd"></HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<HeaderTemplate>
Items
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</ItemTemplate>
</asp:DataList>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 600px; POSITION: absolute; TOP: 80px" runat="server">Label</asp:Label>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 640px; POSITION: absolute; TOP: 168px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>

boytomato 2005-07-09
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<script runat="server">

ICollection CreateDataSource()
{

// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;

// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Description for item " + i.ToString();
dr[2] = 1.23 * (i + 1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;

}


void Page_Load(Object sender, EventArgs e)
{

// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}

}

void Item_Created(Object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

// Retrieve the Label control in the current DataListItem.
Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");

// Retrieve the text of the CurrencyColumn from the DataListItem
// and convert the value to a Double.
Double Price = Convert.ToDouble(
((DataRowView)e.Item.DataItem).Row.ItemArray[2].ToString());

// Format the value as currency and redisplay it in the DataList.
PriceLabel.Text = Price.ToString("c");

}

}

</script>

<body>

<form runat=server>

<h3>DataList ItemCreated Example</h3>

<asp:DataList id="ItemsList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
ShowBorder="True"
OnItemCreated="Item_Created"
runat="server">

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>

<HeaderTemplate>

List of items

</HeaderTemplate>

<ItemTemplate>

Description: <br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>

<br>

Price:
<asp:Label id="PriceLabel"
runat="server"/>

</ItemTemplate>

</asp:DataList>

</form>

</body>
</html>

62,047

社区成员

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

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

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

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