Undesired Ajax calendar extender focus on postback

Hi guys,
I have a child page that inherits a master page. The child page contains a text box, that has a calendar extender, working fine when you click on textbox.  However, everytime there's a postback, page focuses on the calendar textbox control causing the calendar to popup on every postbacks.  

I have tried the following to remove the focus on this control:
1) Changed the tab index = 3 because it is the 3rd control on the page from the top.
2) Used .focus property on other controls on the pageload event. Ex: txtOther.focus()
2) Tried setting to focus to other controls on the pageload event programatically using the script attached with no luck.  Ex: SetFocus(txtOther)

Thank you for your help.

Private Overloads Sub SetFocus(ByVal control As Control)
            Dim sb As New StringBuilder()

            sb.Append(vbCr & vbLf & "<script language='JavaScript'>" & vbCr & vbLf)
            'sb.Append("<!--" & vbCr & vbLf)
            sb.Append("function SetFocus()" & vbCr & vbLf)
            sb.Append("{" & vbCr & vbLf)
            sb.Append(vbTab & "document.")

            Dim p As Control = control.Parent
            While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
                p = p.Parent
            End While

            sb.Append(p.ClientID)
            sb.Append("['")
            sb.Append(control.UniqueID)
            sb.Append("'].focus();" & vbCr & vbLf)

            sb.Append("}" & vbCr & vbLf)
            sb.Append("window.onload = SetFocus;" & vbCr & vbLf)
            sb.Append("</script>")

          
End Sub

Open in new window

Private Overloads Sub SetFocus(ByVal control As Control)
            Dim sb As New StringBuilder()

            sb.Append(vbCr & vbLf & "<script language='JavaScript'>" & vbCr & vbLf)
            'sb.Append("<!--" & vbCr & vbLf)
            sb.Append("function SetFocus()" & vbCr & vbLf)
            sb.Append("{" & vbCr & vbLf)
            sb.Append(vbTab & "document.")

            Dim p As Control = control.Parent
            While Not (TypeOf p Is System.Web.UI.HtmlControls.HtmlForm)
                p = p.Parent
            End While

            sb.Append(p.ClientID)
            sb.Append("['")
            sb.Append(control.UniqueID)
            sb.Append("'].focus();" & vbCr & vbLf)

            sb.Append("}" & vbCr & vbLf)
            sb.Append("window.onload = SetFocus;" & vbCr & vbLf)
            sb.Append("</script>")

          
End Sub

Open in new window

LISCNYAsked:
Who is Participating?
 
LISCNYConnect With a Mentor Author Commented:
I fixed the source of the problem so I do not need the setFocus function for this issue anymore.  

Problem: What I did was I created a textbox and added a MaskedEditExtender on it and then added CalendarExtender.  

Solution: As you can see in the source code, I moved the CalendarExtender up and pushed the MaskedEditExtender below the CalendarExtender.  This worked for me.


<asp:TextBox ID="txtInvoiceDate" runat="server" Width="100px" 
                           BackColor="AliceBlue" TabIndex="2" Font-Names="Arial" Font-Size="Small"></asp:TextBox>
                       <asp:CalendarExtender ID="txtInvoiceDate_CalendarExtender" runat="server" 
                           Enabled="True" TargetControlID="txtInvoiceDate">
                       </asp:CalendarExtender>
                         <asp:MaskedEditExtender ID="txtInvoiceDate_MaskedEditExtender" runat="server" 
                           CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" 
                           CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
                           CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" 
                           Mask="99/99/9999" MaskType="Date" TargetControlID="txtInvoiceDate">
                       </asp:MaskedEditExtender>

Open in new window

0
 
gamarrojgqCommented:
Hi,

Can you post the code of your page once it is render? to see how the SetFocus javascript function is finally written in the page.
0
 
LISCNYAuthor Commented:
This fixed the problem was I experiencing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.