Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to register a javascript function when i am using Ajax update panel?

Posted on 2008-10-21
2
Medium Priority
?
960 Views
Last Modified: 2012-05-05
hi all,
i need to do the following:
1. when every time there is an error message that's need to be displayed on the web page, make the message flashing for a few seconds. i am using a javascript function to achieve it.
2. i am using ajax update panel to control the postback. but i found the javascript function won't work if i am using this update panel. i googled on internet and found i need to register the javascript function, but i don't know where to do this registration and how to do it. can anyone help me to solve this problem?

thanks a lot
viola
<head runat="server">
    <script src="gen_lib.js" type="text/javascript"></script> //call this javascript file
</head>
<body onload="Init()"> //call this function to display Messages on the form
<form id="form1" runat="server">
    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" ScriptMode=Release />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
    <ContentTemplate>
    <asp:Label ID="status_label" runat="server" CssClass="note_item"></asp:Label>  //the message displayed in 'status_label' won't flash at all
 
<br/>
    <asp:Button ID="btnSave" runat="server" Text="Save"   OnClick="btnSave_Click" />
</ContentTemplate>
   
 
    </asp:UpdatePanel>
    </form>
</body>
 
--------------------------------
protected void btnSave_Click(object sender, EventArgs e)
    {
        SetStatus("My Details saved.");
    }
 
private void SetStatus(string msg)
    {
      status_label.Text="<input type=\"hidden\" id=\"flash_on\" value=\"true\">"+msg;
      
    }
 
--------------------------------
in my gen_lib.js file:
function Init()
{
  var status, exec_elem, ro_elems, ro_elem, objs, i, menu_id;
 
  //alert("Init...");
  status=document.all.status_label;
  if (status!=null && document.all.flash_on!=null)
  {
    if (status!=null && status.innerText!="" && document.all.flash_on.value=="true")
    {
      Init_Flash("#ffffff", "#00dd00", status);
    }
    if (status!=null && status.innerText!="" && document.all.flash_on.value=="error")
    {
      Init_Flash("#ffffff", "#ff0000", status);
    }
    if (status!=null && status.innerText!="" && document.all.flash_on.value=="warning")
    {
      Init_Flash("#ffffff", "#999900", status);
    }
    document.all.flash_on.value="false";
  }
    //do some other flash job......
}

Open in new window

0
Comment
Question by:viola123
[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
2 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 2000 total points
ID: 22773277
Just try this...

1. Give reference to the js file (if you are using it) from scriptmanager as

    <asp:ScriptManager ID="scpMgr" runat="server">
                             <Scripts>
                                  <asp:ScriptReference Path="js/MyScriptfile.js" />
                            </Scripts>
  </asp:ScriptManager>

2. And to call it from your code behind


                     ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "OpenWindow", "fnOpenWindow('myMsg');", True)
 

This is to call javascript function from code behind.

also refer this link for details
http://msdn.microsoft.com/en-us/magazine/cc163354.aspx
0
 
LVL 6

Author Comment

by:viola123
ID: 22773414
hi,
it works great!!!!
what i am doing are:
1. reference the javascript file in ScriptManager
2. register the founction in page_load event

thanks heaps
<head runat="server">
    <link href="../../../templates/styles.css" type="text/css" rel="stylesheet" />
</head>
<body onload="Init()" >
    <form id="form1" runat="server">
//reference it in ScriptManager
    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" ScriptMode=Release>
    <Scripts>
          <asp:ScriptReference Path="../../../templates/gen_lib.js" />
    </Scripts>
    </ajaxToolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
    <ContentTemplate>
    <asp:Label ID="status_label" runat="server" CssClass="note_item"></asp:Label>  //the message displayed in 'status_label' won't flash at all
 
<br/>
    <asp:Button ID="btnSave" runat="server" Text="Save"   OnClick="btnSave_Click" />
</ContentTemplate>
   
 
    </asp:UpdatePanel>
    </form>
</body>
-------------------------------------------
protected void Page_Load(object sender, EventArgs e)
    {
       //register it in page_load event
       ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Init", "Init();", true);
    }

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

664 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