Solved

client-side set focus event overridden?

Posted on 2006-11-07
4
348 Views
Last Modified: 2012-05-05
Hi folks,

I have a small asp.net datagrid application that allows the user to either edit or add new records.  When the user selects either the edit function or add function, the application runs a small sub that generates a small client-side script to set the focus to a certain control.  

The edit function is selected by clicking the EDIT column link, the new function is selected by clicking a large NEW button on the form.   Both the edit and new subsections run the same Sub SetFocus routine at the end.  It works for the edit function but does not for the NEW function.    I believe the client-side script runs properly when the new button is click, but for some reason, the focus is immediately removed from the control and placed back on the NEW button itself.

Any help would be appreciated.  (I can give any respondent, the web address of the application to see the problem in live action.)

My entired code-behind page is shown below.   The relevant sections are:

Private Sub ButtonNew_Command   (This is the code that runs when the user clicks the NEW button)

Private Sub DataGridHolidayGifts_EditCommand   (This section does the EDIT function)

Private Sub SetFocus()   (This section is called from the other two sections and generates the client-side script to set focus)


Thanks.

-------------------------------------------------------------
Option Explicit On
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Text.RegularExpressions


Public Class Summary
    Inherits System.Web.UI.Page
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents label_cuser As System.Web.UI.WebControls.Label
    Protected WithEvents DataGridHolidayGifts As System.Web.UI.WebControls.DataGrid
    Protected WithEvents SqlConnectionSurveys As System.Data.SqlClient.SqlConnection
    Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlDataAdapterHolidayGifts As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DataSetHolidayGifts As holidaygift.DataSetHolidayGifts
    Protected WithEvents ButtonNew As System.Web.UI.WebControls.Button
    Protected WithEvents SqlCommandLists As System.Data.SqlClient.SqlCommand
    Protected WithEvents DropDownListList As System.Web.UI.WebControls.DropDownList
    Protected WithEvents SqlDataAdapterLists As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DataSetLists As holidaygift.DataSetLists
    Protected WithEvents SqlCommandHolidayGifts As System.Data.SqlClient.SqlCommand
    Protected WithEvents TextBoxHCC As System.Web.UI.WebControls.TextBox
    Protected WithEvents LabelHCC As System.Web.UI.WebControls.Label
    Protected WithEvents SqlCommandCardLookup As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlDataAdapterCardLookup As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DataSetCardLookup As holidaygift.DataSetCardLookup
    Protected WithEvents LinkButtonUpdate As System.Web.UI.WebControls.LinkButton

    Public FilterListID As Integer
    Public SortSql As String


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.SqlCommandHolidayGifts = New System.Data.SqlClient.SqlCommand
        Me.SqlConnectionSurveys = New System.Data.SqlClient.SqlConnection
        Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlDataAdapterHolidayGifts = New System.Data.SqlClient.SqlDataAdapter
        Me.DataSetHolidayGifts = New holidaygift.DataSetHolidayGifts
        Me.SqlCommandLists = New System.Data.SqlClient.SqlCommand
        Me.SqlDataAdapterLists = New System.Data.SqlClient.SqlDataAdapter
        Me.DataSetLists = New holidaygift.DataSetLists
        Me.SqlCommandCardLookup = New System.Data.SqlClient.SqlCommand
        Me.SqlDataAdapterCardLookup = New System.Data.SqlClient.SqlDataAdapter
        Me.DataSetCardLookup = New holidaygift.DataSetCardLookup
        CType(Me.DataSetHolidayGifts, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataSetLists, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataSetCardLookup, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'SqlCommandHolidayGifts
        '
        Me.SqlCommandHolidayGifts.CommandText = "SELECT dbo.holidaygifts.*, listdir.LName + N', ' + listdir.FName AS username, dbo" & _
        ".holidaygifts.userid AS Expr1 FROM dbo.holidaygifts LEFT OUTER JOIN GENERAL.dbo." & _
        "directory listdir ON dbo.holidaygifts.userid = listdir.[Key] WHERE (dbo.holidayg" & _
        "ifts.userid BETWEEN @start AND @end) ORDER BY listdir.LName + N', ' + listdir.FN" & _
        "ame, dbo.holidaygifts.clast, dbo.holidaygifts.cfirst"
        Me.SqlCommandHolidayGifts.Connection = Me.SqlConnectionSurveys
        Me.SqlCommandHolidayGifts.Parameters.Add(New System.Data.SqlClient.SqlParameter("@start", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Expr1", System.Data.DataRowVersion.Current, "1"))
        Me.SqlCommandHolidayGifts.Parameters.Add(New System.Data.SqlClient.SqlParameter("@end", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Expr1", System.Data.DataRowVersion.Current, "1"))
        '
        'SqlConnectionSurveys
        '
        Me.SqlConnectionSurveys.ConnectionString = "workstation id=MSIDESXP;packet size=4096;user id=msides;data source=""207.190.242." & _
        "134"";persist security info=True;initial catalog=Surveys;password=infinityplus1"
        '
        'SqlDataAdapterHolidayGifts
        '
        Me.SqlDataAdapterHolidayGifts.DeleteCommand = Me.SqlDeleteCommand1
        Me.SqlDataAdapterHolidayGifts.InsertCommand = Me.SqlInsertCommand1
        Me.SqlDataAdapterHolidayGifts.SelectCommand = Me.SqlCommandHolidayGifts
        Me.SqlDataAdapterHolidayGifts.UpdateCommand = Me.SqlUpdateCommand1
        '
        'DataSetHolidayGifts
        '
        Me.DataSetHolidayGifts.DataSetName = "DataSetHolidayGifts"
        Me.DataSetHolidayGifts.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'SqlCommandLists
        '
        Me.SqlCommandLists.CommandText = "SELECT LName + N', ' + FName AS name, [Key], Title, dot FROM GENERAL.dbo.director" & _
        "y WHERE (Title = N'associate') AND (dot IS NULL) OR (Title = N'partner') AND (do" & _
        "t IS NULL) OR (Title = N'counsel') AND (dot IS NULL) ORDER BY LName + N', ' + FN" & _
        "ame"
        Me.SqlCommandLists.Connection = Me.SqlConnectionSurveys
        '
        'SqlDataAdapterLists
        '
        Me.SqlDataAdapterLists.SelectCommand = Me.SqlCommandLists
        '
        'DataSetLists
        '
        Me.DataSetLists.DataSetName = "DataSetLists"
        Me.DataSetLists.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'SqlCommandCardLookup
        '
        Me.SqlCommandCardLookup.CommandText = "SELECT holidaycardcount FROM GENERAL.dbo.directory WHERE ([Key] = @userid)"
        Me.SqlCommandCardLookup.Connection = Me.SqlConnectionSurveys
        Me.SqlCommandCardLookup.Parameters.Add(New System.Data.SqlClient.SqlParameter("@userid", System.Data.SqlDbType.Int, 4, "Key"))
        '
        'SqlDataAdapterCardLookup
        '
        Me.SqlDataAdapterCardLookup.SelectCommand = Me.SqlCommandCardLookup
        '
        'DataSetCardLookup
        '
        Me.DataSetCardLookup.DataSetName = "DataSetCardLookup"
        Me.DataSetCardLookup.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DataSetHolidayGifts, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataSetLists, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataSetCardLookup, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

    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 Property SortExpression() As String
        Get
            Dim o As Object = viewstate("SortExpression")
            If o Is Nothing Then
                Return String.Empty
            Else
                Return o.ToString
            End If
        End Get
        Set(ByVal Value As String)
            viewstate("SortExpression") = Value
        End Set
    End Property

    Private Property SortAscending() As Boolean
        Get
            Dim o As Object = viewstate("SortAscending")
            If o Is Nothing Then
                Return True
            Else
                Return Convert.ToBoolean(o)
            End If
        End Get
        Set(ByVal Value As Boolean)
            viewstate("SortAscending") = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s As String
        'gets the username from the server and sets it to the cuser variable and the name label
        If Not IsPostBack Then
            SortSql = SqlDataAdapterHolidayGifts.SelectCommand.CommandText
            'fill list datagridperiod filter option


            SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)

            cuser = Request.ServerVariables("logon_user")
            cuser = Mid(cuser, InStr(cuser, "\") + 1, 100)
            getuserinfo(cuser)
            Session("cuser") = cuser
            Session("cuser_id") = cuser_id
            Session("cuser_fullname") = cuser_fullname

            label_cuser.Text = ""
            SqlDataAdapterLists.Fill(DataSetLists)
            viewstate("SortExpression") = "username"
            viewstate("SortAscending") = True
            UpdateColumnHeaders(DataGridHolidayGifts)
            Page.DataBind()
            If cuser = "msides" Or cuser = "gjohnson" Or cuser = "pengel" Then
                DropDownListList.Items.Insert(0, New ListItem("full list", 1))
            End If
            DropDownListList.Items.Insert(0, New ListItem("select", 0))
        Else
        End If
        FilterListID = DropDownListList.SelectedValue
    End Sub

    Private Sub getuserinfo(ByVal cuser As String)
        Dim sqlConn As SqlConnection
        Dim sqlCmd As SqlCommand
        Dim StrConnection As String
        Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter
        Dim dataset As DataSet = New DataSet

        Try
            'Get connection string from Web.Config
            StrConnection = ConfigurationSettings.AppSettings("GeneralConnectionString")
            'set up connection
            sqlConn = New SqlConnection(StrConnection)
            'set up command for connection
            sqlCmd = New SqlCommand("Select * from directory where email='" + cuser + "'", sqlConn)
            'assign the command results to a data adapter
            sqlAdapter.SelectCommand = sqlCmd
            'assign the data adapter to a dataset
            sqlAdapter.SelectCommand.Connection.Open()
            'assign the data to a dataset and give the table a name
            sqlAdapter.Fill(dataset, "user")
            cuser_id = dataset.Tables("user").Rows(0).Item("Key")
            cuser_fullname = dataset.Tables("user").Rows(0).Item("Fullname")
        Catch ex As Exception
            Response.Write(ex.ToString & "
")
        Finally
            sqlConn.Close()
        End Try

    End Sub

    Private Sub ButtonNew_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles ButtonNew.Command
        Dim wuserid, wdtentered, wenteredby As String
        wuserid = DropDownListList.SelectedValue
        wdtentered = Now()
        wenteredby = cuser_id

        Dim sql As String = _
          "INSERT into surveys.dbo.holidaygifts " & _
          " (userid, dtentered, enteredby) " & _
          "VALUES " & _
          " ('" & wuserid & "', '" & wdtentered & "', '" & wenteredby & "')"

        'update the review record itself
        Dim Sqlcommand As SqlCommand = New SqlCommand(sql, SqlConnectionSurveys)
        Sqlcommand.CommandType = CommandType.Text

        SqlConnectionSurveys.Open()
        Sqlcommand.ExecuteNonQuery()

        'refill the grid
        ResetFilter()
        ResetSort()
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        DataGridHolidayGifts.EditItemIndex = 0
        DataGridHolidayGifts.DataBind()
        DataGridHolidayGifts.Columns(19).Visible = True

        SetFocus()
    End Sub

    Private Sub DataGridHolidayGifts_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridHolidayGifts.EditCommand
        'refill the grid
        ResetFilter()
        ResetSort()
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)

        DataGridHolidayGifts.EditItemIndex = e.Item.ItemIndex
        DataGridHolidayGifts.DataBind()
        DataGridHolidayGifts.Columns(19).Visible = True

        SetFocus()
    End Sub



-------------------------------------------------------------------------------------------------------------------
'!!!!!!!!!!!!This is the code that generates the script!!

    Private Sub SetFocus()
        Dim descTB As TextBox
        descTB = DataGridHolidayGifts.Items(DataGridHolidayGifts.EditItemIndex).Cells(3).FindControl("clast")

        Dim script As String
        script = "<script language=""vbscript"">" & vbCrLf & _
                 "document.Form1." & descTB.ClientID & ".focus()" & vbCrLf & _
                 "</script>" & vbCrLf
        RegisterStartupScript("setfocus", script)
    End Sub

    Private Sub DataGridHolidayGifts_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridHolidayGifts.UpdateCommand
        Dim wid, wuserid, wdtentered, wenteredby, wclast, wcfirst, wctitle, wccompany, wcadd1, wcadd2, wccity, wcstate, wczip, wccountry, wcphone, wsigids As String
        wclast = CType(e.Item.Cells(3).Controls(1), TextBox).Text
        wcfirst = CType(e.Item.Cells(4).Controls(0), TextBox).Text
        wctitle = CType(e.Item.Cells(5).Controls(0), TextBox).Text
        wccompany = CType(e.Item.Cells(6).Controls(0), TextBox).Text
        wcadd1 = CType(e.Item.Cells(7).Controls(0), TextBox).Text
        wcadd2 = CType(e.Item.Cells(8).Controls(0), TextBox).Text
        wccity = CType(e.Item.Cells(9).Controls(0), TextBox).Text
        wcstate = CType(e.Item.Cells(10).Controls(0), TextBox).Text
        wczip = CType(e.Item.Cells(11).Controls(0), TextBox).Text
        wccountry = CType(e.Item.Cells(12).Controls(0), TextBox).Text
        wcphone = CType(e.Item.Cells(13).Controls(0), TextBox).Text
        wsigids = CType(e.Item.Cells(14).Controls(0), TextBox).Text
        wid = e.Item.Cells(15).Text
        wuserid = e.Item.Cells(16).Text
        wdtentered = Now()
        wenteredby = cuser_id

        Dim sql As String = _
           "UPDATE surveys.dbo.holidaygifts " & _
           "SET " & _
           "    clast = @clast, " & _
           "    cfirst = @cfirst, " & _
           "    ctitle = @ctitle, " & _
           "    ccompany = @ccompany, " & _
           "    cadd1 = @cadd1, " & _
           "    cadd2 = @cadd2, " & _
           "    ccity = @ccity, " & _
           "    cstate = @cstate, " & _
           "    czip = @czip, " & _
           "    ccountry = @ccountry, " & _
           "    cphone = @cphone, " & _
           "    sigids = @sigids, " & _
           "    userid = @userid, " & _
           "    dtentered = @dtentered, " & _
           "    enteredby = @enteredby " & _
           "WHERE id = @id"

        SqlConnectionSurveys.Open()

        'update the review record itself
        Dim Sqlcommand As SqlCommand = New SqlCommand(sql, SqlConnectionSurveys)
        Sqlcommand.CommandType = CommandType.Text

        Dim parameterclast As SqlParameter = New SqlParameter("@clast", SqlDbType.VarChar)
        parameterclast.Value = wclast
        Sqlcommand.Parameters.Add(parameterclast)

        Dim parametercfirst As SqlParameter = New SqlParameter("@cfirst", SqlDbType.VarChar)
        parametercfirst.Value = wcfirst
        Sqlcommand.Parameters.Add(parametercfirst)

        Dim parameterctitle As SqlParameter = New SqlParameter("@ctitle", SqlDbType.VarChar)
        parameterctitle.Value = wctitle
        Sqlcommand.Parameters.Add(parameterctitle)

        Dim parameterccompany As SqlParameter = New SqlParameter("@ccompany", SqlDbType.VarChar)
        parameterccompany.Value = wccompany
        Sqlcommand.Parameters.Add(parameterccompany)

        Dim parametercadd1 As SqlParameter = New SqlParameter("@cadd1", SqlDbType.VarChar)
        parametercadd1.Value = wcadd1
        Sqlcommand.Parameters.Add(parametercadd1)

        Dim parametercadd2 As SqlParameter = New SqlParameter("@cadd2", SqlDbType.VarChar)
        parametercadd2.Value = wcadd2
        Sqlcommand.Parameters.Add(parametercadd2)

        Dim parameterccity As SqlParameter = New SqlParameter("@ccity", SqlDbType.VarChar)
        parameterccity.Value = wccity
        Sqlcommand.Parameters.Add(parameterccity)

        Dim parametercstate As SqlParameter = New SqlParameter("@cstate", SqlDbType.VarChar)
        parametercstate.Value = wcstate
        Sqlcommand.Parameters.Add(parametercstate)

        Dim parameterczip As SqlParameter = New SqlParameter("@czip", SqlDbType.VarChar)
        parameterczip.Value = wczip
        Sqlcommand.Parameters.Add(parameterczip)

        Dim parameterccountry As SqlParameter = New SqlParameter("@ccountry", SqlDbType.VarChar)
        parameterccountry.Value = wccountry
        Sqlcommand.Parameters.Add(parameterccountry)

        Dim parametercphone As SqlParameter = New SqlParameter("@cphone", SqlDbType.VarChar)
        parametercphone.Value = wcphone
        Sqlcommand.Parameters.Add(parametercphone)

        Dim parametersigids As SqlParameter = New SqlParameter("@sigids", SqlDbType.VarChar)
        parametersigids.Value = wsigids
        Sqlcommand.Parameters.Add(parametersigids)

        Dim parameterID As SqlParameter = New SqlParameter("@id", SqlDbType.Int)
        parameterID.Value = wid
        Sqlcommand.Parameters.Add(parameterID)

        Dim parameteruserid As SqlParameter = New SqlParameter("@userid", SqlDbType.Int)
        parameteruserid.Value = wuserid
        Sqlcommand.Parameters.Add(parameteruserid)

        Dim parameterdtentered As SqlParameter = New SqlParameter("@dtentered", SqlDbType.DateTime)
        parameterdtentered.Value = wdtentered
        Sqlcommand.Parameters.Add(parameterdtentered)

        Dim parameterenteredby As SqlParameter = New SqlParameter("@enteredby", SqlDbType.VarChar)
        parameterenteredby.Value = wenteredby
        Sqlcommand.Parameters.Add(parameterenteredby)

        Sqlcommand.ExecuteNonQuery()

        ResetFilter()
        ResetSort()
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        DataGridHolidayGifts.EditItemIndex = -1
        DataGridHolidayGifts.DataBind()
        DataGridHolidayGifts.Columns(19).Visible = False
    End Sub

    Private Sub DataGridHolidayGifts_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridHolidayGifts.CancelCommand
        'delete if blank

        ResetFilter()
        ResetSort()
        DataGridHolidayGifts.EditItemIndex = -1
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        UpdateColumnHeaders(DataGridHolidayGifts)
        DataGridHolidayGifts.Columns(19).Visible = False
        DataGridHolidayGifts.DataBind()

    End Sub

    Private Sub DataGridHolidayGifts_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGridHolidayGifts.DeleteCommand
        Dim SelectedRecordID As Integer = DataGridHolidayGifts.DataKeys(e.Item.ItemIndex)
        Dim sql As String
        sql = "DELETE from holidaygifts where id=" & SelectedRecordID
        Dim SqlCom As SqlCommand = New SqlCommand(sql, SqlConnectionSurveys)
        SqlConnectionSurveys.Open()
        SqlCom.ExecuteNonQuery()
        SqlConnectionSurveys.Close()

        ResetFilter()
        ResetSort()
        DataGridHolidayGifts.EditItemIndex = -1
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        UpdateColumnHeaders(DataGridHolidayGifts)
        DataGridHolidayGifts.Columns(19).Visible = False
        DataGridHolidayGifts.DataBind()
    End Sub

    Private Sub DropDownListList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownListList.SelectedIndexChanged
        Select Case FilterListID
            Case 0
                viewstate("SortExpression") = "username"
                viewstate("SortAscending") = True
            Case 1
                viewstate("SortExpression") = "username"
                viewstate("SortAscending") = True
            Case Else
                viewstate("SortExpression") = "clast"
                viewstate("SortAscending") = True
        End Select
        ResetFilter()
        ResetSort()
        DataGridHolidayGifts.EditItemIndex = -1
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        UpdateColumnHeaders(DataGridHolidayGifts)
        DataGridHolidayGifts.DataBind()
    End Sub

    Private Sub ResetFilter()
        Select Case FilterListID
            Case 0
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@start").Value = 0
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@end").Value = 0
                DataGridHolidayGifts.Columns(2).Visible = False
                ButtonNew.Enabled = False
                LabelHCC.Visible = False
                TextBoxHCC.Visible = False
                LinkButtonUpdate.Visible = False
            Case 1
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@start").Value = 1
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@end").Value = 9999
                DataGridHolidayGifts.Columns(2).Visible = True
                ButtonNew.Enabled = False
                LabelHCC.Visible = False
                TextBoxHCC.Visible = False
                LinkButtonUpdate.Visible = False
            Case Else
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@start").Value = FilterListID
                SqlDataAdapterHolidayGifts.SelectCommand.Parameters("@end").Value = FilterListID
                DataGridHolidayGifts.Columns(2).Visible = False
                ButtonNew.Enabled = True
                LabelHCC.Visible = True
                TextBoxHCC.Visible = True
                LookUpCardCount()
                LinkButtonUpdate.Visible = True
        End Select

    End Sub

    Private Sub LookUpCardCount()
        FilterListID = DropDownListList.SelectedValue
        SqlDataAdapterCardLookup.SelectCommand.Parameters("@userid").Value = FilterListID
        SqlDataAdapterCardLookup.Fill(DataSetCardLookup)
        If IsDBNull(DataSetCardLookup.Tables(0).Rows(0).Item(0)) Then
            TextBoxHCC.Text = ""
        Else
            TextBoxHCC.Text = DataSetCardLookup.Tables(0).Rows(0).Item(0)
        End If
    End Sub

    Private Sub LinkButtonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButtonUpdate.Click
        FilterListID = DropDownListList.SelectedValue
        Dim wcount = TextBoxHCC.Text

        Dim sql As String = _
           "UPDATE general.dbo.directory " & _
           "SET holidaycardcount='" & wcount & "' " & _
           "WHERE [key]=" & FilterListID

        'update the record itself
        Dim Sqlcommand As SqlCommand = New SqlCommand(sql, SqlConnectionSurveys)
        Sqlcommand.CommandType = CommandType.Text
        SqlConnectionSurveys.Open()
        Sqlcommand.ExecuteNonQuery()
    End Sub

    Private Sub ResetSort()
        'get the SQL statement from the data adapter
        Dim SortSql As String = SqlDataAdapterHolidayGifts.SelectCommand.CommandText
        'cut off the ORDER BY part at the end
        SortSql = Left(SortSql, InStr(SortSql, "ORDER BY") + 8)
        'add the new ORDER BY part from the sortexpression
        SortSql = SortSql + " " + Me.SortExpression
        'set ascending or descending
        If SortAscending Then
            SortSql = SortSql + " ASC"
        Else
            SortSql = SortSql + " DESC"
        End If
        'apply it back to the dataadapter, then update the dataset and datagrid
        SqlDataAdapterHolidayGifts.SelectCommand.CommandText = SortSql
    End Sub

    Private Sub DataGridHolidayGifts_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGridHolidayGifts.SortCommand
        'Toggle SortAscending if the column that the data was sorted by has
        'been clicked again...
        If e.SortExpression = Me.SortExpression Then
            SortAscending = Not SortAscending
        Else
            SortAscending = True
        End If

        'Set the SortExpression property to the SortExpression passed in
        Me.SortExpression = e.SortExpression
        ResetFilter()
        ResetSort()
        SqlDataAdapterHolidayGifts.Fill(DataSetHolidayGifts)
        UpdateColumnHeaders(DataGridHolidayGifts)
        DataGridHolidayGifts.DataBind()
    End Sub

    Private Sub UpdateColumnHeaders(ByVal dg As DataGrid)
        Dim c As DataGridColumn
        For Each c In dg.Columns
            'Clear any <img> tags that might be present
            c.HeaderText = Regex.Replace(c.HeaderText, "\s<.*>", String.Empty)

            If c.SortExpression = SortExpression Then
                If SortAscending Then
                    c.HeaderText &= " <img src=""up.gif"" border=""0"">"
                Else
                    c.HeaderText &= " <img src=""down.gif"" border=""0"">"
                End If
            End If
        Next
    End Sub

End Class
0
Comment
Question by:mlsides
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 17891763
You can try putting a bit delay in Clint side focus event,

here's a code, I have on that issue:
(you can use this or modified your code to reflect winwo.setTimeout, change Timeout value based on loading time of your page)
--------------------------------------------------------------------------------------------------------------
Public Sub setFocus(ctrl As System.Web.UI.Control)
        Dim strS As String
        strS = "<SCRIPT language='javascript'>"
        strS = strS + "function SetFocus(){ "                  
        strs = strs + "document.getElementById"
        strs = strs + "('"
        strs = strs + ctrl.ID
        strs = strs + "').focus();"
        strS = strS + " } "                                            
        strS = strS + "window.setTimeout(SetFocus, 500);"    ' This is the Trick part you need...
        strs = strs + "</"
        strs = strs + "SCRIPT>"
        Page.RegisterStartupScript("focus", strS)
    End Sub
--------------------------------------------------------------------------------------------------------------

-tushar
0
 

Author Comment

by:mlsides
ID: 17892160
Tushar,

This solution seems to work, but doesn't really answer the question of what's happening.  Any idea if an event is firing or something that is suppressing the client-side script?

Thanks.
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 17892197
Seems like you have smartnavigation enabled for your application, when SmartNavigation is enabled the framework will try to find out the last control where focus was setup before PostBack and will SetFocus on that particular control.

Now, since datagrid may take a bit longer to render then javascript, your script was executing before SmartNavigation script, thus was suppressed. By giving about 500ms time we can make sure that our script runs after SmartNavigation scripts.

-tushar
0
 

Author Comment

by:mlsides
ID: 17892268
Yes, I do have smartnavigation enabled.   OK,  thanks.   I guess this is the way it will have to go.
Thanks very much for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now