locked
can't get focus on textbox RRS feed

  • Question

  • User1280950372 posted

    When a user on my site exits out of a ModalPopupExtender, I can't get the focus to go to the first textbox in the page as it does in other places, with this code:

    ListView lsv = Helpers.FindChild(Page, "lsvNewInvite") as ListView;
    if (lsv != null)
    {
        TextBox txt = Helpers.FindChild(lsv, "txtFriendFirstName") as TextBox;
        if (txt != null)
            txt.Focus();
    }
    
    
    public static Control FindChild(Control start, string id)
    {
        System.Web.UI.Control foundControl;
        if (start != null)
        {
            foundControl = start.FindControl(id);
            if (foundControl != null)
                return foundControl;
            foreach (Control c in start.Controls)
            {
                foundControl = FindChild(c, id);
                if (foundControl != null)
                    return foundControl;
            }
        }
        return null;
    }
    

    I suspect that it has to do with a postback issue but I can't figure it out. Here is the code that I believe is the most applicable:

    <asp:UpdatePanel ID="udpModalSuccess" runat="server">
    <ContentTemplate>
        <%--success popup items--%>
        <div style="display: none;">
            <asp:Button ID="btnDummy" UseSubmitBehavior="true" OnClientClick="ShowModalPopup" OnClick="btnDummy_Click" runat="server" />
            <%--Dummy Button added to assign the target controlid of PopupExtender--%>
            <asp:Button ID="btnDummyButton" UseSubmitBehavior="true" runat="server" Text="DummyButton" Style="display: none;" />
        </div>
        <asp:Panel ID="pnlNewInviteSuccess" class="modalPopup" runat="server">
            <div><asp:Label ID="lblNewInviteSuccess" runat="server" /></div>
            <br />
            <div><asp:Button ID="btnNewInviteReturn" Text="OK" CssClass="button" runat="server" /></div>
        </asp:Panel>
        <%--Here I have changed the TargetControlID property--%>
        <act:ModalPopupExtender ID="mpeNewInviteSuccess" TargetControlID="btnDummyButton" CancelControlID="btnNewInviteReturn"
            BackgroundCssClass="modalBackground" PopupControlID="pnlNewInviteSuccess" DropShadow="true"
            BehaviorID="mpe" runat="server">
        </act:ModalPopupExtender>
    </ContentTemplate>
    </asp:UpdatePanel>
    
    
    
        protected void lsvNewInvite_ItemCommand(Object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                //place cursor in txtFirstName
                var lsv = Helpers.FindChild(this, "lsvNewInvite") as ListView;
                TextBox txt = Helpers.FindChild(lsv, "txtFriendFirstName") as TextBox;
                if (txt != null)
                    txt.Focus();
    
                //set up parameter values
                ListViewItem item = e.Item as ListViewItem;
                var userInfoList = UserInfo.UserInfoGetByUserID(UserID);
                UserInformation = userInfoList.FirstOrDefault();
                var user = UserInformation.UserID;
                string firstName = (item.FindControl("txtFriendFirstName") as TextBox).Text.ToString();
                string lastName = (item.FindControl("txtFriendLastName") as TextBox).Text.ToString();
                string eMail = (item.FindControl("txtFriendEmail") as TextBox).Text.ToString();
    
                try
                {
                    //code to be inserted into DB
                    srcNewInvite.InsertParameters["FriendID"].DefaultValue = "0";
                    srcNewInvite.InsertParameters["User"].DefaultValue = user.ToString();
                    srcNewInvite.InsertParameters["FirstName"].DefaultValue = firstName;
                    srcNewInvite.InsertParameters["LastName"].DefaultValue = lastName;
                    srcNewInvite.InsertParameters["Email"].DefaultValue = eMail;
                    srcNewInvite.InsertParameters["Date"].DefaultValue = DateTime.Now.ToString();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    EmailNewInvitee(firstName, lastName, eMail);
    
                    //concatenate these values and make them as a comma-separated string
                    string concatenatedValue = firstName + "," + lastName + "," + eMail;
                    btnDummy.CommandArgument = concatenatedValue;
                    //show success message by programatically "clicking" hidden button
                    //trigger the button click event
                    btnDummy_Click(this.btnDummy, e);
                    pnlMain.Visible = false;
                    pnlNewInviteSuccess.Visible = true;
                }
            }
        }
    
        protected void btnDummy_Click(Object sender, EventArgs e)
    {
            //mpeNewInviteSuccess.Show();
            //create object of button from sender
             Button button = sender as Button;
    
            //get command argument
            string cmdArgument = button.CommandArgument;
    
            //split command argument based on comma separator and convert to a list
             List<string> lstPassedValues = new List<string>(cmdArgument.Split(','));
    
            ListView lsv = Helpers.FindChild(Page, "lsvNewInvite") as ListView;
    
            //create successful message from list
            string SuccessMsg = "Success! Your invitation was sent to:<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + lstPassedValues[0] + " " + lstPassedValues[1] + " (" + lstPassedValues[2] + "</b>)";
            lblNewInviteSuccess.Text = SuccessMsg;
            //assign TargetControl id of ModalPopUpExtender to btnDummy
            mpeNewInviteSuccess.TargetControlID = "btnDummy";
            //show ModalPopup Extender
            mpeNewInviteSuccess.Show();
        }

    This are two things I tried at the end of the btnDummy_Click event, but they didn't work:

            Response.Redirect(Request.Url.AbsoluteUri);        
    
            ListView lsv = Helpers.FindChild(Page, "lsvNewInvite") as ListView;
            if (lsv != null)
            {
                TextBox txt = Helpers.FindChild(lsv, "txtFriendFirstName") as TextBox;
                if (txt != null)
                    txt.Focus();
            }




    Monday, January 13, 2014 2:39 PM

Answers

  • User555306248 posted
          

    You can set onOkScript and onCancelScript and set the focus with a javascript method.

    OR

    add a handler to the ModalPopupExtender's add_hidden event hook.  It will be fired when the ModalPanelExtender is hide

    <script type="text/javascript" language="javascript">
                 function pageLoad(){
                     $find("ModalPopupExtender's BehavoirID").add_hidden(onModalPopupHide);
                 }
                 function onModalPopupHide(){
                  //set focus here.
                  }
    
            </script>
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2014 10:39 PM

All replies

  • User555306248 posted
          

    You can set onOkScript and onCancelScript and set the focus with a javascript method.

    OR

    add a handler to the ModalPopupExtender's add_hidden event hook.  It will be fired when the ModalPanelExtender is hide

    <script type="text/javascript" language="javascript">
                 function pageLoad(){
                     $find("ModalPopupExtender's BehavoirID").add_hidden(onModalPopupHide);
                 }
                 function onModalPopupHide(){
                  //set focus here.
                  }
    
            </script>
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2014 10:39 PM
  • User1280950372 posted

    Beautiful! Thank you, works perfectly. This is the code I ended up using:

    <script type="text/javascript" language="javascript">
        function pageLoad() { $find("mpe").add_hidden(onModalPopupHide); }
        function onModalPopupHide() { document.getElementById('txtFriendFirstName').focus(); }
    </script>
    

    Tuesday, January 14, 2014 2:34 AM