473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Tie LinkButtons to EventHandler dynamicly (C#, Web)

Hi,

I trying to build a table using the TableControl in order manage som image files. What I would like to do is to load an image combined with a linkbutton into each cell and to delete the image if you click it.

The table is created complete with links and images, but the eventhandler attached to the linkbutton is not reached when I activate the linkbutton.

I assume that it has something to do with how the page is loaded, but I cannot figure out how to solve it.

I hope someone can supply me with some guidiance or perhaps just tell me whether it's even possible or not. The complete code for the codebehind file is:
Expand|Select|Wrap|Line Numbers
  1. public partial class Backend_manageImages : System.Web.UI.Page
  2. {
  3.     ProductFactory objImages = new ProductFactory();
  4.     CategoryFactory objCat = new CategoryFactory();
  5.  
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.         if (!IsPostBack)
  9.         {
  10.             ddlAlbums.DataTextField = "fldName";
  11.             ddlAlbums.DataValueField = "fldID";
  12.             ddlAlbums.DataSource = objCat.getAll();
  13.             ddlAlbums.DataBind();
  14.         }
  15.         createTable();
  16.     }
  17.     protected void ddlAlbums_SelectedIndexChanged(object sender, EventArgs e)
  18.     {
  19.         createTable();
  20.     }
  21.  
  22.     protected void createTable()
  23.     {
  24.         tblImages.Controls.Clear();
  25.  
  26.         TableRow tr = new TableRow();
  27.         foreach (DataRow image in objImages.getByCategoryID(Convert.ToInt32(ddlAlbums.SelectedValue)).Rows)
  28.         {
  29.             TableCell td = new TableCell();
  30.             //td.Text = "<a href=\"?ID=" + image["fldID"] + "\"><img src=\"../images/thumbs/" + image["fldFile"] + ".jpg\" alt=\"\" /></a>";
  31.             Image img = new Image();
  32.             LinkButton lnk = new LinkButton();
  33.             img.ImageUrl = "../images/thumbs/" + image["fldFile"] + ".jpg";
  34.             img.ToolTip = "Klik for at slette billedet";
  35.             lnk.OnClientClick = "javascript: return confirm('Delete " + image["fldFile"] + ".jpg ?');";
  36.             lnk.CommandArgument = image["fldID"].ToString();
  37.             lnk.Click += lnk_Click;
  38.             lnk.Controls.Add(img);
  39.             td.Controls.Add(lnk);
  40.             tr.Controls.Add(td);
  41.         }
  42.         tblImages.Controls.Add(tr);    
  43.     }
  44.  
  45.     protected void lnk_Click(object sender, EventArgs e)
  46.     {
  47.         LinkButton lnk = (LinkButton)sender;
  48.         int ID = Convert.ToInt32(lnk.CommandArgument);
  49.         DataRow image = objImages.getById(ID);
  50.         System.IO.File.Delete(Request.PhysicalApplicationPath + "images\\" + image["fldFile"] + ".jpg");
  51.         System.IO.File.Delete(Request.PhysicalApplicationPath + "images\\thumbs\\" + image["fldFile"] + ".jpg");
  52.         objImages.deleteProduct(ID);
  53.         Response.Redirect(Request.UrlReferrer.ToString());
  54.     }
  55. }
  56.  
Jan 11 '08 #1
5 1498
kenobewan
4,871 Expert 4TB
Try inserting this line - public event EventHandler Click;. HTH.
Jan 11 '08 #2
I feel a bit stupid for asking, but where do I put the line?
Jan 11 '08 #3
kenobewan
4,871 Expert 4TB
Line 44 should work, if I have the syntax right :)
Jan 12 '08 #4
Thanks a lot. The click event is now working - however only sometimes... If I choose a different "Album set" from my dropdown box, the click event is ignored.

After reading up on understand EventHandlers at MSDN without no sign of any epiphany I deciced to give up and use QueryStrings to switch between the album sets.

Cheers
Jan 14 '08 #5
kenobewan
4,871 Expert 4TB
Most likely reason is the enableautopostback and postback event.
Jan 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
1
by: Kristof Pauwels | last post by:
Hellow, I have this peace of code from an ASP.NET page for a Mobile device, but i have a small problem. In this first SUB I dynmamicly create a MobileControls.Command. I add an EventHandler to...
2
by: TPS | last post by:
I have some dynamic controls (LinkButton) that I am adding to my pages. When the link button is clicked, I am getting an ID from the LinkButton.CommandArguement value. I am then using that ID...
2
by: DaWoE | last post by:
Hi all, I'm fairly new to ASP.NET. What i want to do is creat a online registration form. On the first step is getting the users details and the number of people he wants to register. Based on...
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
6
by: Andrew Robinson | last post by:
I have a page that contains a number of link buttons that are used for making selections. I load my LinkButtons during the Page_PreInit event and they render fine but then I need to make a change...
2
by: Nathan Sokalski | last post by:
I have an UpdatePanel that contains a LinkButton with a PostBackUrl. The LinkButton is marked as a PostBackTrigger, so it does a full postback. However, when it posts back, it goes straight to the...
8
by: Hamayun Khan | last post by:
Hi I have created linkbuttons dynamically using the below code Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32) Dim i =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.