Solved

Using 'RegisterClientScriptBlock' from server side

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

808 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