473,413 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

ASP:TEXTBOX TextChangedEvent Focus problem

Hi,

I have a number of text boxes on my web form. when the text is change the
serverside event TextChanged gets raised and I do some server side
validation. This is fine.

However, when the event has finished processing, I would expect focus to be
returned to the next textbox on the page - this does not happen.

My question is, how can I programatically in code behind set focus to a
specfied control.

--
Regards

Rob
Nov 18 '05 #1
6 1520
Check out this code:
http://www.csharphelp.com/archives/archive201.html


"Rob Shorney" <Bi****@community.nospam> wrote in message
news:46**********************************@microsof t.com...
Hi,

I have a number of text boxes on my web form. when the text is change the
serverside event TextChanged gets raised and I do some server side
validation. This is fine.

However, when the event has finished processing, I would expect focus to
be
returned to the next textbox on the page - this does not happen.

My question is, how can I programatically in code behind set focus to a
specfied control.

--
Regards

Rob

Nov 18 '05 #2
Rob,

Client side javascript call myTextBox.focus() will do. You can emit this
statement from server-side code behind.

--
Eliyahu

"Rob Shorney" <Bi****@community.nospam> wrote in message
news:46**********************************@microsof t.com...
Hi,

I have a number of text boxes on my web form. when the text is change the
serverside event TextChanged gets raised and I do some server side
validation. This is fine.

However, when the event has finished processing, I would expect focus to be returned to the next textbox on the page - this does not happen.

My question is, how can I programatically in code behind set focus to a
specfied control.

--
Regards

Rob

Nov 18 '05 #3
Here is a piece of code.
Hope this helps!
Franck Quintana.

static string SetJavascriptFocus(Control Control, bool HighlightText) {

if(Control == null || Control.ID == null || Control.ID.Length == 0)

throw new ApplicationException("Impossible to give the focus to a control
which has no ID.");

StringBuilder oSb = new StringBuilder();

oSb.Append("document.getElementById(\"");

oSb.Append(Control.ClientID);

oSb.Append("\").focus();\r\n");

if(HighlightText && Control is TextBox) {

oSb.Append("document.getElementById(\"");

oSb.Append(Control.ClientID);

oSb.Append("\").select();\r\n");

}

return JavascriptEmbed(oSb.ToString());

}

/// <summary>

/// Embeds a javascript code with correct tags.

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

/// <example>

/// <script type="text/javascript"><!--

/// document.getElementById("standard").focus();

/// //-->

/// </script>

/// </example>

public static string JavascriptEmbed(string value) {

return "<script type=\"text/javascript\"><!--\r" + (value.EndsWith("\r") ?
value : value + "\r") + "//-->\r</script>";

}

Nov 18 '05 #4
NB! This code is IE only

This code is better:
http://www.dotnetbips.com/displayarticle.aspx?id=106

I'm trying to translate it into C#. Could you help me?

This is what i've done so far. Need help on the while loop:

public static void SetInitialFocus2(Control ctrl)
{
StringBuilder s = new StringBuilder();
s.Append("<SCRIPT LANGUAGE='JavaScript'>");
s.Append("function SetInitialFocus()");
s.Append("{");
s.Append(" document.");
Control p = ctrl.Parent;
while (Not typeof p Is System.Web.UI.HtmlControls.HtmlForm)
{
p = p.Parent;
}
s.Append(p.ClientID);
s.Append("['");
s.Append(ctrl.UniqueID);
s.Append("'].focus();");
s.Append("}");
s.Append("window.onload = SetInitialFocus;");
s.Append("</SCRIPT>");
ctrl.Page.RegisterClientScriptBlock("InitialFocus" , s.ToString());
}

"Eirik Eldorsen" <ku*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Check out this code:
http://www.csharphelp.com/archives/archive201.html


"Rob Shorney" <Bi****@community.nospam> wrote in message
news:46**********************************@microsof t.com...
Hi,

I have a number of text boxes on my web form. when the text is change the
serverside event TextChanged gets raised and I do some server side
validation. This is fine.

However, when the event has finished processing, I would expect focus to
be
returned to the next textbox on the page - this does not happen.

My question is, how can I programatically in code behind set focus to a
specfied control.

--
Regards

Rob


Nov 18 '05 #5
NB This code is for netscape, IE, Mozilla, firefox...
Nov 18 '05 #6

"Rob Shorney" wrote in message

Quick question: Is there any reason why you can't do your validation client
side with JavaScript?

if not then IMHO multiple postbacks for one page is a bit in-effiecient and
irratating to the user maybe do one postback once all the fields are
complete, validate the buggers then in the event of errors reshow the page
with the errors highlighted.

Ian
Hi,

I have a number of text boxes on my web form. when the text is change the
serverside event TextChanged gets raised and I do some server side
validation. This is fine.

However, when the event has finished processing, I would expect focus to be returned to the next textbox on the page - this does not happen.

My question is, how can I programatically in code behind set focus to a
specfied control.

--
Regards

Rob

Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex | last post by:
I am a newbie to ASP.NET... What I was trying to do... but yet to succeed in... I am building a customer info database... and on default, I want to display the customer's basic information in...
4
by: George Durzi | last post by:
I'm trying to set focus on an asp:textbox. I'm running this code after my page_load StringBuilder sb = new StringBuilder(); sb.Append(@"<script language=javascript>"); sb.Append(@"<!--");...
5
by: PD | last post by:
The TextChanged event is not being called when I hit the "Enter" key. Yet it does for the "Tab" key. I set a break point in the Page_Load event and the TextChanged event and none of these get...
7
by: Jan Hoffman | last post by:
hi @all I have the following element in an ascx-file: <asp:TextBox id="name" runat="server">name</asp:TextBox> The textbox belongs to an application of a telefonebook. I want that if I...
3
by: Dave | last post by:
I have a button that calls a JavaScript. I need to get the value in an asp:TextBox field to the javascript, either as a parameter or via some mechanism within the JavaScript itself. Any Ideas? ...
1
by: Shilpi Chaudhry | last post by:
I have a forloop which I use to create textboxes - but unfortunately all of them have the same ids - I need to assign different ids to these textboxes creeated in the loop so that I can retrieve...
0
by: Luis Esteban Valencia | last post by:
am trying to databind within an asp:textbox control. I have tried many variations found here but nothing seems to work. It works fine if I use a regular HTML textbox input though. Here is what I...
0
by: datakix | last post by:
After 16 hours of frustration, I've managed to solve this problem for a project I'm working on. The 'trick' is set EnableViewState="False" for the asp:textbox inside the Repeater control. The...
0
by: sjickells | last post by:
Hi I am having a problem using asp:TextBox's in a transparent table. I have a background image on the page and a table in the middle of the page. I have set the background colour of the table...
2
by: Christian Cambier | last post by:
Hello, I have a textbox in which I only want to display numbers when the focus is set on the textbox and the user presses a key on the kb. Easy with html-control <input id="htmlTxtVal2"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.