Solved

Using 'RegisterClientScriptBlock' from server side

Posted on 2009-04-07
1
299 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

840 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