Datalist显示基于文件名的项目

时间:2012-04-18 15:29:59

标签: c# asp.net datalist

此问题与我几天前发布的类似Data list问题有关。我有一个显示类别的数据列表,然后是类别中的文档。需要发生的是,每个类别下的文档会根据文件名中的某些数字按顺序显示。文档的格式类似于'001-filename.pdf','002-filename.pdf'...'00x-filename.pdf'。我可以使用第一个破折号作为某种“拆分”功能,然后抓取像'001'等数字,以使分拣工作。我认为这可以在itemdatabound或sql语法中完成。我在这里发布相关代码。有什么想法,我怎么能让它工作?可能有多个文档具有共享数字前缀:'001-filename.pdf','001-filenameversion2.pdf'等等。谢谢!

ASPX:

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Vertical" DataKeyField="docid"
    EnableViewState="True" OnItemDataBound="DataList1_ItemDataBound">
    <ItemTemplate>
        <table cellpadding="0" cellspacing="0" id="tbl_data">
            <tr runat="server" id="tr_category">
                <td>
                    <asp:Label ID="lblHeader" runat="server" Font-Bold="True" Text='<%# Eval("categoryname") %>'
                        Font-Underline="True"></asp:Label>
                    <asp:Label runat="server" ID="lbl_cb_all">Select All
                        <asp:CheckBox runat="server" OnCheckedChanged="CheckAllChanged" AutoPostBack="true"
                            ID="cb_selectall" />
                    </asp:Label>
                    <asp:HiddenField ID="HiddenCatID" runat="server" Value='<%# Eval("CatID") %>' />
                    <asp:HiddenField ID="HiddenDocID" runat="server" Value='<%# Eval("docid") %>' />
                </td>
            </tr>
            <tr runat="server" id="tr_data">
                <td>
                    <asp:CheckBox runat="server" ID="cb_docid" Value='<%# Eval("docid") %>' OnCheckedChanged="displayselectedinit"
                        AutoPostBack="true" />
                    <asp:HyperLink ID="hpl_docfileencr" Text='<%# Eval("docfileencr") %>' NavigateUrl='<%# "~/PDFEncr/" + DataBinder.Eval(Container.DataItem, "docfileencr") %>'
                        Target="_blank" runat="server" />
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

C#CodeBehind:

sqlsyntax = @"SELECT  dbo.projectsdocuments.docfileencr,dbo.categories.catid, dbo.categories.categoryname, dbo.projectsdocuments.docid
              FROM dbo.Projects INNER JOIN dbo.projectsdocuments ON (dbo.Projects.projectid = dbo.projectsdocuments.projectid)
              INNER JOIN dbo.categories ON (dbo.projectsdocuments.categoryid = dbo.categories.catid)
              WHERE  Projects.projectid = " + projectid + " ORDER BY dbo.categories.sortorder ASC";

protected void DataList1_ItemDataBound(Object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var row = (DataRowView)e.Item.DataItem;
        var view = row.DataView;
        var lastRow = e.Item.ItemIndex == 0 ? null : view[e.Item.ItemIndex - 1];
        var tr_category = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("tr_category");
        var sameCategory = lastRow != null && (int)row["catid"] == (int)lastRow["catid"];
        tr_category.Visible = !sameCategory;
    }
}

1 个答案:

答案 0 :(得分:1)

将您的ORDER BY更改为包含两列。

SELECT  
    dbo.projectsdocuments.docfileencr,
    dbo.categories.catid, 
    dbo.categories.categoryname, 
    dbo.projectsdocuments.docid
FROM dbo.Projects 
INNER JOIN dbo.projectsdocuments 
    ON (dbo.Projects.projectid = dbo.projectsdocuments.projectid)
INNER JOIN dbo.categories 
    ON (dbo.projectsdocuments.categoryid = dbo.categories.catid)
WHERE  Projects.projectid = " + projectid + "
ORDER BY dbo.categories.sortorder, dbo.projectsdocuments.docfileencr

优先顺序是从左到右,默认为“ASCENDING”,但您可以通过在其后直接添加“ASC或DESC”来更改特定的列排序。

**重要说明**

您的代码容易受到 SQL注入的影响,因为您正在进行字符串连接。如果您担心这一点,请更改SQL语句以使用您使用命令参数分配的命名参数(请参阅下面的示例)。

第1步:将内联字符串参数更改为命名参数“@projectid”。

SELECT  
    dbo.projectsdocuments.docfileencr,
    dbo.categories.catid, 
    dbo.categories.categoryname, 
    dbo.projectsdocuments.docid
FROM dbo.Projects 
INNER JOIN dbo.projectsdocuments 
    ON (dbo.Projects.projectid = dbo.projectsdocuments.projectid)
INNER JOIN dbo.categories 
    ON (dbo.projectsdocuments.categoryid = dbo.categories.catid)
 WHERE  Projects.projectid = @projectid
 ORDER BY dbo.categories.sortorder, dbo.projectsdocuments.docfileencr

第2步:分配参数内联(示例代码)

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    SqlCommand command = new SqlCommand(sql, conn);
    command.CommandType = CommandType.Text;

    // Assign the value projectid to the parameter @projectid
    command.Parameters.Add(new SqlParameter("@projectid", projectid));

    // Execute The Command (fill dataset, create datareader, etc...)
    SqlDataReader reader = command.ExecuteReader();
}