Question

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.

Was it helpful?

Solution

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

protected void thumbnailUpload(object sender, EventArgs e)
    {
        FileUpload thumbnailImageUpload =(FileUpload)DataList1.Items[DataList1.EditItemIndex].FindControl("thumbnailImageUpload");
        Label thumbnailUploadLabel = (Label)DataList1.Items[DataList1.EditItemIndex].FindControl("thumbnailUploadLabel");
        if (thumbnailImageUpload.HasFile)
        {

           //Your code here

        }
        else
        {
            thumbnailUploadLabel.Text = "Please choose a thumbnail image to upload.";
        }
    }

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top