views:

2115

answers:

2

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.

A: 

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.

Pankaj Kumar
Hey Pankaj, thanks for the response. Late is better then never. I ended up using code similar to what you've posted above. Yes, I did discover the FileUpload inside UpdatePanel problem, so fixed that once I discoved they don't play well together. Also - I've discovered NeatUpload by Dean Brettle (www.brettle.com) as a very elegant Multi-File upload object - incase the need arises...
Doug
A: 

Tht's really working..........

Bharath

related questions