473,799 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically added LinkButton event handling

Hi,

I have a strange issue occurring with LinkButtons that are dynamically added
to each (data) row of my DataGrid on that grid's ItemDataBound event. Each
LinkButton is assigned its own event handler to deal with the Command event,
but for some reason I can get this to work in a C# project but not in a newly
created VB.NET one.

The relevant VB.NET is as follows:

''' <summary>
''' Bind the columns within the grid to runtime data
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Protected Sub MattersGrid_Ite mDataBound(ByVa l sender As Object,
ByVal e As DataGridItemEve ntArgs)
If (e.Item.ItemTyp e = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem OrElse e.Item.ItemType =
ListItemType.Se lectedItem) Then
Dim l_link As New LinkButton
With l_link
.ID = "MatterSelected Button"
.Text = DataBinder.Eval (e.Item.DataIte m,
"Name").ToStrin g()

If e.Item.ItemType = ListItemType.Se lectedItem Then
.BackColor = System.Drawing. Color.LightYell ow
Else
.CommandName = "ItemSelect ed"
.CommandArgumen t = e.Item.ItemInde x
AddHandler .Command, AddressOf MattersGrid_Com mand
End If
End With

' the 1th column is the templatecolumn in the grid
e.Item.Cells(2) .Controls.Add(l _link)
End If
End Sub

''' <summary>
''' Handle click-commands performed on the grid (TODO; currently
misbehaving)
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Protected Sub MattersGrid_Com mand(ByVal sender As Object, ByVal e As
CommandEventArg s)
If (e.CommandName = "ItemSelect ed") Then
MattersGrid.Sel ectedIndex =
Integer.Parse(e .CommandArgumen t.ToString)
RebindDG()
End If
End Sub

and the similar, but working code in C# is:

/// <summary>
/// Bind the columns within the grid to runtime data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TestDataGrid_It emDataBound(obj ect sender,
DataGridItemEve ntArgs e)
{
if (pr_debug) this.Page.Trace .Warn("TestData Grid_ItemDataBo und");

// only display the link on standard grid items (not
edit/select/header etc.)
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
LinkButton l_link = new LinkButton();
l_link.Text = DataBinder.Eval (e.Item.DataIte m,
"ID").ToString( );
l_link.CommandN ame = "ItemSelect ed";
l_link.CommandA rgument = e.Item.ItemInde x.ToString();
l_link.Command += new
CommandEventHan dler(TestDataGr id_Command);

// the 1th column is the templatecolumn in the grid
e.Item.Cells[1].Controls.Add(l _link);
}
}

/// <summary>
/// Handle click-commands performed on the grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TestDataGrid_Co mmand(Object sender, CommandEventArg s e)
{
if (pr_debug) this.Page.Trace .Warn("TestData Grid_Command");

if (e.CommandName == "ItemSelect ed")
{
TestDataGrid.Se lectedIndex =
int.Parse(e.Com mandArgument.To String());

RebindDG();
}
}

I know there are a couple of subtle differences between the two, but the
core is essentially the same. However, the VB.NET one never fires the Command
event but the C# one does.

The DataGrid in both cases is also dynamically added to the parent control
(a Web Part) and is a 'global' protected object.

Any ideas?

Thanks,

Marc
May 24 '07 #1
5 4719
Marc,

It looks like your VB is not working due to extra filter on If condition:
e.Item.ItemType = ListItemType.Se lectedItem
as in this case you do not add the handler to the link. As your
ItemDataBound event occurs after postback, your condition within the code
skips event hookup.

Anyhow with the grid you do not have to hook command events to every link
individually - the grid will do it for you in bulk if you just add RowCommand
event handler to the grid during Page_Init event (you only need to do it
during Postback)

"Marc Woolfson" wrote:
Hi,

I have a strange issue occurring with LinkButtons that are dynamically added
to each (data) row of my DataGrid on that grid's ItemDataBound event. Each
LinkButton is assigned its own event handler to deal with the Command event,
but for some reason I can get this to work in a C# project but not in a newly
created VB.NET one.

The relevant VB.NET is as follows:

''' <summary>
''' Bind the columns within the grid to runtime data
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Protected Sub MattersGrid_Ite mDataBound(ByVa l sender As Object,
ByVal e As DataGridItemEve ntArgs)
If (e.Item.ItemTyp e = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem OrElse e.Item.ItemType =
ListItemType.Se lectedItem) Then
Dim l_link As New LinkButton
With l_link
.ID = "MatterSelected Button"
.Text = DataBinder.Eval (e.Item.DataIte m,
"Name").ToStrin g()

If e.Item.ItemType = ListItemType.Se lectedItem Then
.BackColor = System.Drawing. Color.LightYell ow
Else
.CommandName = "ItemSelect ed"
.CommandArgumen t = e.Item.ItemInde x
AddHandler .Command, AddressOf MattersGrid_Com mand
End If
End With

' the 1th column is the templatecolumn in the grid
e.Item.Cells(2) .Controls.Add(l _link)
End If
End Sub

''' <summary>
''' Handle click-commands performed on the grid (TODO; currently
misbehaving)
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Protected Sub MattersGrid_Com mand(ByVal sender As Object, ByVal e As
CommandEventArg s)
If (e.CommandName = "ItemSelect ed") Then
MattersGrid.Sel ectedIndex =
Integer.Parse(e .CommandArgumen t.ToString)
RebindDG()
End If
End Sub

and the similar, but working code in C# is:

/// <summary>
/// Bind the columns within the grid to runtime data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TestDataGrid_It emDataBound(obj ect sender,
DataGridItemEve ntArgs e)
{
if (pr_debug) this.Page.Trace .Warn("TestData Grid_ItemDataBo und");

// only display the link on standard grid items (not
edit/select/header etc.)
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
LinkButton l_link = new LinkButton();
l_link.Text = DataBinder.Eval (e.Item.DataIte m,
"ID").ToString( );
l_link.CommandN ame = "ItemSelect ed";
l_link.CommandA rgument = e.Item.ItemInde x.ToString();
l_link.Command += new
CommandEventHan dler(TestDataGr id_Command);

// the 1th column is the templatecolumn in the grid
e.Item.Cells[1].Controls.Add(l _link);
}
}

/// <summary>
/// Handle click-commands performed on the grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TestDataGrid_Co mmand(Object sender, CommandEventArg s e)
{
if (pr_debug) this.Page.Trace .Warn("TestData Grid_Command");

if (e.CommandName == "ItemSelect ed")
{
TestDataGrid.Se lectedIndex =
int.Parse(e.Com mandArgument.To String());

RebindDG();
}
}

I know there are a couple of subtle differences between the two, but the
core is essentially the same. However, the VB.NET one never fires the Command
event but the C# one does.

The DataGrid in both cases is also dynamically added to the parent control
(a Web Part) and is a 'global' protected object.

Any ideas?

Thanks,

Marc
May 24 '07 #2
Hi Sergey,

Thanks for your reply. This extra condition just means that the LinkButton
will be added to the selected row in the VB.NET version but not the C#
version - this is required but I'm not event getting this far!

Your second point seems to be the problem I am experiencing - how do you
think I should modify the code to enable the desired event hookup? Do you
know why this may work in C# and not VB.NET?

I couldn't see a RowCommand for the grid, but there was the standard
ItemCommand one. I tried to implement this on the OnInit(), but it too didn't
fire when one of LinkButtons was clicked.

Thanks,

Marc

"Sergey Poberezovskiy" wrote:
Marc,

It looks like your VB is not working due to extra filter on If condition:
e.Item.ItemType = ListItemType.Se lectedItem
as in this case you do not add the handler to the link. As your
ItemDataBound event occurs after postback, your condition within the code
skips event hookup.

Anyhow with the grid you do not have to hook command events to every link
individually - the grid will do it for you in bulk if you just add RowCommand
event handler to the grid during Page_Init event (you only need to do it
during Postback)
May 25 '07 #3
I have just converted the entire project to C#... and the problem still
exists. Any ideas?

"Marc Woolfson" wrote:
Hi Sergey,

Thanks for your reply. This extra condition just means that the LinkButton
will be added to the selected row in the VB.NET version but not the C#
version - this is required but I'm not event getting this far!

Your second point seems to be the problem I am experiencing - how do you
think I should modify the code to enable the desired event hookup? Do you
know why this may work in C# and not VB.NET?

I couldn't see a RowCommand for the grid, but there was the standard
ItemCommand one. I tried to implement this on the OnInit(), but it too didn't
fire when one of LinkButtons was clicked.

Thanks,

Marc
May 25 '07 #4
"Marc Woolfson" <Ma**********@d iscussions.micr osoft.comwrote in message
news:E3******** *************** ***********@mic rosoft.com...
>I have just converted the entire project to C#... and the problem still
exists. Any ideas?
Dynamically created controls need to be created in Page_PreInit or
Page_Init, otherwise their events don't get wired up properly...

You're creating them far too late in the page cycle...
--
http://www.markrae.net

May 25 '07 #5
I have managed to get this working by adding a ButtonColumn in place of the
TemplateColumn and using the grid's ItemCommand event to capture the required
event.

Thanks for your assistance!

"Marc Woolfson" wrote:
I have just converted the entire project to C#... and the problem still
exists. Any ideas?
May 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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
8908
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the problem is that when you click the added column header it doesn't trigger the sort. The code : <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
0
2474
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
2
1950
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status) to determine what options should be available for this particular item (which is an issue... small issue tracking system). Each of these options is an action that may be performed on the issue and I am dynamically creating LinkButtons for each...
3
8528
by: Mark | last post by:
Assume you want to dynamically add one to many link button controls to a web page dynamically at run time. Each link button needs to post back and execute code. As the link buttons are created at run time and the number may vary, we can't statically tie to the event to a control. I'm assuming I need to dynamically add event handlers at run time, and that one method could act as the event handler, with unique event args being created for...
5
2305
by: tshad | last post by:
I found I can create Template columns dynamically - as long as I don't use objects that need onclick events, such as a LinkButton. Textboxes and Labels work fine. I create the Template columns like so: Dim column as TemplateColumn = new TemplateColumn() column.HeaderText = "Template Column" column.ItemStyle.Width = Unit.Pixel(width) column.HeaderStyle.Width = Unit.Pixel(width)
0
1375
by: Syoam4ka | last post by:
My project is about jewellery. I have devided my jewelery into main types, which each one of them has sub types, and each one those sub types has the jewellery. I have a tabcontainer. It includes tabpanels such as:Catalog,Terms,SiteMap.ViewCart ,etc. All the jewellery Intro is in the Catalog panel. when I first click the Catalog panel it Introduces me all the main types of the jewellery(It's all done dynamically from the cs file).The...
1
1450
by: successgac | last post by:
//I have the event declerations as follows down in a separate class file public event CommandEventHandler PageNumberClicked; //I have created link button dynamically using the following C# code which is also available in the same class HtmlTableCell htcPageNumber = new HtmlTableCell(); htcPageNumber.Attributes.Add("valign", "middle"); LinkButton lnkNewPageLink = new LinkButton(); ...
4
17928
by: jack | last post by:
Hi, Consider the following handler: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow) return;
0
9685
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
9538
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
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
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
5461
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
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
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
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.