ServerClick event on a generated HtmlInputImage

S

schleichv

Hello,

I am attempting to get the ServerClick event to fire on a dynamically
generated HtmlInputImage and having no luck at all. I've looked over
many items on the forums but none seem to cover what seems to be
happening to me.

Below is a small test I have put together to demonstrate the issue.
The form has an HtmlInputImage on it that has it's ServerClick event
wired in the code. Another HtmlInputImage is generated in the code and
also has it's ServerClick event wired.

The control that has been defined in the HTML works and the one
generated in code does not.

Thanks in advance for any help.

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="TestPage.aspx.vb" Inherits="EngAppTracking.TestPage"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>TestPage</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input id="inImage1" type="image" src="xxx.jpg" runat="server" >
</form>
</body>
</HTML>

Public Class TestPage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents inImage1 As
System.Web.UI.HtmlControls.HtmlInputImage

Protected WithEvents inImage2 As
System.Web.UI.HtmlControls.HtmlInputImage

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'context.Session(SessionVariables.IsAdminModeOn) = True
End Sub

Protected Sub HandleImputImageControls()
AddHandler Me.inImage1.ServerClick, AddressOf
Me.inImage1_ServerClick

inImage2 = New HtmlInputImage
inImage2.ID = "inImage2"
inImage2.Src = "xxx.jpg"
inImage2.Attributes.Add("runat", "server")
AddHandler inImage2.ServerClick, AddressOf
Me.inImage2_ServerClick

End Sub

Private Sub inImage1_ServerClick(ByVal sender As System.Object,
ByVal e As System.Web.UI.ImageClickEventArgs)
Response.Write("inImage1 clicked")
End Sub

Private Sub inImage2_ServerClick(ByVal sender As System.Object,
ByVal e As System.Web.UI.ImageClickEventArgs)
Response.Write("inImage2 clicked")
End Sub

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
HandleImputImageControls()

End Sub

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)

Dim MyForm As HtmlForm
MyForm = Me.FindControl("Form1")

MyForm.Controls.Add(inImage2)

MyForm.RenderControl(writer)
End Sub
End Class
 
G

Guest

Hi schleichv, You didn't add a hook with the Page.GetPostBackClientEvent
method
note you should add the control to the controls collection before calling
Page.GetPostBackClientEvent




try the following:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here


' Html input image
Dim imButton As New HtmlInputImage
With imButton
.Src = "~/images/image.gif"
End With
Me.FindControl("Form1").Controls.Add(imButton)
imButton.Attributes.Add("onclick",
Page.GetPostBackClientEvent(imButton, ""))
AddHandler imButton.ServerClick, AddressOf HandleHtmlInputImageClick


' asp ImageButton
Dim aspImButton As New ImageButton
With aspImButton
.ImageUrl = "~/images/image.gif"
End With
Me.FindControl("Form1").Controls.Add(aspImButton)
AddHandler aspImButton.Click, AddressOf HandleAspImageButtonClick

End Sub

Private Sub HandleHtmlInputImageClick(ByVal o As Object, ByVal e As
ImageClickEventArgs)
Response.Write("html input image clicked " & Now.ToLongTimeString)
End Sub

Private Sub HandleAspImageButtonClick(ByVal o As Object, ByVal e As
ImageClickEventArgs)
Response.Write("asp ImageButton clicked " & Now.ToLongTimeString)
End Sub
 
G

Guest

Hi schleichv, ignore my last post, I think all you have to do is add your
control to the controls collection before the page loads viewstate/ post
data, currently you are adding it in the render method.. jd
 
V

Virgil Schleich

I changed the HandleImputImageControls method to which added the
control to the form, changed the call to be in the OnInit event and
removed the override of the render event. The code not throws a
StackOverFlowException. I also tried calling HandleImputImageControls
from the Page_Load and had the same issue. Any suggestions?

Public Class TestPage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents inImage1 As
System.Web.UI.HtmlControls.HtmlInputImage

Protected WithEvents inImage2 As
System.Web.UI.HtmlControls.HtmlInputImage

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'context.Session(SessionVariables.IsAdminModeOn) = True

End Sub

Protected Sub HandleImputImageControls()
Dim MyForm As HtmlForm
MyForm = Me.FindControl("Form1")

AddHandler Me.inImage1.ServerClick, AddressOf
Me.inImage1_ServerClick

inImage2 = New HtmlInputImage
inImage2.ID = "inImage2"
inImage2.Src = "xxx.jpg"
inImage2.Attributes.Add("runat", "server")
AddHandler Me.inImage2.ServerClick, AddressOf
Me.inImage2_ServerClick

MyForm.Controls.Add(MyForm)

End Sub

Private Sub inImage1_ServerClick(ByVal sender As System.Object,
ByVal e As System.Web.UI.ImageClickEventArgs)
Response.Write("inImage1 clicked")
End Sub

Private Sub inImage2_ServerClick(ByVal sender As System.Object,
ByVal e As System.Web.UI.ImageClickEventArgs)
Response.Write("inImage2 clicked")
End Sub

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
HandleImputImageControls()

End Sub

'Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)

' Dim MyForm As HtmlForm
' MyForm = Me.FindControl("Form1")

' 'MyForm.Controls.Add(inImage2)

' MyForm.RenderControl(writer)
'End Sub
End Class
 

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

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top