读书人

这个有关问题小弟我不知写什么标题好…

发布时间: 2012-08-25 10:06:20 作者: rapoo

这个问题我不知写什么标题好…反正是求帮忙…………!!
Table2是一个数据库表……调试时他说“myDataList”的 DataSourceID 必须是 IDataSource 类型的控件的 ID。未能找到 ID 为“Table2”的控件。”,我不懂怎么解决……求帮忙!! (还有,如果发错论坛就提示下啊)
以下是C#代码:
[code=C#][/code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace DataList控件
{
public partial class _Default : System.Web.UI.Page
{
private void Repeater0()
{
string myConnectionString = "Data Source=li-pc\\sqlexpress;Initial Catalog=测试数据库2;Integrated Security=True";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string cmdString = "select top 5 * from Table2";
SqlCommand myCommand = new SqlCommand(cmdString,myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
myDataList.DataSource = myReader;
myDataList.DataBind();
myReader.Close();
myConnection.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Repeater0();
}

protected void myDataList_ItemCommand(object source,DataListCommandEventArgs e)
{
if (e.CommandName.ToLower() == "Show")
{
Label name = (Label)e.Item.FindControl("name");
Label city = (Label)e.Item.FindControl("city");
Label address = (Label)e.Item.FindControl("address");
if (name != null && city != null && address != null)
{
myLabel1.Text = name.Text + "" + city.Text + "" + address.Text + "";
}
}
}
}
}
以下是HTML代码:
[code=HTML][/code]<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataList控件._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>DataList控件演示</h1>
<asp:DataList ID="myDataList" runat="server" BackColor="Black"
BorderWidth="1px" OnItemCommand="myDataList_ItemCommand"
DataSourceID="Table2" >



<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr><td>客户名称</td><td>所在城市</td><td>联系地址</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"name") %>'></asp:Label></td>
<td><asp:Label ID="city" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"city") %>'></asp:Label></td>


<td><asp:Label ID="address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"address") %>'></asp:Label></td>
<td><asp:Button ID="ShowInfo" runat="server" CommandName="Show" Text="选择用户" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
你选择的用户是:<asp:Label ID="myLabel1" runat="server" Text="myLabel" Width="308px"></asp:Label>
</div>
</form>
</body>
</html>


[解决办法]
DataSourceID="Table2" 这个是干什么的?问题在这里。。。

读书人网 >asp.net

热点推荐