473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Command event for dynamically created linkbuttons not firing

106 New Member
Hi I have created linkbuttons dynamically using the below code




Expand|Select|Wrap|Line Numbers
  1. Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32)
  2.  
  3. Dim i = 0
  4.  
  5. Dim panel1 As Panel = New Panel
  6.  
  7. panel1.CssClass = "boxx"
  8.  
  9. pane.Controls.Add(panel1)
  10.  
  11. Dim lbButton As LinkButton = New LinkButton
  12.  
  13. lbButton.Text = commandtext
  14.  
  15. lbButton.CommandName = commandtext
  16.  
  17. lbButton.CommandArgument = Cmdarg
  18.  
  19. AddHandler lbButton.Command, AddressOf Me.LinkButton_Command
  20.  
  21. panel1.Controls.Add(lbButton)
  22.  
  23. End Sub
  24.  
  25.  
  26.  
  27. Protected Sub LinkButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
  28.  
  29. If e.CommandArgument.ToString = "Region" Then
  30.  
  31. Session("Region") = e.CommandName
  32.  
  33. End If
  34.  
  35. If e.CommandArgument.ToString = "LEA" Then
  36.  
  37. Session("LEA") = e.CommandName
  38.  
  39. End If
  40.  
  41. If e.CommandArgument.ToString = "Subject" Then
  42.  
  43. Session("Subject") = e.CommandName
  44.  
  45. End If
  46.  
  47. If e.CommandArgument.ToString = "Position" Then
  48.  
  49. Session("Position") = e.CommandName
  50.  
  51. End If
  52.  
  53. If e.CommandArgument.ToString = "SchoolPhase" Then
  54.  
  55. Session("SchoolPhase") = e.CommandName
  56.  
  57. End If
  58.  
  59. Response.Write("<script language=""javascript"">alert(""" & Session("Region") & """)</script>")
  60.  
  61. populate()
  62.  
  63. End Sub





Linkbuttons are successfully generated. When i click one of the linkbutton. The command event doesn't fired. While I have Add command event handler to the linkbuttons.

What is wrong in my code

Thanks
Mar 27 '09 #1
8 12951
Frinavale
9,735 Recognized Expert Moderator Expert
Where are you calling your "createlinkbutt on" method?
In the Page Init event?

If not then your event will never fire. The link button will post back to the server, but the server will not load the ViewState of the link button, and so the event for the link button will not be loaded...theref ore it will not fire.

-Frinny
Mar 27 '09 #2
Hamayun Khan
106 New Member
@Frinavale
Thanks for your reply.

But The problem is very strange. Please look into code problem once again.

Function to generate linkbuttons.
Expand|Select|Wrap|Line Numbers
  1. Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32)
  2.         Dim panela As Panel = New Panel
  3.         panela.CssClass = "boxx"
  4.         pane.Controls.Add(panela)
  5.         Dim lbButton As LinkButton = New LinkButton
  6.         lbButton.Text = commandtext
  7.         lbButton.CommandName = commandtext
  8.         lbButton.CommandArgument = Cmdarg
  9.         AddHandler lbButton.Command, AddressOf Me.LinkButton_Command
  10.         AddHandler lbButton.Click, AddressOf Me.LinkButton_Click
  11.         panela.Controls.Add(lbButton)
  12.     End Sub
  13.  
And the command event is
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub LinkButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
  2.         If e.CommandArgument.ToString = "Region" Then
  3.             Session("Region") = e.CommandName
  4.         End If
  5.         If e.CommandArgument.ToString = "LEA" Then
  6.             Session("LEA") = e.CommandName
  7.         End If
  8.         If e.CommandArgument.ToString = "Subject" Then
  9.             Session("Subject") = e.CommandName
  10.         End If
  11.         If e.CommandArgument.ToString = "Position" Then
  12.             Session("Position") = e.CommandName
  13.         End If
  14.         If e.CommandArgument.ToString = "SchoolPhase" Then
  15.             Session("SchoolPhase") = e.CommandName
  16.         End If
  17.         Response.Write("<script language=""javascript"">alert(""" & Session("Region") & """)</script>")
  18.         populate()
  19.         Response.Write("<script language='javascript'>alert('hello')</script>")
  20.     End Sub
  21.  
Another funciton for some other functionality
Expand|Select|Wrap|Line Numbers
  1. Public Sub loadRegion()
  2. For k = 0 To DT.Rows.Count
  3.       createlinkbutton(DT.Rows(k)("Region"), DT.Rows(k)("BtnArg"), Pane1, DT.Rows(k)("num"))
  4. Next
  5.  
  6.         'createlinkbutton("London", "Region", Pane, 12)
  7.         'createlinkbutton("aLondon", "Region", Pane, 12)
  8.         'createlinkbutton("Lsondon", "Region", Pane, 12)
  9.         'createlinkbutton("Lodndon", "Region", Pane, 12)
  10.         'createlinkbutton("Londdon", "Region", Pane, 12)
  11.         'createlinkbutton("Londfon", "Region", Pane, 12)
  12.         'createlinkbutton("Londofn", "Region", Pane, 12)
  13.         'createlinkbutton("fLondon", "Region", Pane, 12)
  14.         'createlinkbutton("Lcondon", "Region", Pane, 12)
  15.         'createlinkbutton("Lovndon", "Region", Pane, 12)
  16.         'createlinkbutton("Lon don", "Region", Pane, 12)
  17.         'createlinkbutton("Loncdon", "Region", Pane1, 12)
  18.         'createlinkbutton("Loncdon", "Region", Pane1, 12)
  19.         'createlinkbutton("Londxon", "Region", Pane1, 12)
  20.         'createlinkbutton("Londosn", "Region", Pane1, 12)
  21.         'createlinkbutton("Londons", "Region", Pane1, 12)
  22.         'createlinkbutton("kLondon", "Region", Pane1, 12)
  23.         'createlinkbutton("Ljondon", "Region", Pane, 12)
  24.         'createlinkbutton("Lonndon", "Region", Pane, 12)
  25.         'createlinkbutton("Lonndon", "Region", Pane, 12)
  26.         'createlinkbutton("Londnon", "Region", Pane1, 12)
  27.         'createlinkbutton("Londonn", "Region", Pane1, 12)
  28. End Sub
  29.  
Now if I call the LoadRegion function from any where the button generated don't fires the command event. If i comment the For loop portion and uncomment the
commented portion then the button generated works fine. i.e when i click one of the button command event fires successfully.

Instead of using if i use while loop buttons are created successfully but event doesn't work.

This is very urgent.
Waiting for quick reply.
Mar 30 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Is DT.Rows.Count = 0?
Does your loop actually happen?
Have you tried stepping through this?

You never answered my original question.
Are you calling this in your Page Init event?
Mar 30 '09 #4
Hamayun Khan
106 New Member
@Frinavale
NO DT.Rows.Count is not equals 0 but the loop happen successfully.

And I m calling this from page load event.

Thanks
Mar 31 '09 #5
Frinavale
9,735 Recognized Expert Moderator Expert
Move it to the Page Init event.
For more information on why please see this article on how to use dynamic controls in asp.net.
Mar 31 '09 #6
Hamayun Khan
106 New Member
@Frinavale

Thanks

If from the page load event I call to button create function like below then the command event works fine.
Expand|Select|Wrap|Line Numbers
  1.  
  2. createlinkbutton("Londosn", "Region", Pane1, 12) 
  3.  
  4.  
but if i place the same code in for or while loop then the command event doesn't work.

e.g
Expand|Select|Wrap|Line Numbers
  1. for i=1 to 5
  2. createlinkbutton("Londosn", "Region", Pane1, 12) 
  3. Next
  4.  
Here the command event does not work.
Apr 3 '09 #7
Frinavale
9,735 Recognized Expert Moderator Expert
Well,one obvious thing is that you are creating a button with the command name and text "Londosn" in Panel 1, five times.

Expand|Select|Wrap|Line Numbers
  1. for i=1 to 5
  2. createlinkbutton("Londosn", "Region", Pane1, 12) 
  3. Next
  4.  

Have you considered providing unique values (like you do by hand when you aren't looping) to see if it works?

For example:
Expand|Select|Wrap|Line Numbers
  1. for i=1 to 5
  2. createlinkbutton("Londosn_"+i.ToString, "Region", Pane1, 12) 
  3. Next
  4.  
Apr 3 '09 #8
Hamayun Khan
106 New Member
@Frinavale

Thanks for Reply. I have done the job little different.
Expand|Select|Wrap|Line Numbers
  1. <asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1" RepeatDirection="horizontal"
  2.                     RepeatLayout="Flow">
  3.                     <ItemTemplate>
  4.                         <asp:LinkButton OnCommand="Command"  CommandName="Region" CommandArgument='<%# Eval("Region") %>' runat="server"><%# Eval("Region") %></asp:LinkButton>                        
  5.                         |
  6.                     </ItemTemplate>
  7.                 </asp:DataList>
  8.                 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbase_DataConnectionString6 %>"
  9.                     SelectCommand="SELECT  [Region] FROM [tblregion]"></asp:SqlDataSource>
  10.  
It is now working fine. thanks.
Apr 6 '09 #9

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

Similar topics

2
3714
by: Linda | last post by:
Hi, How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample? Many thanks, Linda
1
1575
by: Grant | last post by:
The Idea: Page1.aspx sets a property value to an arraylist. Page2.aspx retreives that property (using a Context.Handler) every time the page loads. It then iterates through the arraylist and builds a table containing dynamically created LinkButtons. These LinkButtons have an event handler. The problem: When I click one of these linkbuttons, the page re-initialises and calls the Page_Load event. At this point that arraylist is lost and...
6
1942
by: James Norton-Jones | last post by:
Hi, I am wanting to create Linkbuttons and Event Handlers on the fly. Ideally I would be able to pass the CommandName and CommandArgument to the Event Handler which in turn would pass these to a generic method for processing. I have been able to create the LinkButtons themselves on the fly. However I haven't managed to connect the to an Event Handler.
1
2234
by: Klom Dark | last post by:
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and the name of a common command (btn_Command) assigned to it's Command property. The creation of the table is done by a function called drawGeo. drawGeo is called during the initial Page_Load (!IsPostBack), but should be called by btn_Command on...
0
1312
by: Solomon Shaffer | last post by:
This is very interesting - and odd. I have a number of LinkButtons that are created in a custom data grid that I created. The purpose of the LinkButtons is to provide alphabetical filtering functionality on the grid. Therefore, a LinkButton is added for every letter of the alphabet. I also have a numeric filter button and an "All" button that removes the filter. All of the LinkButtons are wired up to the same event called...
3
4323
by: JoakimR | last post by:
Hello, I have a problem with events not firing. I've created a web user control which renders a calendar using a table control. In two cells I'm adding LinkButtons for "prev/next month". However, the events from these are not fired. In the PageLoad event I check if it's a PostBack. If it isn't, I'm calling DisplayCalendar() to render the calendar. This part works and I get the output. If it *is* a postback (i.e. one of the linkbuttons...
2
2242
by: Steve | last post by:
Hi, I have a dynamically created ImageButton in a custom control, and I can't get the control to handle the ImageButton Click event. I see the control in the Page.Request.Form.AllKeys collection, but it's represented as "imgbtn.X" (and .Y), and I'm guessing that .NET can't connect the name to the event since the ID isn't an exact match. I read an article stating that I need to use IPostBackDataHandler methods, but I didn't have a lot of...
0
1030
by: viovanov | last post by:
I am trying to get some events to fire properly. I have a series of LinkButtons with some attribute set, one of which is unique for each LinkButton. At first I load a few buttons, and I set their Click event to the same function. In this function I need to add more LinkButtons. Not a problem at the first click. (I load the linkButtons once at Load_Page and then I reload them in the click event). At the second click (on an item created...
1
5551
by: RSH | last post by:
Hi, I have a page that is generating Linkbuttons dynamically. I need to dynamically wire up the buttons to an OnClick handler server side. I am using this code but they are not firing the event. What am i missing? Dim ctrl As New LinkButton
0
9663
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
10195
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
10136
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
7525
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
6765
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
5415
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...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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
3695
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.