简体   繁体   English

基于文件名的数据列表显示项

[英]Datalist display items based on file names

This question to related to a similar Data list question I posted some days ago. 该问题与几天前我发布的类似Data list问题有关。 I have a datalist which displays Categories, and then documents within categories. 我有一个显示类别的数据列表,然后显示类别中的文档。 What needs to happen is that documents under each category gets displayed in the order based on some numbers in file names. 需要发生的是,每个类别下的文档根据文件名中的一些数字按顺序显示。 Documents are in format like '001-filename.pdf', '002-filename.pdf' ... '00x-filename.pdf'. 文件的格式如'001-filename.pdf','002-filename.pdf'...'00x-filename.pdf'。 I can use the first dash as some kind of 'split' function then grab the numbers like '001' etc to make the sorting to work. 我可以将第一个破折号用作某种“拆分”功能,然后获取诸如“ 001”之类的数字以使排序工作。 I think this could be done on either itemdatabound or in the sql syntax. 我认为可以在itemdatabound或sql语法中完成此操作。 I am posting the relevant code here. 我在这里发布相关代码。 Any idea as to how I can make this to work? 关于如何使它起作用的任何想法? It is possible that there could be more than one document with shared number prefix: '001-filename.pdf', '001-filenameversion2.pdf' etc. Thanks! 可能会有多个共享编号前缀的文档:“ 001-filename.pdf”,“ 001-filenameversion2.pdf”等。谢谢!

ASPX: 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: C#代码背后:

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;
    }
}

Change your ORDER BY to include both columns. 更改您的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

The order by precedence is left to right, the default is "ASCENDING" but you can change a specific column sort by adding "ASC or DESC" directly after it. 优先顺序从左到右,默认值为“ ASCENDING”,但是您可以通过在其后直接添加“ ASC或DESC”来更改特定的列排序。

** IMPORTANT NOTE ** ** 重要的提示 **

Your code is susceptible to SQL Injection because your doing string concatenation. 您的代码很容易受到SQL注入的影响,因为您在进行字符串连接。 If this is a concern to you, change the SQL statement to use a named parameter that you assign using a command parameter (see example below). 如果您对此感到担心,请更改SQL语句以使用通过命令参数分配的命名参数(请参见下面的示例)。

Step 1: Change the inline string parameter, to a named parameter "@projectid". 步骤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

Step 2: Assign the parameter inline (example code) 步骤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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM