Laden...

TextBox focus setzen

Erstellt von solle vor 18 Jahren Letzter Beitrag vor 18 Jahren 9.103 Views
S
solle Themenstarter:in
4 Beiträge seit 2005
vor 18 Jahren
TextBox focus setzen

Hallo,

kann mir jemand sagen wie man einer TextBox sagen kann das sie beim aufrufen der
Webseite focusiert sein soll, d.h. der Curser soll darin aktiv sein und man kann sofort
lostippen?

Danke für eure Hilfe.

O
98 Beiträge seit 2005
vor 18 Jahren

hier mal eine Funktion, hoffe du kannst sie nachvollziehen. Übrigens ist das ASP was du hier fragst

/// <summary>
		/// Set the InitialFocus to the given control. Only works when JavaScript is supported.
		/// </summary>
		/// <param name="control">Control to set the InitialFocus on.</param>
		public static void SetInitialFocus(Control control) 
		{
			if (control.Page == null) 
			{
				throw new ArgumentException(
					"The Control must be added to a Page before you can set the IntialFocus to it.");
			}
			if (control.Page.Request.Browser.JavaScript == true) 
			{
				// Create JavaScript
				StringBuilder s = new StringBuilder();
				s.Append("\n<SCRIPT LANGUAGE='JavaScript'>\n");
				s.Append("<!--\n");
				s.Append("function SetInitialFocus()\n");
				s.Append("{\n");
				s.Append("   document.");

				// Find the Form
				Control p = control.Parent;
				while (!(p is System.Web.UI.HtmlControls.HtmlForm))
					p = p.Parent;
				s.Append(p.ClientID);

				s.Append("['");
				s.Append(control.UniqueID);

				// Set Focus on the selected item of a RadioButtonList
				RadioButtonList rbl = control as RadioButtonList;
				if (rbl != null) 
				{
					string suffix = "_0";
					int t = 0;
					foreach (ListItem li in rbl.Items) 
					{
						if (li.Selected) 
						{
							suffix = "_" + t.ToString();
							break;
						}
						t++;
					}
					s.Append(suffix);
				}

				// Set Focus on the first item of a CheckBoxList
				if (control is CheckBoxList) 
				{
					s.Append("_0");
				}

				s.Append("'].focus();\n");
				s.Append("}\n");

				if (control.Page.SmartNavigation)
					s.Append("window.setTimeout(SetInitialFocus, 500);\n");
				else
					s.Append("window.onload = SetInitialFocus;\n");

				s.Append("// -->\n");
				s.Append("</SCRIPT>");

				// Register Client Script
				control.Page.RegisterClientScriptBlock("InitialFocus", s.ToString());
			}
		}
N
4.644 Beiträge seit 2004
vor 18 Jahren

So funktioniert es in ASP.NET 2.0.

protected void Page_Load(object sender, EventArgs e)
{
    textBox.Focus();
}
O
98 Beiträge seit 2005
vor 18 Jahren

Original von Noodles
So funktioniert es in ASP.NET 2.0.

protected void Page_Load(object sender, EventArgs e)  
{  
    textBox.Focus();  
}  

das ist natürlich viel schneller geschrieben 😁 leider ist VS2005 noch nix für ne Firma X(

S
solle Themenstarter:in
4 Beiträge seit 2005
vor 18 Jahren

Ich habe noch das .NET 1.1 Framework, da gibt es das

 textBox.Focus(); 

noch nicht. Die 1. Lösung oben steig ich nicht durch, ist mir zu kompliziert... 🙂

O
98 Beiträge seit 2005
vor 18 Jahren

Original von solle
Ich habe noch das .NET 1.1 Framework, da gibt es das

 textBox.Focus();   

noch nicht. Die 1. Lösung oben steig ich nicht durch, ist mir zu kompliziert... 🙂

ist eigentlich ganz einfach, du kopierst die Funktion in dein Project und in der Pageload rufst du dann die Funktion auf und übergibst einfach die TextBox,die den Focus haben soll...voila

du kannst auch andere Controls übergeben, muss nicht unbeding ne TextBox sein

N
4.644 Beiträge seit 2004
vor 18 Jahren
private void Page_Load(object sender, System.EventArgs e)
{
	string myCallback =
		"<script language=\"javascript\">\\n" +
		"function Show()\\n" +
		"{{\\n" +
		"   var textbox = document.getElementById ('{0}');\\n" +
		"   if( textbox != null )\\n" +
		"       textbox.focus();\\n" +
		"}}\\n" +
		"</script>";
		this.RegisterClientScriptBlock( "key", string.Format( myCallback, myTextBox.ClientID ));
	HtmlGenericControl myBody = (HtmlGenericControl)this.FindControl( "BODY" );
	myBody.Attributes.Add( "onload", "Show()");
}

Im HTML muss folgendes im Body Tag stehen.

<body id="BODY" runat=server>
S
solle Themenstarter:in
4 Beiträge seit 2005
vor 18 Jahren

Hallo Noodles,
ich habe gerade deine Lösung ausprobiert habe einen Fehler gefunden, man muss die
"n" weglassen (war mal \n oder ?). Mit dem Code gehts jetzt:


private void Page_Load(object sender, System.EventArgs e)
		{
			string myCallback =
				"<script language=\"javascript\">" +
				"function Show()" +
				"{{" +
				"   var textbox = document.getElementById ('{0}');" +
				"   if( textbox != null )" +
				"       textbox.focus();" +
				"}}" +
				"</script>";
			this.RegisterClientScriptBlock( "key", string.Format( myCallback, TextBox1.ClientID ));
			HtmlGenericControl myBody = (HtmlGenericControl)this.FindControl( "BODY" );
			myBody.Attributes.Add( "onload", "Show()");
		}


DANKE !!! 😉

N
4.644 Beiträge seit 2004
vor 18 Jahren

Ja das war mal "\n", hatte nicht nochmal hingeschaut nach dem posten.
Wenn "\n" da steht funktioniert es natürlich auch.

Ich habe es mal berichtigt.