2012-08-14 124 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>