dshi15
asked on
html setfocus on textarea
Hi Expert,
I have a survey.aspx.vb page
strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Tri
lblQuestions.Text = strLabel
If user not put anyting in textarea, I want set focus on this textarea, how I can do it?
Thanks in adance.
ASKER
I want add some code in my exiting code, I just don't know how to add
My code like that
If strCurrComment ="" then
strLabel = strLabel.Trim & "<textarea setfocus=true rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Tri m & """>" & strCurrComment.Trim & "</textarea></td></tr>"
Else
strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Tri m & """>" & strCurrComment.Trim & "</textarea></td></tr>"
End if
How to replace "setfocus=true" and let it happen?
I had asp button Submit on this page.
My code like that
If strCurrComment ="" then
strLabel = strLabel.Trim & "<textarea setfocus=true rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Tri
Else
strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Tri
End if
How to replace "setfocus=true" and let it happen?
I had asp button Submit on this page.
When you render this on page means when it will become part of the page right after that call like this
Textbox1.Focus()
but control should be run at server. Why you are not adding dynamic control because after that you can easily call this
Textbox1.Focus()
but control should be run at server. Why you are not adding dynamic control because after that you can easily call this
like this
after the last line from above call this
txtArea.focus()
where txtArea is ID of the control that we are adding
Dim txtBox As New TextBox
txtBox.ID = "txtArea"
txtBox.TextMode = TextBoxMode.MultiLine
txtBox.Columns = 10
form1.Controls.Add(txtBox)
after the last line from above call this
txtArea.focus()
where txtArea is ID of the control that we are adding
ASKER
I worked on already exiting code, I don't want make big change on somebody else code.
@dshi15: please sent me next few line of code also where this string is going to render on HTML. because after when this textarea will be part of the HTML we can apply the solution then hope you are getting my point
I implemented following application here at my machine for you that is running fine.
Add this in <Head> tag of your application
Where "myTextArea" would be textarea ID
If you already have some javascript funcation added in the head section then simply add the following function in <script> tag
then I put an asp.net button on page and on click event I called a server side event and add a textarea from code behind into the page
like this
here lblMsg is my already added .net control in which I am adding textarea.
when it become part of the page then I am calling javascript function for cursor setting from code behind like this
This will wait just for 2 second so that page load successfully to call client side method and can get textarea that we added.
Hope that will help you.
Regards,
AAK
Add this in <Head> tag of your application
<script language="javascript" type="text/javascript">
function setCursor() {
document.getElementById('myTextArea').focus();
}
</script>
Where "myTextArea" would be textarea ID
If you already have some javascript funcation added in the head section then simply add the following function in <script> tag
function setCursor() {
document.getElementById('myTextArea').focus();
}
then I put an asp.net button on page and on click event I called a server side event and add a textarea from code behind into the page
like this
lblMsg.Text = "<textarea id='myTextArea' setfocus=true rows='7' cols='100' name='Comment'> Hello </textarea>";
here lblMsg is my already added .net control in which I am adding textarea.
when it become part of the page then I am calling javascript function for cursor setting from code behind like this
string jScript = "<script>setTimeout('setCursor()',2000)</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "jScript", jScript);
This will wait just for 2 second so that page load successfully to call client side method and can get textarea that we added.
Hope that will help you.
Regards,
AAK
As you are looking for VB .Net then you can use like this little change according to your code
Add this in head TAG
And where you have these line of code
Right after this add these lines
That all and here we go just test this :)
Add this in head TAG
<script language="javascript" type="text/javascript">
function setCursor(textAreaId) {
document.getElementById(textAreaId).focus();
}
</script>
And where you have these line of code
Label = strLabel.Trim & "<textarea setfocus=true rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"
Else
strLabel = strLabel.Trim & "<textarea rows=""7"" cols=""100"" style=""" & strStyle.Trim & """ name=""Comment" & intQuestionNo.ToString.Trim & """>" & strCurrComment.Trim & "</textarea></td></tr>"
End if
Right after this add these lines
Dim strScript As String = "<script>setTimeout('setCursor('Comment"&intQuestionNo.ToString.Trim&"')',2500)</script>"
Page.RegisterStartupScript(this.GetType(),strScript)
That all and here we go just test this :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much and I will try it and let you know tomorrow.
Open in new window
Live demo
http://www.java2s.com/Tutorial/JavaScript/0200__Form/Textareafocus.htm