Solved

Using 'RegisterClientScriptBlock' from server side

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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 …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

730 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