Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ImageButton

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
I'm dymanically creating ImageButtons on my page and trying to associate all of them with 1 event handler. This is my code, which compiles fine:
Code:
ImageButton ib = new ImageButton();
ib.ImageUrl = @"..\images\delete.gif";
ib.ID = "ib" + y.ToString();
ib.ToolTip = "Delete file '" + f.Name + "'";

ImageClickEventHandler iceh = new ImageClickEventHandler(DeleteFile);

ib.Click += new ImageClickEventHandler(iceh);

THis is the code for DeleteFile
Code:
    private void DeleteFile(object sender, ImageClickEventArgs e)
    {
        Response.Write("FFFFFFFFFFFFF");
        Response.End();
    }

It never writes anything to the page, any ideas?
 
I forgot to mention that the variable y is an int and is within a loop, so it gets incremented with each pass, making the ID of the ImageButton unique.
 
well the only code that's missing is a loop, just a simple for(int y=0;y<10;y++)

but I figured it out anyways...here's a link to the answer. In short, you can only create EventHandlers in the Page_Load event of a page.

 
well the only code that's missing is a loop, just a simple for(int y=0;y<10;y++)
There must be other code. If you just placed that code into a code behind file (even with a loop around it) it will not work. It would at least have to be part of an event.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
I guess I made the wrong assumption that people would know it was part of an event. I didn't think others wanted me to post the unnecessary code (some 100+ lines that is irrelevant to what I was trying to achieve) for them to filter through.

Now I know.
 
some 100+ lines that is irrelevant to what I was trying to achieve
i would argue you could break the code out into more managable objects using OOP

if you create controls dynamically they must be created each postback. if you create the controls in the Init event and assign an id to the control it will be tracked in viewstate.

why not load the data into simple data objects and bind the data to a repeater which generates the buttons? something like this
Code:
protected override void OnInit(EventArs e)
{
   ImageRepeater.ItemCommand += ImageRepeaterCommand;
}

protected override void OnLoad(EventArs e)
{
   If(!IsPostBack) LoadImages();
}

private void LoadImages()
{
   ImageRepeater.DataSource = GetListOfImagesFromSomewhere();
   ImageRepeater.DataBind();
}

private void ImageRepeaterCommand(object sender, RepeaterCommandEventArgs e)
{
   switch(e.CommandName)
   {
      case "DeleteImage":
         string fileName = e.CommandArgument;
         if (File.Exists(fileName))
         {
            File.Delete(fileName);
            LoadImages;
         }
         break;
      default: break; //do nothing
   }
}
Code:
<asp:Repeater
   id="ImageRepeater"
   runat="server" >
   <ItemTemplate>
      <asp:ImageButton 
         id="DeleteImageButton"
         runat="server"
         CommandName="DeleteImage"
         CommandArgument='<%#Eval("FileName")%>'
         ImageUrl='<%#Eval("FileName")%>'
         ToolTip='<%# "Delete file " + Eval("FileName")%>'
      />
   </ItemTemplate>
</asp:Repeater>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I guess I made the wrong assumption that people would know it was part of an event. I didn't think others wanted me to post the unnecessary code (some 100+ lines that is irrelevant to what I was trying to achieve) for them to filter through.

Now I know.
The problem with assumptions is that they can often be wrong. If we assumed you had done everything else correctly, we could be wrong. If you assume we know what the rest of your code looks like, that is definately wrong.

Yes, you're correct that we don't want to see 100+ lines of irrelevant code, but we do want to see all code that relates to the problem. What you should have done was to create a completely new page, only add the code that demonstrates the problem and then make the post here with all that code. We can then simply copy/paste, find the error and post back a solution. This would have most likely happened very early on the thread rather than a post dragging on due to lack of information and guesswork from all parties.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top