Solved

Using 'RegisterClientScriptBlock' from server side

Posted on 2009-04-07
1
295 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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now