ASP:TEXTBOX TextChangedEvent Focus problem

G

Guest

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.
 
E

Eliyahu Goldin

Rob,

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

John Bilbo

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>";

}
 
E

Eirik Eldorsen

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 said:
 
I

Ian Frawley

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top