Calling a client side control event from a server side function


I would like to know how to raise an event on a client side only control from a server side function. 
 For example, I have Button1 whose onclick is handled at the server. The logic behind the function will do 1 of 2 things, change the text of a label to indicate a problem or navigate to a new page.  I cannot use redirect because i need the new page to appear inside 2 of 3 existing frames.  I can manage this using an anchor on the client.  All I need to do is automatically click the anchor after I post back from the Button1.click event.
I tried the following line of code at the end of the Button1.click function with no success. 
RegisterClientScriptBlock("RaiseClientSideEvent", "<script language=""JavaScript"">document.getElementById(""MrShowCal"").click();</script>")
My environment is .NET 1.1 with IE6.
Any ideas?

Bill
0
wbraun30
9/13/2006 4:54:07 PM
📁 asp.net.client-side
📃 24353 articles.
⭐ 2 followers.

💬 3 Replies
👁️‍🗨️ 1532 Views


You're on the right track. Use RegisterStartupScript instead of RegisterClientScriptBlock. What i'm not sure, is if the JavaScript you have inside will work. Instead use a window.location.href = "locationgoeshere.aspx"; If you're trying to redirect a frame, then use window.frames[0].window.location.href = 'locationgoeshere.aspx';

JoNaS
MVP - Microsoft Most Valuable Professional
MCAD - Microsoft Certified Application Developer
http://www.jstawski.com
0
jstawski
9/13/2006 6:25:59 PM

You need to call RegisterStartupScript so that the script will be run after the page has been loaded.  This ensures that your DOM is fully loaded and you can manipulate any element on the page.
 &nbsp;
  Then in your page (or better yet in a separate file so it will be cached) , create a function called: 
 
function someFunc() {
   //dostuff 
}

Ben Scheirman
http://www.flux88.com

ASP.NET MVP
Certified ScrumMaster
ASPInsider
MCSD
Page.RegisterStartupScript(&quot;some_key&quot;, &quot;&lt;script language=&#39;Javascript&#39;&gt;someFunc();&lt;/script&gt;&quot;)
 
0
subdigital
9/14/2006 4:54:31 PM

 Thanks Ben. I got it to work.  I put this line in the code behind file:
       RegisterStartupScript("RaiseClientSideEvent", "<script language=""JavaScript"">RedirectFunc();</script>")
I put this script in the pg
       function RedirectFunc() {
              document.getElementById('<%=MrShowCal.ClientId%>_MyHref').click();
       }
Also, to get the id right, I had to look at the source.  MrShowCal is a web user controld that contains Protected MyHref As HtmlControls.HtmlAnchor and so _MyHref was appended to the id.

Bill
0
wbraun30
9/14/2006 5:12:24 PM
Reply: