Anyone else sick to death of examples that just don't work?

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
http://www.123aspx.com/redir.aspx?res=28124

You see that same example all over the Internet

That example does NOT work. You can click the button until your fingernail drops off, and it won't raise an event

Why? Because for whatever reason, there is something in ASP.Net that doesn't let you raise events using the event handler

The CORRECT method is to do this:

Code:
 - in the Page load:

Dim sID As String = Me.UniqueID
Dim but As New Button

Page.ClientScript.RegisterHiddenField(sID & _
               "_buttonClicked", "")

 
but.Attributes.Add("onClick", _
               "document.forms[0]." & sID & _
               "_buttonClicked.value='" & _
               but.UniqueID & "';document.forms[0].submit();")

If Not Page.IsPostBack Then
    If Request.Item(Me.UniqueID & _
              "_buttonClicked").Length > 0 Then
                Response.Write("Button " & _
                   Request.Item(Me.UniqueID & _
                      "_buttonClicked") & _
                      " was clicked!")

    End If

End If

Now you have postback and now you can work with the item, and add features like a page.redirect using Java.

Of course, it takes 2 days to find an example like that because you have to wade through all of the other geniuses who read Microsoft's "I sure wish it worked like we designed it" manual and then post the solution like it was their own without testing it.

Ok - there. I vented. Carry on
 
Back
Top