Capture Click Event Problem :-(

G

Guest

Hi

I am trying to create a list dynamically as follow

Private conn As New System.Data.SqlClient.SqlConnectio
Private WithEvents htm As System.Web.UI.HtmlControls.HtmlAncho
Protected WithEvents tbl As System.Web.UI.WebControls.Tabl

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
'If Not Page.IsPostBack The
GetReports()
'End I
End Su

Private Sub htm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles htm.ServerClic
Response.Write("You clicked on: " & CStr(CType(sender, System.Web.UI.HtmlControls.HtmlAnchor).Name)
End Su

Private Sub GetReports(
Dim cmd As New System.Data.SqlClient.SqlComman
Dim _rdr As System.Data.SqlClient.SqlDataReade
InitializeConnection(
Tr
cmd.CommandText = "GetReports
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandTimeout = 36
cmd.Connection = con

If conn.State = ConnectionState.Open Then conn.Close(
conn.Open(

_rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection

Dim r As TableRow = New TableRo
Dim c As TableCell = New TableCel
Dim btn1 As New System.Web.UI.HtmlControls.HtmlButto

Do While _rdr.Rea
Dim img As New System.Web.UI.WebControls.Imag
Dim htm1 As New System.Web.UI.HtmlControls.HtmlAncho

Controls.Add(htm1

img.ImageUrl = "http://portal/dev/ReportsImages/navlink.gif
htm1.Name = _rdr.Item("DescriptionRefID"
htm1.InnerText = _rdr.Item("Description"
htm1.Attributes.Add("class", "Normal"
htm1.HRef = "WebForm1.aspx

c.Controls.Add(img
c.Controls.Add(New LiteralControl(" ")
c.Controls.Add(htm1
c.Controls.Add(New LiteralControl("<br>")
c.Height = Unit.Pixel(17
r.Cells.Add(c
tbl.Rows.Add(r

AddHandler htm1.ServerClick, AddressOf htm_Clic
Loo
Catch ex As Exceptio
If Not _rdr.IsClosed Then _rdr.Close(
If conn.State = ConnectionState.Open Then conn.Close(
Throw New Exceptio
Finall
If Not _rdr.IsClosed Then _rdr.Close(
If conn.State = ConnectionState.Open Then conn.Close(
End Tr
End Su

'--// Set up the Connectio
Private Sub InitializeConnection(
conn = New System.Data.SqlClient.SqlConnection(strConnect()
End Su

Private Function strConnect() As Strin
Dim m_strConn As System.Strin
m_strConn = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"
Return m_strCon
End Functio

But whenever I uncomment the IsPostBack in my load event so that GetReport function doesn't get called everytime the page is reloaded it does not capture the click event of my HTMLAnchor click event!

Can you tell me why and how to make this work without having me to call the GetReport every time the page is refreshed

Thanks

Yama
 
N

Natty Gur

Hi,

In order to receive dynamic control events or viewsate you need to
recreate them any time the page load, even when post back occurred. Your
problem is more architectural. You merge data retrieval code with
presentation code. Good design practice is to separate between
application Data Access Layer, application Business Logic and
application presentation. Working this way you will have DLL that return
data from DB. Your page will get data from DB and create those dynamic
controls. If you want to prevent your page from accessing DB to get data
you can find a way to preserve DB results returning from DAL and use
them on postback.

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
Y

Yama

Hello Natty,

Thanks for your reply... I wonder why we need to reload the control events
or viewstate everytime the page is reloaded! As far as the data access layer
I am very much aware of it and I just wanted to present my problem in its
full context rather than just trying to use words to describe my problem. I
did thing it would be this picky when it came to preserving the viewstates
when control events were created dynamically. I guess this could be an
improvement to-do list for Microsoft...

Yama
 

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