Calling Javascript from ASP.Net

Results 1 to 2 of 2

Thread: Calling Javascript from ASP.Net

  1. #1
    Join Date
    Dec 1969

    Default Calling Javascript from ASP.Net

    I am trying to set which Control gets the focus after postback. I want to run some server side code after the user modifies a textbox so I have the AutoPostback attribute of the textbox set to true. After the PostBack I want a different control to have the focus. I created a javascript function and used the Add Attributes method to call the function once the Textbox is changed. <BR><BR>When I look at the html that is generated, this produces two different actions for the OnChange event - one for my javascript and one for the postback. (See code at bottom) Only the first is handled and therefore the PostBack no longer occurs when I after the user modifies the textbox. How can I get both the PostBack and Focus script to run after a textbox is changed? Thanks.<BR><BR><BR>private void Page_Load(object sender, System.EventArgs e)<BR>{<BR> if (!IsClientScriptBlockRegistered("FocusScript"))<BR > {<BR> StringBuilder strBldr = new StringBuilder();<BR> strBldr.Append(" &#060;script language=&#039;javascript&#039;&#062; "); <BR> strBldr.Append(" function setFocus(obj) {"); <BR> strBldr.Append(" document.getElementById(obj).focus();} "); <BR>strBldr.Append(" &#060;/script&#062; "); <BR> RegisterClientScriptBlock("FocusScript", strBldr.ToString()); <BR> }<BR> TextBox1.Attributes.Add("onchange","setFocus(&#039 ;TextBox2&#039;)");<BR> }<BR><BR><BR><BR>&#060;input name="TextBox1" type="text" tabindex="1" onchange="setFocus(&#039;TextBox2&#039;)" onchange="__doPostBack(&#039;TextBox1&#039;,&#039; &#039;)" ....<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Calling Javascript from ASP.Net

    when you&#039;ve posted back, couldn&#039;t you add the call to the onload attribute of the body tag and set the focus from there?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts