【问题标题】:How do I populate a listview based on 2 boolean table columns?如何基于 2 个布尔表列填充列表视图?
【发布时间】:2010-02-11 14:45:30
【问题描述】:

我有一个包含戏剧/戏剧脚本的数据库表。剧本可以归类为“戏剧和教育”、“年轻人的戏剧”或两者(通过使用布尔值来确定它们是否属于上述类别);

  • scriptID: int
  • 剧院和教育:位
  • playsForYoungPeople: 位
  • scriptTitle: varchar
  • scriptSample:文本
  • thumbImageUrl: varchar

在主菜单中,我有一个下拉列表,其中包含以下链接;

  • a runat="server" href="~/WebForm/scriptList">戏剧和教育
  • a runat="server" href="~/WebForms/scriptList">扮演4个年轻人

当用户点击其中一个链接时,他们会被带到一个包含列表视图的页面。我希望这个列表视图填充与单击的链接相对应的记录。

我猜测需要一个查询字符串,但我想不出一个可以使用的合适的。

下面是当前状态的列表视图。关于查询字符串以及如何配置 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>

【问题讨论】:

    标签: c# .net asp.net sql listview


    【解决方案1】:

    在 page_load 上为 sqldatasource 设置 select 语句

    a runat="server" href="~/WebForm/scriptList?type=theatre">戏剧与教育

    a runat="server" href="~/WebForms/scriptList?type=plays">扮演4个年轻人

    protected void Page_Load(object sender, EventArgs e)

    如果(Page.IsPostBack == false) { 字符串 s;

    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;
    

    } }

    【讨论】:

    • 是的,这很完美,艾伦。非常感谢!
    【解决方案2】:

    您可以传递一个查询字符串参数来区分这两个视图。 此外,在后面的代码中,在 SqlDataSource 的 Select 事件中,您可以换出 select 命令。

    【讨论】:

    • 抱歉 darthjit 但我不完全确定您的意思。
    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2014-01-05
    • 1970-01-01
    • 2012-07-14
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多