Listview中的ASP.Net Gridview - 如何访问listview以前的行数据

时间:2014-12-10 14:28:13

标签: asp.net listview gridview

我在谷歌上查了好几个小时但找不到解决我特定问题的方法。

我是ASP.NET& c# - 我有一个包含数据行(会议日期)的列表视图,其中每个都是一个gridview,显示要下载的任何相关文件。一切正常。现在我想添加另一行,其中包含将文件上传到该会议的选项。上传工作正常,但我需要访问父listview行CRMID字段(传递到存储过程以更新数据库)。我已经尝试了很多但似乎无法访问它。

非常感谢任何帮助。我附上代码,但它最好用屏幕截图显示(我是这个网站的新手,所以无法上传一个,但可以在这里查看一个:http://imgur.com/9tqtmRa)。当他们点击上传按钮时,我想从上一行访问该CRMID。

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound" DataKeyNames="CRMID">
        <LayoutTemplate>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th width="15px"> <%--"15px">--%>
                    </th>
                    <th width="10%">
                        CRMID
                    </th>
                    <th width="15%">
                        Period
                    </th>
                    <th width="15%">
                        Type
                    </th>
                    <th width="15%">
                        Planned Date
                    </th>
                    <th width="15%">
                        Actual Date
                    </th>
                    <th>
                        CRMNotes
                    </th>

                </tr>
            </table>
            <div runat="server" id="itemPlaceHolder">
            </div>
        </LayoutTemplate>
        <ItemTemplate>
            <div id="Div1" class="SUBDIV" runat="server">
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="15px">
                            <div class="btncolexp collapse">
                                &nbsp;
                            </div>
                        </td>
                        <td width="10%">
                            <%#Eval("CRMID") %>
                        </td>
                        <td width="15%">
                            <%#Eval("PeriodDescr") %>
                        </td>
                        <td width="15%">
                            <%#Eval("CRMType") %>
                        </td>
                        <td width="15%">
                            <%#Eval("CRMPlannedDate", "{0:dd/mm/yyyy}")%>
                        </td>
                        <td width="15%">
                            <%#Eval("CRMActualDate", "{0:dd/mm/yyyy}")%>
                        </td>
                        <td>
                            <%#Eval("CRMNotes")%> 
                        </td>
                    </tr>                      
                    <tr>
                        <td colspan="8" style="border-bottom-style:none">
                            <div style="margin: 20px">
                                <asp:GridView ID="GridView1" runat="server" class="MasterListView" AutoGenerateColumns="false" Width="100%"
                                            OnRowCommand="GridView1_RowCommand" 
                                             >
                                    <Columns>                                            
                                        <asp:BoundField HeaderText="File Name" DataField="ContractReviewMeetingFileName" ItemStyle-Width="80%"/>
                                        <asp:TemplateField HeaderText="Download" ItemStyle-Width="10%" >
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lnkDownload" runat="server" 
                                                    CommandArgument='<%# Eval("ContractReviewMeetingFileFullPath") %>' CommandName="cmdDownload">Download
                                                </asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField HeaderText="Upload Date" DataField="ContractReviewMeetingFileUploadDate" 
                                        DataFormatString="{0:d}" ItemStyle-Width="10%" />
                                        <%--<asp:BoundField HeaderText="Description" DataField="ContractReviewMeetingFileDescr" />--%>
                                    </Columns>
                                </asp:GridView>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="8">                                
                            <div style="margin: 10px 20px 20px 80px; text-align:center">                                    
                                Upload File: <asp:FileUpload ID="FileUploader" runat="server"/>
                                <asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
                                <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
                            </div>                                
                        </td>
                    </tr>
                </table>
            </div>
        </ItemTemplate>
    </asp:ListView>

代码背后(相关功能):

protected void UploadButton_Click(object sender, EventArgs e)
        {            
            //FileUpload FileUploader = (FileUpload)ListView1.FindControl("FileUploader");
            var btn = (Button)sender;
            var item = (ListViewItem)btn.NamingContainer;     

            var FileUploader = (FileUpload)item.FindControl("FileUploader");
            var StatusLabel = (Label)item.FindControl("StatusLabel");
            // I've tried various forms of below with no joy..
            //var itemParent = (ListViewItem)item.NamingContainer;
            //var CRMID = itemParent.FindControl("CRMID");

            string DefaultFileName = @"\\BCVSTORE03\PublicHealthStore$\CMS\Testing\";

            if (FileUploader.HasFile)
                try
                {
                    string filename = Path.GetFileName(FileUploader.FileName);
                    FileUploader.SaveAs(DefaultFileName + filename);//Server.MapPath(DefaultFileName) + filename);
                    //FileUploader.SaveAs(Server.MapPath(DefaultFileName) +
                    //     FileUploader.FileName);
                         StatusLabel.Text = "File name: " +
                         FileUploader.PostedFile.FileName + "<br>" +
                         FileUploader.PostedFile.ContentLength + " kb<br>" +
                         "Content type: " +
                         FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";

                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                StatusLabel.Text = "You have not specified a file.";
            }

        } 

1 个答案:

答案 0 :(得分:1)

您的FindControl调用失败,因为无法找到控件。 <%# %>不输出控件,只输出字符串文字。

这里有一个快速解决方法。在上传控件附近有一个隐藏字段,它将为您提供ID:

Upload File: <asp:FileUpload ID="FileUploader" runat="server"/>
<asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
<asp:HiddenField runat="server" id="CRMIDHiddenField" Value='<%# Eval("CRMID") %>' />

在后面的代码中使用它:

var FileUploader = (FileUpload)item.FindControl("FileUploader");
var StatusLabel = (Label)item.FindControl("StatusLabel");
var CRMID = ((HiddenField)itemParent.FindControl("CRMIDHiddenField")).Value;

其他解决方法是:

  1. 使用您当前只输出CMDIR的某个控件
  2. [在我看来,正确的方法,但需要大量工作]重构代码以使用列表视图命令而不仅仅是单独的控件事件,然后使用数据键。