I just tried the code posted by KJAK (05-23-2007 11:05 AM), but it wasn't working for me.
I spent about 30 minutes trying different variations of the code, but my modalPopup object was always being returned as null, which produced a null reference on the "add_shown" call.
But, I've finally discovered the error! In the code above, KJAK escapes a double-quote character twice. That is, you used: \""
That should produce a compiler error as far as I'm concerned, but it still compiles. For some reason, that seemed to be causing the issue I was experiencing. After I removed the backslash at that point, the code worked for me. So, if anyone else has the
same issue, try the modified code below.
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}');", mpe1.BehaviorID))
sb.Append("modalPopup.add_shown(SetFocusOnControl); }")
sb.Append("function SetFocusOnControl() {")
sb.Append(String.Format("var textBox1 = $get('{0}');", confirmUsername.ClientID))
sb.Append("textBox1.focus();}")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString())
End If
End If
Well, it's hard to tell whether it is or isn't from your hastily scawled code snippet, but your textbox has to be inside the update panel. Maybe it would help if you showed your actual code for the update panel.
EndangeredMa...
Participant
1134 Points
211 Posts
Re: Set Focus on Textbox in modalPopup Panel
Dec 05, 2007 04:08 PM|LINK
I just tried the code posted by KJAK (05-23-2007 11:05 AM), but it wasn't working for me.
I spent about 30 minutes trying different variations of the code, but my modalPopup object was always being returned as null, which produced a null reference on the "add_shown" call.
But, I've finally discovered the error! In the code above, KJAK escapes a double-quote character twice. That is, you used: \""
That should produce a compiler error as far as I'm concerned, but it still compiles. For some reason, that seemed to be causing the issue I was experiencing. After I removed the backslash at that point, the code worked for me. So, if anyone else has the same issue, try the modified code below.
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}');", mpe1.BehaviorID)) sb.Append("modalPopup.add_shown(SetFocusOnControl); }") sb.Append("function SetFocusOnControl() {") sb.Append(String.Format("var textBox1 = $get('{0}');", confirmUsername.ClientID)) sb.Append("textBox1.focus();}") sb.Append("</script>") Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString()) End If End Ifsamuelmpmac
Member
4 Points
4 Posts
Re: Set Focus on Textbox in modalPopup Panel
Nov 27, 2008 02:54 PM|LINK
Thank you! Saved my life...
lianaent
Member
76 Points
98 Posts
Re: Set Focus on Textbox in modalPopup Panel
Dec 23, 2008 02:46 PM|LINK
I knew it couldn't possibly be that complicated. The answer:
private void btnShowPopup_Click(object sender, EventArgs e)
{
ModalPopup.Show();
ScriptManager.GetCurrent(this).SetFocus(this.textbox1);
}
Just change "textbox1" to the name of your control.
KJAK
Participant
1673 Points
478 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 20, 2009 06:22 PM|LINK
There are several answers marked on this thread. Which method are you having problems with?
KJAK
buehlej1
Member
6 Points
3 Posts
Re: Set Focus on Textbox in modalPopup Panel
Nov 06, 2009 02:16 PM|LINK
Thank you KJAK for posting this in the first place! and thank you Laurent for your great answer! I was having the same problem. It still works great!
yetrus
Member
6 Points
4 Posts
Re: Set Focus on Textbox in modalPopup Panel
Mar 22, 2010 03:20 PM|LINK
Hi there
I'm with this, but I can't do it works
this it's the testing code:
[Master]
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
[/Master]
[Content ]
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Panel>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" TargetControlID="Label1" runat="server" PopupControlID="Panel1" Enabled="True">
</cc1:ModalPopupExtender>
<asp:Button ID="Button2" runat="server" Text="Button" />
</div>
[/Content ]
[VB ]
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
ModalPopupExtender1.Show()
System.Web.UI.ScriptManager.GetCurrent(Me).SetFocus(Me.TextBox1)
[/vb]
where's a tricks??
I neen that focus on the textbox !!
help me please !!
greetings
lianaent
Member
76 Points
98 Posts
Re: Set Focus on Textbox in modalPopup Panel
Mar 22, 2010 11:14 PM|LINK
In VB you have to use:
System.Web.UI.ScriptManager.GetCurrent(Me.Page).SetFocus(Me.TextBox1)
Larry
yetrus
Member
6 Points
4 Posts
Re: Set Focus on Textbox in modalPopup Panel
Mar 23, 2010 11:59 AM|LINK
with this do not works
=(
System.Web.UI.ScriptManager.GetCurrent(Me.Page).SetFocus(Me.TextBox1)
lianaent
Member
76 Points
98 Posts
Re: Set Focus on Textbox in modalPopup Panel
Mar 24, 2010 02:47 AM|LINK
Well, it's hard to tell whether it is or isn't from your hastily scawled code snippet, but your textbox has to be inside the update panel. Maybe it would help if you showed your actual code for the update panel.
rajan_top1
Member
160 Points
104 Posts
Re: Set Focus on Textbox in modalPopup Panel
May 25, 2010 02:04 PM|LINK
What is this ?
What do you mean and what do you say?