protected object GetShipNameByCustomerID(object CustomerID) { object result = null; using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind")) { SqlDataAdapter sda = new SqlDataAdapter("select top 1 ShipName from Orders where CustomerID=@CustomerID", con); sda.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", CustomerID)); DataSet ds = new DataSet(); sda.Fill(ds, "ShipName"); if (ds.Tables.Contains("ShipName") && ds.Tables["ShipName"].Rows.Count > 0) { result = ds.Tables["ShipName"].Rows[0][0]; } } return result; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind")) { SqlDataAdapter sda = new SqlDataAdapter("select top 10 * from Customers", con); DataSet ds = new DataSet(); sda.Fill(ds, "Customers"); if (ds.Tables.Contains("Customers")) { dl.DataSource = ds.Tables["Customers"].DefaultView; dl.DataBind(); } } } }
在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//如果要绑定的项是交替项或者是普通项
//注意此外还有脚模板和脚模版
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//e.Item表示当前绑定的那一行
//利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件
Label lbSex = (Label)(e.Item.FindControl("Label1"));
//利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件
DataList dl2 = (DataList)(e.Item.FindControl("DataList2"));
bool male = bool.Parse(lbSex.Text);
dl2.DataSource = GetDataTable(male);
dl2.DataBind();
}
}
DataList有Items属性,这个属性表示获取表示控件内单独项的 DataListItem 对象的集合。
{
if (DataList1.Items.Count > 0)
{
Label1.Text = "The Items collection contains: <br />"; foreach(DataListItem item in DataList1.Items)//对DataList的项进行遍历
{ Label1.Text += ((DataBoundLiteralControl)item.Controls[0]).Text +
"<br />";
}
}
}
在datalist的项模板里添加了一个表格
表格为一行两列
第一列是绑定数据
第二列的数据是根据第一列的值去查找一个表的某列值
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList ID="dl" runat="server">
<AlternatingItemTemplate>
<div>Customers || CustomerID: <%#Eval("CustomerID")%> CompanyName: <%#Eval("CompanyName") %>
</div>
<div>
Orders || ShipName: <%# GetShipNameByCustomerID(Eval("CustomerID"))%>
</div>
</AlternatingItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
<ItemTemplate>
<div>Customers || CustomerID: <%#Eval("CustomerID")%> CompanyName: <%#Eval("CompanyName") %>
</div>
<div>
Orders || ShipName: <%# GetShipNameByCustomerID(Eval("CustomerID"))%>
</div>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
protected object GetShipNameByCustomerID(object CustomerID)
{
object result = null;
using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind"))
{
SqlDataAdapter sda = new SqlDataAdapter("select top 1 ShipName from Orders where CustomerID=@CustomerID", con);
sda.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", CustomerID)); DataSet ds = new DataSet();
sda.Fill(ds, "ShipName");
if (ds.Tables.Contains("ShipName") && ds.Tables["ShipName"].Rows.Count > 0)
{
result = ds.Tables["ShipName"].Rows[0][0];
}
}
return result;
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=root;database=Northwind"))
{
SqlDataAdapter sda = new SqlDataAdapter("select top 10 * from Customers", con);
DataSet ds = new DataSet();
sda.Fill(ds, "Customers");
if (ds.Tables.Contains("Customers"))
{
dl.DataSource = ds.Tables["Customers"].DefaultView;
dl.DataBind();
}
} }
}
BorderStyle="Double" BorderWidth="1px">
<ItemTemplate>
<table>
<tr>
<td style="width: 100px; height: 244px" valign="top">
<table>
<tr>
<td style="width: 100px" valign="top">
<div class="lt-zl_2">
<asp:Image ID="Image1" runat="server" Height="78px" Width="80px" /> <br />
<asp:LinkButton ID="lkbtnRname" runat="server" ForeColor="Black" Text='<%# Eval("reply_username") %>'></asp:LinkButton><br />
<asp:Label ID="lbRurl" runat="server" Text='<%# GetShipNameByCustomerID(Eval("reply_id")) %>' Visible="false"></asp:Label>
<br />
<asp:Label ID="lbid" runat="server" Text='<%# Eval("reply_id") %>'></asp:Label></div>
</td>
</tr>
</table>
</td>
<td style="width: 100px; height: 244px" valign="top">
<div class="lt-zr_2">
<%--<%# GetShipNameByCustomerID(Eval("reply_id")) %>--%>
<%
string Rurl = Convert.ToString(Eval("reply_content"));
Response.ContentType = "text/HTML";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.WriteFile(Rurl);
Response.Write(Rurl);
%>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
我想在<%
string Rurl = Convert.ToString(Eval("reply_content"));
Response.ContentType = "text/HTML";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.WriteFile(Rurl);
Response.Write(Rurl);
%>
这里输出文件 但是string Rurl = Convert.ToString(Eval("reply_content"));要出错
那应该怎么做啊。。?
输出文件到datalist的指定位置
但问题是每一行都要输出不同的文件 该文件是根据每一行绑定的一个id来输出不同的文件那位 帮帮忙..
输出文件到datalist的指定位置
我只能在每一行输出相同的文件我想 在每一行都要输出不同的文件 该文件是根据每一行绑定的一个id来输出不同的文件那位 帮帮忙..
前台用javascript遍历 datalist 吧
o(∩_∩)o...哈哈
<%--<script type="text/javascript">
//获取GridView2中TextBox的值
onload = function ForeachGridView()
{
var gridview = document.getElementById("<%=GridView2.ClientID %>");
for(int i=0;i<girdview.rows.length;i++)
{
var sortID = gridview.rows[i].cell[0].getElementsByTagName("CP_SortID")[4];
alert(sortID.value);
}
}
</script>--%>
...