locked
how to set focus on a dropdownlist RRS feed

  • Question

  • User-944424728 posted

    I have a dropdownlist which has "all, active and inactive". When select inactive, they show all the inactive users from a table and same with active users. However, I want the cursor focus stay on the dropdown list but the focus always jump to the last cell column title. I tried "$("#<%= ddlShowStaffStatus.ClientId %>").focus(); " and it works when I comment out "loadStaffGroup() which list the all, inactive and active user. Is there anyway, my cursor focus stay on the dropdownlist and at the same time work with loadstaffgroup(). Please see code below. Thanks.

    $("#ddlShowStaffStatus").on("change", function () {
                  
                    loadStaffGroup();
                  
                });
    function loadStaffGroup() {
                var hdGroupUsersList = '#<%=hdGroupUsersList.ClientID %>';
                var tblTemp = '#tblManageGroup';
                var objGrp = new objForm();
                var initObj = {};
                initObj.sda = objGrp.sda;
                initObj.programId = objGrp.programId;
                initObj.programCode = objGrp.programCode;
                initObj.groupId = objGrp.groupId;
                initObj.inactiveUser = objGrp.inactiveUser;
                initObj.locationValue = objGrp.locationValue;
                initObj.locationType = objGrp.locationType;
                var url = 'CaseStaffAssign.aspx/GetStaffCaseAssignmentGroup';
                $.ajax({
                    beforeSend: function () {
                        $('.loader').show();
                    },
                    complete: function () {
                        $('.loader').hide();
                    },
                    error: function (msg) {
                        alert("loadStaffCaseAssignmentGroup - error:" + JSON.stringify(msg));
                    },
                    url: url,
                    type: "POST",
                    async: true,
                    data: JSON.stringify(initObj),
                    processData: false,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        var results = data.d;
                        if (results.length > 0) {
                            showHideDiv(true);
                            var tblTempBody = $(tblTemp + " tbody");
                            tblTempBody.empty();
                            for (var i = 0; i < results.length; i++) {
                                var oUsername = results[i].StaffLoginname;
                                var oFirstName = results[i].FirstName;
                                var oLastName = results[i].LastName;
                                var oAssign = results[i].col_Assign;
                                var oGroupLead = results[i].col_GroupLead;
                                var row = "<tr>" +
                                    "<td>" + oUsername + "</td>" +
                                    "<td>" + oFirstName + "</td>" +
                                    "<td>" + oLastName + "</td>" +
                                    "<td style='text-align: center;'>" + oAssign + "</td>" +
                                    "<td style='text-align: center;'>" + oGroupLead + "</td>" +
                                    "</tr>";
                                tblTempBody.append(row);
                                var rows = results.length - 1
                                if (i == rows) {
                                    var ret = results[i].GroupUsersList;
                                    $(hdGroupUsersList).val(ret);
                                }                           
                            }
                            //alert('from loadStaffGroup() ' + $(hdGroupUsersList).val());
                            $(tblTemp).trigger('update');
                        }
                        else {
                            showHideDiv(false);
                        }
                    },
                });
            }

        Protected Sub InitialLoad()
            With ddlShowStaffStatus
                .DataSource = CaseAssignmentManager.GetLookUpItemsWiaCommon("staff", "active_staff", AppUser.Lang, "0")
                .DataTextField = "Description"
                .DataValueField = "Value"
                .DataBind()
                .SelectedValue = "2"
            End With
            GetInfoByGroupID()
            GetCaseAssignedGoupInfo()
            GetCaseAssignedOtherGroupMembers()
            ddlShowStaffStatus.Focus()
        End Sub
    Wednesday, May 15, 2019 12:16 AM

All replies

  • User-1174608757 posted

    Hi aspvbnet,

    According to your description, I have made a sample here.However the focus is still stay on the dropdownlist.So have you used any other tools or method?Here is demo.I hope it could help you.

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                $("#D1").change(function () {
                    $.ajax({
                        url: "test1.asmx/He",
                        data:JSON.stringify( { "name": "aa" }),
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                         beforeSend: function () {
                        $('.loader').show();
                    },
                    complete: function () {
                        $('.loader').hide();
                    },
                        success: function (data) {
                            data = data.d;
                            var table = $("#Table1");
                            for (var i = 0; i < data.length; i++) {
                                table.append( "<tr><td>" + data[i] + "</td></tr>");
                            }
                        }
                        
                    })
                })
    
    
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <span class="loader">aaaa</span>
                <asp:DropDownList ID="D1" runat="server">
                    <asp:ListItem>
                        d1
                    </asp:ListItem>
                    <asp:ListItem>
                        d2
                    </asp:ListItem>
                </asp:DropDownList>
                <asp:Table ID="Table1" runat="server">
    
                </asp:Table>
            </div>
        </form>
    </body>
    </html>
    

    asmx.vb:

    Public Class test11
        Inherits System.Web.Services.WebService
    
        <WebMethod()>
        Public Function He(ByVal name As String) As List(Of String)
            Dim l1 As List(Of String) = New List(Of String)()
            l1.Add("1")
            l1.Add("2")
            Return l1
        End Function
    
    End Class

    You could see the focus:

    Best Regards

    Wei 

    Wednesday, May 15, 2019 9:35 AM