我在谷歌上查了好几个小时但找不到解决我特定问题的方法。
我是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">
</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.";
}
}
答案 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;
其他解决方法是: