DataList 编辑模式下的 FindControl
作为一名新的 .net/C# Web 初学者,当我尝试使用 FindControl 时,我总是会遇到困难。 砰——平放在我脸上。 这是我当前的 FindControl 问题:
我有一个 .aspx 页面和 Form,然后是 ajax updatePanel,里面有我的 DataList (DataList1),它有一个 EditItemTemplate: 具有以下内容:
<EditItemTemplate>
<asp:Label ID="thumbnailUploadLabel" runat="server" text="Upload a new thumbnail image:"/><br />
<asp:FileUpload ID="thumbnailImageUpload" runat="server" />
<asp:Button ID="thunbnailImageUploadButton" runat="server" Text="Upload Now" OnClick="thumbnailUpload"/><br />
</EditItemTemplate>
在我后面的 C# 代码中,我有 OnClick 代码fileUpload 对象:
protected void thumbnailUpload(object s, EventArgs e)
{
if (thumbnailImageUpload.HasFile)
{
//get name of the file & upload
string imageName = thumbnailImageUpload.FileName;
thumbnailImageUpload.SaveAs(MapPath("../../images/merch_sm/" + imageName));
//let'em know that it worked (or didn't)
thumbnailUploadLabel.Text = "Image " + imageName + "has been uploaded.";
}
else
{
thumbnailUploadLabel.Text = "Please choose a thumbnail image to upload.";
}
因此,我当然会收到 FileUpload 和 Label 的“对象引用未设置为对象的实例”。
在 OnClick 事件中处理这些控件之前,找到这些控件的正确语法是什么?
我使用 FindControl 的唯一方法是这样的:
labelthumbnailUploadLabel = DataList1.FindControl("thumbnailUploadLabel") as Label;
但是,这当然会引发“对象引用未设置到对象的实例”错误。 很感谢任何形式的帮助。
(我还看到了“递归”代码,应该可以使 FindControl 的使用变得更容易。哈!我对 C# 很陌生,我什至不知道如何将它们合并到我的项目中。)
感谢大家看看这个。
As a new .net/C# web begginner, I always get tripped up when I try to use FindControl. Blam -flat on my face. Here is my current FindControl problem:
I have an .aspx page and Form, then ajax updatePanel, inside it there is my DataList (DataList1) that has an EditItemTemplate: that has the following:
<EditItemTemplate>
<asp:Label ID="thumbnailUploadLabel" runat="server" text="Upload a new thumbnail image:"/><br />
<asp:FileUpload ID="thumbnailImageUpload" runat="server" />
<asp:Button ID="thunbnailImageUploadButton" runat="server" Text="Upload Now" OnClick="thumbnailUpload"/><br />
</EditItemTemplate>
In my C# code behind I have the OnClick code for the fileUpload object:
protected void thumbnailUpload(object s, EventArgs e)
{
if (thumbnailImageUpload.HasFile)
{
//get name of the file & upload
string imageName = thumbnailImageUpload.FileName;
thumbnailImageUpload.SaveAs(MapPath("../../images/merch_sm/" + imageName));
//let'em know that it worked (or didn't)
thumbnailUploadLabel.Text = "Image " + imageName + "has been uploaded.";
}
else
{
thumbnailUploadLabel.Text = "Please choose a thumbnail image to upload.";
}
So of course I'm getting "Object reference not set to an instance of an object" for the FileUpload and the Label.
What is the correct syntax to find these controls, before dealing with them in the OnClick event?
The only way Ive used FindControl is something like:
label thumbnailUploadLabel = DataList1.FindControl("thumbnailUploadLabel") as Label;
But of course this is throwing the "Object reference not set to an instance of an object" error. Any help is very much appreciated.
(I've also seen the 'recursive' code out there that is supposed to make using FindControl easier. Ha! I'm so green at C# that I don't even know how to incorporate those into my project.)
Thanks to all for taking a look at this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这已经晚了很多,但我一直在寻找要回答的问题......您现在一定已经想到了,但
如果您在代码中添加这些行,
这将为您正在编辑的行找到适当的控件。 ..
还将您的数据列表保留在更新面板之外,因为更新面板与文件上传不兼容。 如果您这样做,代码将运行,但它始终将 thumbnailImageUpload.HasFile 显示为 False。
I know this is a hell lot late but i was looking for questions to answer....you must have figured it by now but still
if you add these lines in your code
this will find the appropriate control for the row you are editing...
also keep your Datalist out of the UPdate Panel beacuse Update Panels are not compatible with FileUpload. if you do the code will run but it will always show thumbnailImageUpload.HasFile as False.