转发器表中的下载选项

时间:2013-11-25 20:02:49

标签: c# asp.net

当用户上传任何文件时,当本文件批准时,此文件将获得批准,然后用户可以下载以下载其他虎钳

这是我想要的

           doc id doc name file uplaoded uploaded date department       status 
   download       1    analysis  abc.docx    12-12-2013   finance        approve
   download      2    report fm  fm.docx    14-06-2013   finance         reject
  download      3    report ibf  ibf.docx    14-06-2013   finance         approve
   download      4    report ma  ma.docx    14-06-2013   finance         reject

这就是我想要的

            doc id doc name file uplaoded uploaded date department       status 
   download       1    analysis  abc.docx    12-12-2013   finance        approve
                 2    report fm  fm.docx    14-06-2013   finance         reject
  download       3    report ibf  ibf.docx    14-06-2013   finance         approve
                 4    report ma  ma.docx    14-06-2013   finance         reject

现在在拒绝行中我不想显示下载选项,因为这是被拒绝的文档 这是下载代码

  protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            string filename = e.CommandArgument.ToString();
            string path = MapPath("~/Docfiles/" + filename);
            byte[] bts = System.IO.File.ReadAllBytes(path);
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length", bts.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=" + 
            filename);
            Response.BinaryWrite(bts);
            Response.Flush();
            Response.End();
        }


    }

HTML

            <div class="CSSTableGenerator">
                <table border="0" width="100%" cellpadding="0" cellspacing="0" 
                          id="results">
                    <asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" 
                       runat="server">
                        <HeaderTemplate>
                            <tr>
                                <td>
                                </td>
                                <td>
                                    Document ID
                                </td>
                                <td>
                                    Document Name
                                </td>
                                <td>
                                    File Uploaded
                                </td>
                                <td>
                                    Uploaded Date
                                </td>
                                <td>
                                    Document Type
                                </td>
                                <td>
                                    Department Type
                                </td>
                                <td>
                                    Approve Name
                                </td>
                            </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:LinkButton ID="LinkButton1" runat="server"   
                             CommandArgument='<%# Eval("FileUploaded") 
                                %>'                                              
             CommandName="download">Download</asp:LinkButton>
                                </td>
                                <td>                                            
                    <%#DataBinder.Eval(Container.DataItem,"DocumentID") %>
                                </td>
                                <td>
                                    <%#DataBinder.Eval(Container.DataItem, 
                           "DocumentName")%>
                                </td>
                                <td>
                                    <%#DataBinder.Eval(Container.DataItem, 
                   "FileUploaded")%>
                                </td>
                                 <td>
                                    <%#DataBinder.Eval(Container.DataItem, 
                        "UploadedDate")%>
                                </td>
                                <td>
                                    <%#DataBinder.Eval(Container.DataItem, 
                        "Document")%>
                                </td>
                                <td>
                                    <%#DataBinder.Eval(Container.DataItem,
                     "Department")%>
                                </td>
                                <td>
                                    <%#DataBinder.Eval(Container.DataItem, "Status")%>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                        </table>
                        </FooterTemplate>
                    </asp:Repeater>

我是怎么做到的?

1 个答案:

答案 0 :(得分:4)

由于您绑定到数据源并且在转发器的标记中没有要检查的文本的服务器控件,因此您需要使用DataRowView内的ItemDataBound对象事件,像这样:

DataRowView theDataRowView = e.Item.DataItem as DataRowView;

现在,您可以通过标记中使用的绑定值来引用数据,如下所示:

theDataRowView.Row["Status"] == "some value"

所以你的ItemDataBound事件现在应该是这样的:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // Only look in data rows, ignore header and footer rows
    if (e.Item.ItemType == ListItemType.AlternatingItem || 
        e.Item.ItemType == ListItemType.Item)
    {
        // Get a data view row object so we can reference the data 
        // in the repeater by the bound field names      
        DataRowView theDataRowView = e.Item.DataItem as DataRowView;

        // Make sure we got the data row view before we try to use it
        if(theDataRowView != null)
        {
            // Get the value of status from the control that holds the value
            string theStatus = theDataRowView.Row["Status"];

            // Find the download link control
            LinkButton theLinkButtonDownload = e.Item.FindControl("LinkButton1") as LinkButton;

            if(theStatus.ToLower() == "approve")
            {
                if(theLinkButtonDownload != null)
                {
                    theLinkButtonDownload.Visible = true;
                }
            }
            else
            {
                if(theLinkButtonDownload != null)
                {
                    theLinkButtonDownload.Visible = false;
                }
            }
        }
    }
}