G
Guest
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_ItemDataBound(ByVal sender As Object,
ByVal e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem OrElse e.Item.ItemType =
ListItemType.SelectedItem) Then
Dim l_link As New LinkButton
With l_link
.ID = "MatterSelectedButton"
.Text = DataBinder.Eval(e.Item.DataItem,
"Name").ToString()
If e.Item.ItemType = ListItemType.SelectedItem Then
.BackColor = System.Drawing.Color.LightYellow
Else
.CommandName = "ItemSelected"
.CommandArgument = e.Item.ItemIndex
AddHandler .Command, AddressOf MattersGrid_Command
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_Command(ByVal sender As Object, ByVal e As
CommandEventArgs)
If (e.CommandName = "ItemSelected") Then
MattersGrid.SelectedIndex =
Integer.Parse(e.CommandArgument.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_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (pr_debug) this.Page.Trace.Warn("TestDataGrid_ItemDataBound");
// only display the link on standard grid items (not
edit/select/header etc.)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
LinkButton l_link = new LinkButton();
l_link.Text = DataBinder.Eval(e.Item.DataItem,
"ID").ToString();
l_link.CommandName = "ItemSelected";
l_link.CommandArgument = e.Item.ItemIndex.ToString();
l_link.Command += new
CommandEventHandler(TestDataGrid_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_Command(Object sender, CommandEventArgs e)
{
if (pr_debug) this.Page.Trace.Warn("TestDataGrid_Command");
if (e.CommandName == "ItemSelected")
{
TestDataGrid.SelectedIndex =
int.Parse(e.CommandArgument.ToString());
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
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_ItemDataBound(ByVal sender As Object,
ByVal e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem OrElse e.Item.ItemType =
ListItemType.SelectedItem) Then
Dim l_link As New LinkButton
With l_link
.ID = "MatterSelectedButton"
.Text = DataBinder.Eval(e.Item.DataItem,
"Name").ToString()
If e.Item.ItemType = ListItemType.SelectedItem Then
.BackColor = System.Drawing.Color.LightYellow
Else
.CommandName = "ItemSelected"
.CommandArgument = e.Item.ItemIndex
AddHandler .Command, AddressOf MattersGrid_Command
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_Command(ByVal sender As Object, ByVal e As
CommandEventArgs)
If (e.CommandName = "ItemSelected") Then
MattersGrid.SelectedIndex =
Integer.Parse(e.CommandArgument.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_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (pr_debug) this.Page.Trace.Warn("TestDataGrid_ItemDataBound");
// only display the link on standard grid items (not
edit/select/header etc.)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
LinkButton l_link = new LinkButton();
l_link.Text = DataBinder.Eval(e.Item.DataItem,
"ID").ToString();
l_link.CommandName = "ItemSelected";
l_link.CommandArgument = e.Item.ItemIndex.ToString();
l_link.Command += new
CommandEventHandler(TestDataGrid_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_Command(Object sender, CommandEventArgs e)
{
if (pr_debug) this.Page.Trace.Warn("TestDataGrid_Command");
if (e.CommandName == "ItemSelected")
{
TestDataGrid.SelectedIndex =
int.Parse(e.CommandArgument.ToString());
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