Problem with AddHandler (New CommandEventHandler) under framework 2.0

F

Fabrice

Hello

This code works under framework 1.1. I add in the header an image with a
command event to execute a sub on click.
It work fine with 1.1. But not under 2.0.

I think the problem could be with the declaration :AddHandler
myBtnLibEveUp.Command, New CommandEventHandler(AddressOf myImgButtonSorting)

Is there a way to build a control and add an command handler ?

thanks for your help
fabrice


Code to add my first image button.

Sub my009DataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Header Then

'Add Control ImageButton UP LIBEVE.
Dim myBtnLibEveUp As New ImageButton
With myBtnLibEveUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")
.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnLibEveUp"
.CommandArgument="LIBEVE ASC"
AddHandler myBtnLibEveUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

.....

The procedure :

Sub myImgButtonSorting (sender As Object, e As
System.Web.UI.WebControls.CommandEventArgs)

response.write("test")

end sud
 
L

Lloyd Sheen

Fabrice said:
Hello

This code works under framework 1.1. I add in the header an image with a
command event to execute a sub on click.
It work fine with 1.1. But not under 2.0.

I think the problem could be with the declaration :AddHandler
myBtnLibEveUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)

Is there a way to build a control and add an command handler ?

thanks for your help
fabrice


Code to add my first image button.

Sub my009DataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Header Then

'Add Control ImageButton UP LIBEVE.
Dim myBtnLibEveUp As New ImageButton
With myBtnLibEveUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")

.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnLibEveUp"
.CommandArgument="LIBEVE ASC"
AddHandler myBtnLibEveUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

....

The procedure :

Sub myImgButtonSorting (sender As Object, e As
System.Web.UI.WebControls.CommandEventArgs)

response.write("test")

end sud

I don't see any code to add the control to the page.

LS
 
F

Fabrice

Sorry. This is my code :

In my aspx file, I have a datagrid with the "OnItemCreated" parameter.
In my code behind (VB file), I have built all the buttons by code
(ImageButton).

The Handler on the differents ImageButtons seems not be fire under .net 2.0
It is ok under 1.1

In my datagrid The parameter OnItemCreated is
OnItemCreated="my009DataGrid_ItemCreated"
(see part 2)

part 1/ My code file in my .vb file
------------------------------------


Sub my009DataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Header Then

'ImageButton Header column : e.Item.Cells(0)

Dim myLibEve As New Label
With myLibEve
.Id="myLibEve"
.Text=myLocalization.EasyResourceManager.myGetString("myLibEve","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'ADD Control ImageButton UP LIBEVE.
Dim myBtnLibEveUp As New ImageButton
With myBtnLibEveUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")
.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnLibEveUp"
.CommandArgument="LIBEVE ASC"
AddHandler myBtnLibEveUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'ADD Control ImageButton DOWN LIBEVE.
Dim myBtnLibEveDown As New ImageButton
With myBtnLibEveDown
..ImageUrl="/img/gen/down.gif"
..EnableViewState = ctype("False",Boolean)
..CausesValidation=ctype("False",Boolean)
..Attributes.Add("runat", "server")
..Attributes.Add("onMouseOver", "window.status=' '; return true;")
..AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveDown","patent.aspx")
..Id="myBtnLibEveDown"
..CommandArgument="LIBEVE DESC"
AddHandler myBtnLibEveDown.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Add controls in e.Item.Cells(0)
e.Item.Cells(0).Controls.Add(myLibEve)
e.Item.Cells(0).Controls.Add(myBtnLibEveUp)
e.Item.Cells(0).Controls.Add(myBtnLibEveDown)


myLibEve.Dispose()
myLibEve=Nothing
myBtnLibEveUp.Dispose()
myBtnLibEveUp=Nothing
myBtnLibEveDown.Dispose()
myBtnLibEveDown=Nothing

'Texte de la Colonne Header : e.Item.Cells(1)
'Label Colonne DATECH.
Dim myDatEch As New Label
With myDatEch
.Id="myDatEch"
.Text=myLocalization.EasyResourceManager.myGetString("myDatEch","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'Ajout Control ImageButton UP DATECH.
Dim myBtnDatEchUp As New ImageButton
With myBtnDatEchUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")
.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnDatEchUp"
.CommandArgument="DATECH ASC"
AddHandler myBtnDatEchUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Ajout Control ImageButton DOWN DATECH.
Dim myBtnDatEchDown As New ImageButton
With myBtnDatEchDown
.ImageUrl="/img/gen/down.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")
.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveDown","patent.aspx")
.Id="myBtnDatEchDown"
.CommandArgument="DATECH DESC"
AddHandler myBtnDatEchDown.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Ajout Effectif dans e.Item.Cells(0)
e.Item.Cells(1).Controls.Add(myDatEch)
e.Item.Cells(1).Controls.Add(myBtnDatEchUp)
e.Item.Cells(1).Controls.Add(myBtnDatEchDown)

'Libération Mémoire des trois Controls
myDatEch.Dispose()
myDatEch=Nothing
myBtnDatEchUp.Dispose()
myBtnDatEchUp=Nothing
myBtnDatEchDown.Dispose()
myBtnDatEchDown=Nothing

'Texte de la Colonne Header : e.Item.Cells(2)
'Label Colonne NATURE.
Dim myNature As New Label
With myNature
.Id="myNature"
.Text=myLocalization.EasyResourceManager.myGetString("myNature","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'Ajout Effectif dans e.Item.Cells(0)
e.Item.Cells(2).Controls.Add(myNature)

'Libération Mémoire des trois Controls
myNature.Dispose()
myNature=Nothing

End If '.Item.ItemType = ListItemType.Header

End Sub 'my009DataGrid_ItemCreated


part 2/ In .aspx file : My datagrid with OnItemCreated parameter

<asp:DataGrid id="my009DataGrid"
runat="server"
FooterStyle-BorderWidth="0"
ShowFooter="False"
FooterStyle-BackColor="#F29168"
FooterStyle-ForeColor="#FFFFFF"
FooterStyle-BorderStyle= "None"
PagerStyle-Mode="NumericPages"
PageCount="1"
AutoGenerateColumns="False"
borderwidth="1"
bordercolor="black"
BorderStyle="Solid"
PagerStyle-VerticalAlign="top"
Cellpadding="1"
Cellspacing="0"
ShowHeader="True"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="False"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-VerticalAlign="Middle"
AllowSorting="False"
AllowPaging="True"
OnPageIndexChanged="PageIndexChanged_OnClick"
OnItemCreated="my009DataGrid_ItemCreated"
PageSize="5"
width="100%"
HeaderStyle-BackColor="#93b0cc"
BackColor="#e5ecf2"
AlternatingItemStyle-BackColor="#e5ecf2"
PagerStyle-Backcolor="#93b0cc"
PagerStyle-Forecolor="Black"
PagerStyle-HorizontalAlign="Right"
PagerStyle-BorderStyle="Inset">

<Columns>
<asp:BoundColumn DataField="libeve" ReadOnly="true">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Left" VerticalAlign="Middle" width="40%">
</ItemStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField="datech" DataFormatString="{0:d}"
ReadOnly="true">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Center" VerticalAlign="Middle" width="30%">
</ItemStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField="nature" ReadOnly="true" HeaderText="Nature">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Center" VerticalAlign="Middle" width="30%">
</ItemStyle>
</asp:BoundColumn>
</Columns>

</asp:DataGrid>



thanks for your help
 
L

Lloyd Sheen

Fabrice said:
Sorry. This is my code :

In my aspx file, I have a datagrid with the "OnItemCreated" parameter.
In my code behind (VB file), I have built all the buttons by code
(ImageButton).

The Handler on the differents ImageButtons seems not be fire under .net
2.0
It is ok under 1.1

In my datagrid The parameter OnItemCreated is
OnItemCreated="my009DataGrid_ItemCreated"
(see part 2)

part 1/ My code file in my .vb file
------------------------------------


Sub my009DataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Header Then

'ImageButton Header column : e.Item.Cells(0)

Dim myLibEve As New Label
With myLibEve
.Id="myLibEve"

.Text=myLocalization.EasyResourceManager.myGetString("myLibEve","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'ADD Control ImageButton UP LIBEVE.
Dim myBtnLibEveUp As New ImageButton
With myBtnLibEveUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")

.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnLibEveUp"
.CommandArgument="LIBEVE ASC"
AddHandler myBtnLibEveUp.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'ADD Control ImageButton DOWN LIBEVE.
Dim myBtnLibEveDown As New ImageButton
With myBtnLibEveDown
.ImageUrl="/img/gen/down.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return true;")
.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveDown","patent.aspx")
.Id="myBtnLibEveDown"
.CommandArgument="LIBEVE DESC"
AddHandler myBtnLibEveDown.Command, New CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Add controls in e.Item.Cells(0)
e.Item.Cells(0).Controls.Add(myLibEve)
e.Item.Cells(0).Controls.Add(myBtnLibEveUp)
e.Item.Cells(0).Controls.Add(myBtnLibEveDown)


myLibEve.Dispose()
myLibEve=Nothing
myBtnLibEveUp.Dispose()
myBtnLibEveUp=Nothing
myBtnLibEveDown.Dispose()
myBtnLibEveDown=Nothing

'Texte de la Colonne Header : e.Item.Cells(1)
'Label Colonne DATECH.
Dim myDatEch As New Label
With myDatEch
.Id="myDatEch"

.Text=myLocalization.EasyResourceManager.myGetString("myDatEch","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'Ajout Control ImageButton UP DATECH.
Dim myBtnDatEchUp As New ImageButton
With myBtnDatEchUp
.ImageUrl="/img/gen/up.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return
true;")

.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveUp","patent.aspx")
.Id="myBtnDatEchUp"
.CommandArgument="DATECH ASC"
AddHandler myBtnDatEchUp.Command, New
CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Ajout Control ImageButton DOWN DATECH.
Dim myBtnDatEchDown As New ImageButton
With myBtnDatEchDown
.ImageUrl="/img/gen/down.gif"
.EnableViewState = ctype("False",Boolean)
.CausesValidation=ctype("False",Boolean)
.Attributes.Add("runat", "server")
.Attributes.Add("onMouseOver", "window.status=' '; return
true;")

.AlternateText=myLocalization.EasyResourceManager.myGetString("BtnLibEveDown","patent.aspx")
.Id="myBtnDatEchDown"
.CommandArgument="DATECH DESC"
AddHandler myBtnDatEchDown.Command, New
CommandEventHandler(AddressOf
myImgButtonSorting)
End With

'Ajout Effectif dans e.Item.Cells(0)
e.Item.Cells(1).Controls.Add(myDatEch)
e.Item.Cells(1).Controls.Add(myBtnDatEchUp)
e.Item.Cells(1).Controls.Add(myBtnDatEchDown)

'Libération Mémoire des trois Controls
myDatEch.Dispose()
myDatEch=Nothing
myBtnDatEchUp.Dispose()
myBtnDatEchUp=Nothing
myBtnDatEchDown.Dispose()
myBtnDatEchDown=Nothing

'Texte de la Colonne Header : e.Item.Cells(2)
'Label Colonne NATURE.
Dim myNature As New Label
With myNature
.Id="myNature"

.Text=myLocalization.EasyResourceManager.myGetString("myNature","patent.aspx")
.EnableViewState = ctype("False",Boolean)
.CssClass="txtOres2"
.Attributes.Add("runat", "server")
End With

'Ajout Effectif dans e.Item.Cells(0)
e.Item.Cells(2).Controls.Add(myNature)

'Libération Mémoire des trois Controls
myNature.Dispose()
myNature=Nothing

End If '.Item.ItemType = ListItemType.Header

End Sub 'my009DataGrid_ItemCreated


part 2/ In .aspx file : My datagrid with OnItemCreated parameter

<asp:DataGrid id="my009DataGrid"
runat="server"
FooterStyle-BorderWidth="0"
ShowFooter="False"
FooterStyle-BackColor="#F29168"
FooterStyle-ForeColor="#FFFFFF"
FooterStyle-BorderStyle= "None"
PagerStyle-Mode="NumericPages"
PageCount="1"
AutoGenerateColumns="False"
borderwidth="1"
bordercolor="black"
BorderStyle="Solid"
PagerStyle-VerticalAlign="top"
Cellpadding="1"
Cellspacing="0"
ShowHeader="True"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="False"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-VerticalAlign="Middle"
AllowSorting="False"
AllowPaging="True"
OnPageIndexChanged="PageIndexChanged_OnClick"
OnItemCreated="my009DataGrid_ItemCreated"
PageSize="5"
width="100%"
HeaderStyle-BackColor="#93b0cc"
BackColor="#e5ecf2"
AlternatingItemStyle-BackColor="#e5ecf2"
PagerStyle-Backcolor="#93b0cc"
PagerStyle-Forecolor="Black"
PagerStyle-HorizontalAlign="Right"
PagerStyle-BorderStyle="Inset">

<Columns>
<asp:BoundColumn DataField="libeve" ReadOnly="true">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Left" VerticalAlign="Middle" width="40%">
</ItemStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField="datech" DataFormatString="{0:d}"
ReadOnly="true">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Center" VerticalAlign="Middle"
width="30%">
</ItemStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField="nature" ReadOnly="true" HeaderText="Nature">
<ItemStyle Font-Names="Times New Roman, Times, serif" ForeColor="#003B6F"
Font-Size="10" horizontalalign="Center" VerticalAlign="Middle"
width="30%">
</ItemStyle>
</asp:BoundColumn>
</Columns>

</asp:DataGrid>



thanks for your help

I wonder why you are doing the dispose of the controls you create, I would
think that one of the things that a dispose would do is remove all handlers.

Hope this helps
LS
 
F

Fabrice

Hello Lloyd

Your are right. I removed the "dispose" of the controls I have created.
And It works. I don't understand the difference of behavior between
framework 1.1 and 2.0.

If your can explain me.

Thanks
fabrice
 
L

Lloyd Sheen

Fabrice said:
Hello Lloyd

Your are right. I removed the "dispose" of the controls I have created.
And It works. I don't understand the difference of behavior between
framework 1.1 and 2.0.

If your can explain me.

Thanks
fabrice

Don't take this a gospel but it looks like in 1.1 the dispose did not remove
the handler. That is only conjecture but I am glad that fixed the problem.

LS
 
K

kaushik

thanks

Lloyd Sheen said:
Don't take this a gospel but it looks like in 1.1 the dispose did not
remove the handler. That is only conjecture but I am glad that fixed the
problem.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top