Setting Focus on a TextBox and sellecting the text in a DataGrid with TextChanged
-
I have a datagrid containing 2 coulmns with 1 TextBox in each. On the first TextBox I have a TextChanged event. After the event is fired I want to set Focus on the next textBox, and sellect the text on it.
private void OnSubscriptionToValueChanged(object sender, System.EventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Trim() != "VAT") { tb.CssClass = "tbStyle0"; DataGridItem dgi = (DataGridItem)tb.Parent.Parent; TextBox tbToFocus = (TextBox)dgi.FindControl("tbNextValue"); tbToFocus.Focus(); // Here it sets focus but it don't select the test } else { tb.Focus(); // Here it sets focus but it don't select the test tb.CssClass = "tbStyleError"; } }
The code over sets Focus on the control I want, but it don't select the text (as it would if the TextChanged event wasen't there when you tab from one TextBox too another). Has anyone an idèe how to do that? Thanks Thomas -
I have a datagrid containing 2 coulmns with 1 TextBox in each. On the first TextBox I have a TextChanged event. After the event is fired I want to set Focus on the next textBox, and sellect the text on it.
private void OnSubscriptionToValueChanged(object sender, System.EventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Trim() != "VAT") { tb.CssClass = "tbStyle0"; DataGridItem dgi = (DataGridItem)tb.Parent.Parent; TextBox tbToFocus = (TextBox)dgi.FindControl("tbNextValue"); tbToFocus.Focus(); // Here it sets focus but it don't select the test } else { tb.Focus(); // Here it sets focus but it don't select the test tb.CssClass = "tbStyleError"; } }
The code over sets Focus on the control I want, but it don't select the text (as it would if the TextChanged event wasen't there when you tab from one TextBox too another). Has anyone an idèe how to do that? Thanks Thomas -
Page.RegisterStartupScript("focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.RegisterStartupScript("select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>");
Visual Studio 2005 says that the function is absolite, so one should rather use:
Page.ClientScript.RegisterStartupScript(GetType(), "focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.ClientScript.RegisterStartupScript(GetType(), "select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>");
Thomas -
Visual Studio 2005 says that the function is absolite, so one should rather use:
Page.ClientScript.RegisterStartupScript(GetType(), "focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.ClientScript.RegisterStartupScript(GetType(), "select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>");
ThomasAnd to expand on your solution. You should just strip off the <script> tags and add a fourth parameter with the value of true. This will ensure that the <script> tags are added automatically with the type attribute of the tag set to text/javascript which will make the output XHTML compliant.
Page.ClientScript.RegisterStartupScript(GetType(), "focus", "document.getElementById('" + tbToFocus.UniqueID + "').focus();", true); Page.ClientScript.RegisterStartupScript(GetType(), "select", "document.getElementById('" + tbToFocus.UniqueID + "').select();", true);
Kelly Herald Software Developer