【问题标题】:Datalist control : binding with ADO.NET objectsDatalist 控件:与 ADO.NET 对象绑定
【发布时间】:2011-09-29 13:27:20
【问题描述】:

代码如下:

private void ShowPossiblePurchases(string CategoryName)
{

    string selectSQL = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
    SqlCommand cmd = new SqlCommand(selectSQL, connection);
    cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
    SqlDataReader reader;

    DataSet myDataSet = new DataSet();
    myDataSet.Tables.Add("Products");


    myDataSet.Tables["Products"].Columns.Add("ProductID");
    myDataSet.Tables["Products"].Columns.Add("CategoryID");
    myDataSet.Tables["Products"].Columns.Add("ProductName");
    myDataSet.Tables["Products"].Columns.Add("Price");

    DataList DataList1 = (DataList)lgnView.FindControl("DataList1");

    try
    {
        connection.Open();
        reader = cmd.ExecuteReader();

        while (reader.Read())
        {

            DataRow rowNew = myDataSet.Tables["Products"].NewRow();
            rowNew["ProductID"] = reader["ProductID"];
            rowNew["CategoryID"] = reader["CategoryID"];
            rowNew["ProductName"] = reader["ProductName"];
            rowNew["Price"] = reader["Price"];
            myDataSet.Tables["Products"].Rows.Add(rowNew);
        }

        DataList1.DataSource = myDataSet.Tables["Products"];
        DataList1.DataBind();
    }
    catch(Exception ex)
    {
        Label lblError = (Label)lgnView.FindControl("lblError");
        lblError.Text = ex.Message;
    }
    finally
    {
        connection.Close();
    }
}

当我运行它时,什么也没有发生。我做错了什么?

【问题讨论】:

  • 返回 'false' 而不是检查任何异常实际上告诉你什么?
  • 为什么不在 catch 块中指定任何异常?这将有助于确定发生了什么
  • 尝试添加 DataList1.DataMember = "Products" 以防万一
  • @mslliviu:我已经试过了,没有效果。还是谢谢你。

标签: c# asp.net data-binding


【解决方案1】:

我已经重新创建了上面的代码,并且运行良好。 因为您没有提供 DataList 的来源,所以我坚信问题在于您没有正确绑定 DataList 中的数据。

这是一个如何做到这一点的示例:

<asp:DataList ID="DataList1" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <td>
                    ProductID
                </td>
                <td>
                    CategoryID
                </td>
                <td>
                    ProductName
                </td>
                <td>
                    Price
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table>
            <tr>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "ProductID")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "CategoryID")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "ProductName")%>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "Price")%>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

这里还有MSDN DataList 文章。在这里你可以找到一个很好的例子来说明如何将数据绑定到DataList。

只是一个注释 您尝试绑定到 DataaList 的任何具体原因? 您问题中的代码可以很容易地绑定到这样的网格视图:

Web.config

<connectionStrings>
    <add name="conn" connectionString="your connection string"/>
</connectionStrings>

来源

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

背后的代码

private void ShowPossiblePurchases(string categoryName)
{
    if (!String.IsNullOrEmpty(categoryName))
    {
        try
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            SqlConnection connection = new SqlConnection(connString);
            string commandText = "SELECT TOP 2 * FROM Menu WHERE CategoryName=@CategoryName ORDER BY NEWID()";
            using (connection)
            {
                using (SqlCommand command = new SqlCommand(commandText, connection))
                {
                    command.Parameters.AddWithValue("@CategoryName", categoryName);
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
            connection.Close();
        }
        catch (Exception ex)
        {
            Label lblError = (Label)Page.FindControl("lblError");
            lblError.Text = ex.Message;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多