===========================================================================
IDE: Visual Studio Pro 2005
Technologies: SQLServerExpress, VB.Net
============================================================================
I am curious....
How do you set focus on a textbox control in a modalPopup Panel once the TargetControl is activated?
I have tried putting it in the code behind for the TargetControl and it does not work.
you can add an event handler to the ModalPopup showing event and set the focus with a javascript function. Add this script to your page:
<script type="text/javascript">
Sys.Application.add_load(modalSetup);
function SetFocusOnControl()
{
// set the focus with javascript, e.g.: window.getElementById("xyz").focus();
}
function modalSetup()
{
var modalPopup = $find('ModalDialogTest'); // <. this is the BehaviorID from the ModalPopupExtender
modalPopup.add_showing(SetFocusOnControl);
}
I am having difficulty trying to get this to work.
I am using Master Pages. How do I register the javaScript on a content page?
EDIT:
I just tried using this code in the code-behind for the Page... This code FAILED and did not work.
If Not Page.IsPostBack Then
If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then
Dim scriptString = "<script type='text/javascript'>" _
& "Sys.Application.add_load(modalSetup);" _
& "function SetFocusOnControl() {" _
& "window.getElementById('txtUsername').focus(); }" _
& "function modalSetup() {" _
& "var modalPopup = $find('ModalDialogTest');" _
& "modalPopup.add_showing(SetFocusOnControl); }" _
& "</script>"
Me.ClientScript.RegisterStartupScript(Me.GetType(), "Startup", scriptString)
End If
End If
Thank you for your response. I have made the adjustments to the code, but it still does not set focus on the login textbox when I run the application and click the TargetControl of the ModalPopupExtender.
Here is my code for the page...
[code behind]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'====================== Java Code for ModalPopup TextBox focus ======================='
If Not Page.IsPostBack Then
If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then
Dim scriptString = "<script type='text/javascript'>" _
& "Sys.Application.add_load(modalSetup);" _
& "function SetFocusOnControl() {" _
& "$get('" _
& txtUsername.ClientID _
& "').focus(); }" _
& "function modalSetup() {" _
& "var modalPopup = $find('" _
& ModalPopupExtender1.ClientID _
& "');" _
& "modalPopup.add_shown(SetFocusOnControl); }" _
& "</script>"
Me.ClientScript.RegisterStartupScript(Me.GetType(), "Startup", scriptString)
End If
End If
'=================== End Java Code for ModalPopup TextBox Focus ====================='
End Sub
Thank you! I had to modify it slightly do to my application being written in VB.NET, but I have gotten it working now!
Here is the code that finally worked:
[codebehind]
If Not Page.IsPostBack Then
If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<script type=""text/javascript\"">")
sb.Append("Sys.Application.add_load(modalSetup);")
sb.Append("function modalSetup() {")
sb.Append(String.Format("var modalPopup = $find('{0}');", ModalPopupExtender1.BehaviorID))
sb.Append("modalPopup.add_shown(SetFocusOnControl); }")
sb.Append("function SetFocusOnControl() {")
sb.Append(String.Format("var textBox1 = $get('{0}');", txtUsername.ClientID))
sb.Append("textBox1.focus();}")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString())
End If
End If
Thank you all for your assistance with this issue. Hopefully this thread will be useful to others in the future!
This works fine for me on a simple page but when I'm using a MultiView I'm having issues. When the ModelPopupExtender and related panel with the textbox is on the second view of a multivew, I get a popup error that says:
"Microsoft JScript runtime error: 'null' is null or not an object"
Protected Sub Page_Load(ByVal sender
As Object, ByVal e
As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If (Not Me.ClientScript.IsStartupScriptRegistered("Startup"))
Then
Dim sb As StringBuilder =
New StringBuilder()
sb.Append("<script type=""text/javascript\"">")
sb.Append("Sys.Application.add_load(modalSetup);")
sb.Append("function modalSetup() {")
sb.Append(String.Format("var modalPopup = $find('{0}');", me.mpeReqDetailPartsAdd.BehaviorID)) sb.Append("modalPopup.add_shown(SetFocusOnControl); }")
sb.Append("function SetFocusOnControl() {")
sb.Append(String.Format("var textBox1 = $get('{0}');", Me.part_numberTextBox.ClientID)) sb.Append("textBox1.focus();}")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"Startup", sb.ToString()) End If
End If
End Sub
Protected Sub btnGotoView2_Click(ByVal sender
As Object, ByVal e
As System.EventArgs)
MultiView1.SetActiveView(View2) End Sub
I imagine that I'm getting this error because the javascript can't find the controls since on the initial page load, the textbox and ModalPopupExtender aren't on the first view. I'm mostly clueless on how to program around this with javascript and would
greatly appreciate some help.
It works with multviews and panels when the container that your controls are in, are intially set to be not visible. I still haven't found a way to get set focus on a textbox in a modalpopup on the second pane of a multiview or a panel that initially has
it's visible property set to false.
KJAK
Participant
1663 Points
473 Posts
Set Focus on Textbox in modalPopup Panel
May 17, 2007 05:22 PM|LINK
===========================================================================
IDE: Visual Studio Pro 2005
Technologies: SQLServerExpress, VB.Net
============================================================================
I am curious....
How do you set focus on a textbox control in a modalPopup Panel once the TargetControl is activated?
I have tried putting it in the code behind for the TargetControl and it does not work.
ModalPopupExtender
KJAK
Zhou
Participant
1428 Points
266 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 18, 2007 10:16 AM|LINK
Hi,
you can add an event handler to the ModalPopup showing event and set the focus with a javascript function. Add this script to your page:
<script type="text/javascript">
Sys.Application.add_load(modalSetup);
function SetFocusOnControl()
{
// set the focus with javascript, e.g.: window.getElementById("xyz").focus();
}
function modalSetup()
{
var modalPopup = $find('ModalDialogTest'); // <. this is the BehaviorID from the ModalPopupExtender
modalPopup.add_showing(SetFocusOnControl);
}
</script>
Regards
Marc André
KJAK
Participant
1663 Points
473 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 18, 2007 01:14 PM|LINK
I am having difficulty trying to get this to work.
I am using Master Pages. How do I register the javaScript on a content page?
EDIT:
I just tried using this code in the code-behind for the Page... This code FAILED and did not work.
If Not Page.IsPostBack Then If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then Dim scriptString = "<script type='text/javascript'>" _ & "Sys.Application.add_load(modalSetup);" _ & "function SetFocusOnControl() {" _ & "window.getElementById('txtUsername').focus(); }" _ & "function modalSetup() {" _ & "var modalPopup = $find('ModalDialogTest');" _ & "modalPopup.add_showing(SetFocusOnControl); }" _ & "</script>" Me.ClientScript.RegisterStartupScript(Me.GetType(), "Startup", scriptString) End If End IfKJAK
Raymond Wen ...
All-Star
32101 Points
3764 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 22, 2007 02:15 AM|LINK
Hi,
Basically, I'm agree with Zhou's idea. Just need to make a few changes. See the demo bellow:
[Master]
<%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html>[Page][CodeBehind]
Hope this helpsKJAK
Participant
1663 Points
473 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 23, 2007 11:57 AM|LINK
Hi,
Thank you for your response. I have made the adjustments to the code, but it still does not set focus on the login textbox when I run the application and click the TargetControl of the ModalPopupExtender.
Here is my code for the page...
[code behind]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load '====================== Java Code for ModalPopup TextBox focus =======================' If Not Page.IsPostBack Then If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then Dim scriptString = "<script type='text/javascript'>" _ & "Sys.Application.add_load(modalSetup);" _ & "function SetFocusOnControl() {" _ & "$get('" _ & txtUsername.ClientID _ & "').focus(); }" _ & "function modalSetup() {" _ & "var modalPopup = $find('" _ & ModalPopupExtender1.ClientID _ & "');" _ & "modalPopup.add_shown(SetFocusOnControl); }" _ & "</script>" Me.ClientScript.RegisterStartupScript(Me.GetType(), "Startup", scriptString) End If End If '=================== End Java Code for ModalPopup TextBox Focus =====================' End Sub[page code]
When debugging, the script appears to register. It just doesn't seem to be doing anything. Can you see any issues with my code?KJAK
dapoussin
Member
54 Points
12 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 23, 2007 03:17 PM|LINK
Hi !
I was able to make it work with the following code in my Page_Load :
StringBuilder sb = new StringBuilder(); sb.Append("<script type=\"text/javascript\">\n"); sb.Append("Sys.Application.add_load(modalSetup);\n"); sb.Append("function modalSetup() {\n"); sb.Append(String.Format("var modalPopup = $find('{0}');\n", ModalPopupExtender1.BehaviorID)); sb.Append("modalPopup.add_shown(SetFocusOnControl); }\n"); sb.Append("function SetFocusOnControl() {\n"); sb.Append(String.Format("var textBox1 = $get('{0}');\n", tbMyTextbox.ClientID)); sb.Append("textBox1.focus();}\n"); sb.Append("</script>\n"); Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString());Hope this helps.
Laurent
KJAK
Participant
1663 Points
473 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 23, 2007 04:05 PM|LINK
YES!
Thank you! I had to modify it slightly do to my application being written in VB.NET, but I have gotten it working now!
Here is the code that finally worked:
[codebehind]
If Not Page.IsPostBack Then If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then Dim sb As StringBuilder = New StringBuilder() sb.Append("<script type=""text/javascript\"">") sb.Append("Sys.Application.add_load(modalSetup);") sb.Append("function modalSetup() {") sb.Append(String.Format("var modalPopup = $find('{0}');", ModalPopupExtender1.BehaviorID)) sb.Append("modalPopup.add_shown(SetFocusOnControl); }") sb.Append("function SetFocusOnControl() {") sb.Append(String.Format("var textBox1 = $get('{0}');", txtUsername.ClientID)) sb.Append("textBox1.focus();}") sb.Append("</script>") Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString()) End If End IfThank you all for your assistance with this issue. Hopefully this thread will be useful to others in the future!Sincerely,
KJAK
KJAK
gknierim
Contributor
2008 Points
492 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 23, 2007 05:50 PM|LINK
And the C# version of it:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (!this.ClientScript.IsStartupScriptRegistered("startup")) { StringBuilder sb = new StringBuilder(); sb.Append("<script type='text/javascript'>"); sb.Append("Sys.Application.add_load(modalSetup);"); sb.Append("function modalSetup() {"); sb.Append(String.Format("var modalPopup = $find('{0}');", popupEntry.BehaviorID)); sb.Append("modalPopup.add_shown(SetFocusOnControl); }"); sb.Append("function SetFocusOnControl() {"); sb.Append(String.Format("var textBox1 = $get('{0}');", txtValue.ClientID)); sb.Append("textBox1.focus();}"); sb.Append("</script>"); Page.ClientScript.RegisterStartupScript(Page.GetType(), "startup", sb.ToString()); } } }Please mark my response as the answer if it helped you.
Mobes
Member
91 Points
20 Posts
Re: Set Focus on Textbox in modalPopup Panel
Aug 17, 2007 10:21 PM|LINK
This works fine for me on a simple page but when I'm using a MultiView I'm having issues. When the ModelPopupExtender and related panel with the textbox is on the second view of a multivew, I get a popup error that says:
"Microsoft JScript runtime error: 'null' is null or not an object"
Here's my code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:Button ID="btnGotoView2" runat="server" Text="View2" OnClick="btnGotoView2_Click" />
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Panel ID="pnlReqDetailAddParts" Style="display: none" runat="server" CssClass="modalPopup">
<div id="dragReqDetailAddParts" class="modalPopupDragArea"> </div>
part_number:
<asp:TextBox ID="part_numberTextBox" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="lbAddPart" runat="server" CausesValidation="True" Text="Add Part" />
<asp:LinkButton ID="lbCancelAddPart" runat="server" CausesValidation="False" Text="Cancel" />
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="mpeReqDetailPartsAdd" runat="server"
TargetControlID="btnAddPart2"
PopupControlID="pnlReqDetailAddParts"
OkControlID="lbAddPart"
CancelControlID="lbCancelAddPart"/>
<asp:Button ID="btnAddPart2" runat="server" Text="Button" />
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If (Not Me.ClientScript.IsStartupScriptRegistered("Startup")) Then
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<script type=""text/javascript\"">")
sb.Append("Sys.Application.add_load(modalSetup);")
sb.Append("function modalSetup() {")
sb.Append(String.Format("var modalPopup = $find('{0}');", me.mpeReqDetailPartsAdd.BehaviorID))
sb.Append("modalPopup.add_shown(SetFocusOnControl); }")
sb.Append("function SetFocusOnControl() {")
sb.Append(String.Format("var textBox1 = $get('{0}');", Me.part_numberTextBox.ClientID))
sb.Append("textBox1.focus();}")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString())
End If
End If
End Sub
Protected Sub btnGotoView2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MultiView1.SetActiveView(View2)
End Sub
I imagine that I'm getting this error because the javascript can't find the controls since on the initial page load, the textbox and ModalPopupExtender aren't on the first view. I'm mostly clueless on how to program around this with javascript and would greatly appreciate some help.
Mobes
Member
91 Points
20 Posts
Re: Set Focus on Textbox in modalPopup Panel
Aug 20, 2007 05:57 PM|LINK
I found a partial solution for my issue...put this in your page load or on button click events, etc.:
System.Web.UI.ScriptManager.GetCurrent(Me).SetFocus(Me.tbPartNumber)
It works with multviews and panels when the container that your controls are in, are intially set to be not visible. I still haven't found a way to get set focus on a textbox in a modalpopup on the second pane of a multiview or a panel that initially has it's visible property set to false.