473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tie LinkButtons to EventHandler dynamicly (C#, Web)

3 New Member
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 1516
kenobewan
4,871 Recognized Expert Specialist
Try inserting this line - public event EventHandler Click;. HTH.
Jan 11 '08 #2
aladinsane
3 New Member
I feel a bit stupid for asking, but where do I put the line?
Jan 11 '08 #3
kenobewan
4,871 Recognized Expert Specialist
Line 44 should work, if I have the syntax right :)
Jan 12 '08 #4
aladinsane
3 New Member
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 Recognized Expert Specialist
Most likely reason is the enableautopostb ack and postback event.
Jan 14 '08 #6

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

Similar topics

4
5482
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
3
23197
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 code does not execute, it just reloads the page. Where am i going so wrong? i don't understand what's missing. many thanks in advance.
1
1478
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 this Control But when I click on this button, the only thing I notice is there is a post, but the eventhandler is never fired. :-( Is it possible that a dynmicly created control looses his event after an PostBack??
2
1976
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 to recreate some dynamic LinkButtons. My problems is I need the CommandEventHandler to fire before the controls are build so I can grab the ID that I will use to build the other Linkbuttons.
2
2011
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 the number of people i want to create a form where he can enter the persons details. For example he wants to register 3 people. Then the second step of the registration would have to be like this :
0
3007
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 not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. Where Peter is the value entered in the textbox for firstname (fnameTBox) I'm sure the problem is something obvious but I...
6
1439
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 to one of these LinkButtons depending on the selection. I am using the Command Event and passing a key in the Command.Name field. From within this even handler, I am clearing my LinkButtons and then re-redering and marking with a different...
2
1656
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 page specified in the PostBackUrl. I always thought (and the way I have always used it in the past, where it worked) that it posted back to the page the button is on, executed the stuff like Page_Load and the eventhandler for the button's Click...
8
12951
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 Dim panel1 As Panel = New Panel
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.