Solved

Using 'RegisterClientScriptBlock' from server side

Posted on 2009-04-07
1
303 Views
Last Modified: 2012-05-06
Hello,

I am writing a C# asp.net application.
I have a page which has a "ul" list element, and some "li" items in it.

In addition, I am using a AJAX timer to refresh the data.
Since the selected item is reset after the timer function is called, i try to set the selectedItem again:

protected void Timer_Tick(object sender, EventArgs e)
{
  //do something
  string selectedTab = selectedTabHidden.Value;
  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showTab", "selectTab('" + selectedTab + "');", true);
}

I am calling a javascript function which should highlight the selected item:

function selectTab(tabid)
        {
         
                var arrTabs = new Array("tabCounters","tabTopFeed");
                for (var currTab in arrTabs)
                {
                    document.all(arrTabs[currTab]).className = "notSelected";
                }  

                document.all("selectedTabHidden").value = tabid;
               
                document.all(tabid).className = "selected";
         
        }

The problem is that when calling the javascript, the page's items are not yet recognized, rendered. So that 'document.all(arrTabs["tabTopFeed"])', for example, is null and I get an exception.

How can I fix that?

Thanks!

<body onload="javascript:selectTab('tabCounters')" bgcolor="#dfdfdf">
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div id="navcontainer">
        <ul id="tablist" runat="server">
            <li><a id="tabCounters" href="ModuleCounters/view.aspx" onclick="selectTab('tabCounters')" target="main">Counters</a></li>
            <li><a id="tabTopFeed" href="ModuleTopFeed/view.aspx" onclick="selectTab('tabTopFeed')" target="main">Top Feed</a></li>
        </ul>
    </div>
    
    <input type="text" runat="server" id="selectedTabHidden" />
    
    <asp:Timer ID="TimerCounters" runat="server" OnTick="Timer_Tick" Interval="30000" />
     </form>
</body>

Open in new window

0
Comment
Question by:Relegence
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 500 total points
ID: 24090695
Hi,

The reason is the RegisterClientScriptBlock registers the script and when the script is rendered in the browser even the page is not loaded fully, it gets executed. So what you can do is to call the javascript function after a little time.

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showTab", "setTimeout('selectTab('" + selectedTab + "')', 500);", true);
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question