Listview数据绑定混淆

时间:2012-08-14 17:04:39

标签: c# listview data-binding stored-procedures

我当前有一个正在运行的存储过程,希望用我的列表视图绑定sp中的数据。但是,我不确定如何继续这样做。

这是我目前的代码。我认为它类似于数据绑定gridview,但却迷失了它。

HTML

<asp:ListView runat="server">
                        <LayoutTemplate>
                            <table>
                                <tr style="background-color:green">
                                    <th><asp:LinkButton ID="lnkid" runat="server">Role ID</asp:LinkButton></th>
                                    <th><asp:LinkButton ID="lnkdesc" runat="server">Role Description</asp:LinkButton></th>
                                </tr>
                            </table>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <tr>
                                <td><asp:Label runat="server" ID="lblroleid">Role ID</asp:Label></td>
                                <td><asp:Label runat="server" ID="lblroledesc">Role Desc></asp:Label></td>
                            </tr>
                        </ItemTemplate>
                        <AlternatingItemTemplate>
                            <tr style="background-color:Aqua">
                                <td><asp:Label runat="server" ID="lblroleid">Role ID</asp:Label></td>
                                <td><asp:Label runat="server" ID="lblroledesc">Role Desc</asp:Label></td>
                            </tr>
                        </AlternatingItemTemplate>
                    </asp:ListView>

C#

protected void roles()
    {
        txtSearch.Focus();
        string[] queryvalue = txtSearch.Text.Split(' ');
        SqlConnection myconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Rollup2ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "USP_GET_USER_ROLES";
        cmd.Connection = myconn;
        cmd.Parameters.Add("@NUID", SqlDbType.VarChar).Value = queryvalue[0].ToString();
        myconn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        da.Fill(ds);




        myconn.Close();
        myconn.Dispose();
    }

1 个答案:

答案 0 :(得分:2)

这应该可以帮到你

ASP.NET Populate ListView with Stored Procedure

<asp:SqlDataSource ID="sdsYourData" Runat="server"
    ProviderName="System.Data.SqlClient"
    ConnectionString="Server=(local);Database=Northwind;Integrated Security=SSPI;"
    SelectCommand="dbo.YourStoredProcName" 
    <SelectParameters>
        <asp:Parameter Name="Param1" Type="String" />>
    </SelectParameters>
</asp:SqlDataSource>