2012-08-14 127 views
0

我currenty有一个存储过程,我正在运行,并希望绑定与我的列表视图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(); 
    } 
+0

哪里是ListView控件的ID? – GrayFox374 2012-08-14 17:09:38

+0

我没有。不知道列表视图需要一个id。只是认为信息被限制在行中,所以行会需要它。我对列表视图的知识非常有限 – user1512593 2012-08-14 17:17:26

+0

看起来您对ASP.NET的知识非常有限。谷歌搜索“ASP.NET数据驱动的控件” – 2012-08-14 18:16:58

回答

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>