2010-02-11 124 views
0

我有一個數據庫表包含戲劇/戲劇的腳本。劇本可以分爲'戲劇和教育','爲年輕人進行戲劇'或兩者(通過使用bools來確定他們是否屬於上述類別);如何根據2個布爾表列填充列表視圖?

  • scriptID:整數
  • theatreAndEducation:位
  • playsForYoungPeople:位
  • scriptTitle:VARCHAR
  • scriptSample:文本
  • thumbImageUrl:VARCHAR

在主菜單我有一個下拉列表,其中包含以下鏈接;

  • 中用runat = 「服務器的」 href = 「〜/表單/ scriptList」>影院&教育
  • 中用runat = 「服務器的」 href = 「〜/ Web窗體/ scriptList」>扮演4年輕人

當用戶點擊其中一個鏈接時,它們被帶到一個包含listview的頁面。我希望這個listview填充與點擊鏈接相對應的記錄。

我猜猜querystring是必要的,但我想不出一個合適的可以使用。

以下是當前狀態下的列表視圖。關於查詢字符串的任何想法以及如何配置sql數據源以基於單擊的鏈接顯示適當的腳本記錄?

<asp:ListView ID="scriptsListView" runat="server" DataSourceID="SqlDataSource1"> 
    <LayoutTemplate> 
     <table> 
      <tr> 
       <td>title</td> 
       <td>summary</td> 
       <td>image</td> 
      </tr> 
      <tr> 
       <asp:PlaceHolder ID="itemPlaceholder" runat="server"> 
       </asp:PlaceHolder> 
      </tr> 
     </table> 
    </LayoutTemplate> 

    <ItemTemplate> 
     <tr> 
      <td><%# Eval ("scriptTitle") %></td> 
      <td><%# Eval ("scriptSample") %></td> 
      <td><asp:Image ID="image" runat="server" 
        ImageUrl='<%# "~/Images/Scripts/" + Eval("thumbImageUrl") %>' /></td> 
     </tr> 
    </ItemTemplate> 
</asp:ListView> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand=""> 
    <SelectParameters> 
    </SelectParameters> 
</asp:SqlDataSource> 

回答

1

設置select語句在SqlDataSource上的Page_Load

中用runat = 「服務器的」 href = 「〜/表單/ scriptList?類型=戲劇」>影院&教育

中用runat = 「服務器」 HREF = 「〜/的WebForms/scriptList?類型=戲劇」>起着4年輕人

保護無效的Page_Load(對象發件人,EventArgs的)

如果(Page.IsPostBack == FALSE) { 個字符串;

if (Request.QueryString["type"]=="theatre") 
    { 
     s="select * from scripts where theatreAndEducation = 1"; 
    } 
else 
    if (Request.QueryString["type"]=="plays") 
    { 
     s = "select * scripts where playsForYoungPeople = 1"; 
    } 
    SqlDataSource1.SelectCommand = s; 

}}

+0

不錯,艾倫。 非常感謝! – user271132 2010-02-11 16:29:52

0

您可以傳遞區分這兩個視圖的查詢字符串參數。 另外,在後面的代碼中,在SqlDataSource的Select事件中,可以換出select命令。

+0

對不起darthjit,但我不完全明白你的意思。 – user271132 2010-02-11 15:45:30