Adding Event Handler to Custom Web Control

microkarl

Regular
Joined
Apr 22, 2004
Messages
88
Location
Morristown, NJ
All,
I have this modified dropdownlist that the user can key in whatever inside the list. But it has a problem, it's SelectedIndexChanged Event in the code behind won't fired, can someone help me on that?

Code:
Imports System
Imports System.Web.UI.WebControls

Public Class KeySortDropDownList
    Inherits System.Web.UI.WebControls.DropDownList

    Private Shared functionName As String = "KeySortDropDownList_onkeypress"
    Private _caseSensitiveKeySort As Boolean = False

    Public Overridable Property CaseSensitiveKeySort() As Boolean
        Get
            Return _caseSensitiveKeySort
        End Get
        Set(ByVal Value As Boolean)
            _caseSensitiveKeySort = Value
        End Set
    End Property

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Dim script As String
        script = "<script language=" & Chr(34) & "javascript" & Chr(34) & " type=" & Chr(34) & "text/javascript" & Chr(34) & ">" & vbCrLf
        script &= "function " & functionName & " (dropdownlist,caseSensitive) {" & vbCrLf
        script &= "  // check the keypressBuffer attribute is defined on the dropdownlist" & vbCrLf
        script &= "  var undefined; " & vbCrLf
        script &= "  if (dropdownlist.keypressBuffer == undefined) { " & vbCrLf
        script &= "    dropdownlist.keypressBuffer = ''; " & vbCrLf
        script &= "  } " & vbCrLf
        script &= "  // get the key that was pressed " & vbCrLf
        script &= "  var key = String.fromCharCode(window.event.keyCode); " & vbCrLf
        script &= "  dropdownlist.keypressBuffer += key;" & vbCrLf
        script &= "  if (!caseSensitive) {" & vbCrLf
        script &= "    // convert buffer to lowercase" & vbCrLf
        script &= "    dropdownlist.keypressBuffer = dropdownlist.keypressBuffer.toLowerCase();" & vbCrLf
        script &= "  }" & vbCrLf
        script &= "  // find if it is the start of any of the options " & vbCrLf
        script &= "  var optionsLength = dropdownlist.options.length; " & vbCrLf
        script &= "  for (var n=0; n < optionsLength; n++) { " & vbCrLf
        script &= "    var optionText = dropdownlist.options[n].text; " & vbCrLf
        script &= "    if (!caseSensitive) {" & vbCrLf
        script &= "      optionText = optionText.toLowerCase();" & vbCrLf
        script &= "    }" & vbCrLf
        script &= "    if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0) { " & vbCrLf
        script &= "      dropdownlist.selectedIndex = n; " & vbCrLf
        script &= "      return false; // cancel the default behavior since " & vbCrLf
        script &= "                    // we have selected our own value " & vbCrLf
        script &= "    } " & vbCrLf
        script &= "  } " & vbCrLf
        script &= "  // reset initial key to be inline with default behavior " & vbCrLf
        script &= "  dropdownlist.keypressBuffer = key; " & vbCrLf
        script &= "  return true; // give default behavior " & vbCrLf
        script &= "} " & vbCrLf
        script &= "</script>"

        ' register the client-side script block
        Me.Page.RegisterClientScriptBlock(functionName, script)
        ' add to the onkeypress event
        Me.Attributes.Add("onkeypress", "return " + functionName + "(this," + _caseSensitiveKeySort.ToString().ToLower() + ")")
    End Sub

End Class
 
what the heck are you doing? Isn't this the default behavior for drop down lists anyway??

You didn't override OnSelectedIndexChanged.
You have to override the event and set postback = true for the drop down list.
 
Back
Top