Link to home
Start Free TrialLog in
Avatar of Tina_Bhole
Tina_Bhole

asked on

How to make java script and update panels work together in asp.net

Hello,

In my asp.net web application, I have used java script and update panels(script manager) together. Java script functions are getting called and working fine on the web pages which don't have update panels. Where ever I have used them together, java script function does  not get called.

Could someone please help.
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

There is nothing explicit about having a scriptmanager and updatepanel on a page that would stop JavaScript in general from working. There has to be something about the way your JavaScript is interacting with the page that is being broken, overridden or otherwise mis-aligned with the processes called from the AJAX controls.

You need to show some code, and example of the type of operations involved and code that is breaking for us to be any help to you here. Please try to set it up in such a way as we have all the information to make it duplicatable.
Avatar of Tina_Bhole
Tina_Bhole

ASKER

Thanks for your response ddayx10.

Please see the following code:

 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Downloads_Manager.aspx.cs" Inherits="VA.Downloads_Manager" Title="Downloads Manager" MasterPageFile="~/Site.master" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:ScriptManager ID="SM1" runat="server" EnablePageMethods="true" EnablePartialRendering="true" EnableScriptLocalization="true" EnableViewState="true"> </asp:ScriptManager>

<asp:UpdatePanel ID="UPmain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
        <table cellpadding="0" cellspacing="0" border="0" class="defaultpg">
            <tr>
                <td>
                     <table cellpadding="0" cellspacing="0" border="0" class="productmenu5">
                          <tr>
                                <td><asp:Button Text="Upload Files" CssClass="menutb" ID="btnUpload" OnClick="ShowUpload" runat="server"/></td>
                                <td><asp:Button Text="Create User" CssClass="menutb"  ID="btnCreateUser" OnClick="showCreateUser"  runat="server" /></td>
                                <td><asp:Button Text="Retrieve Password" CssClass="menutb"  ID="btnRetrievePassword" OnClick="showRetrievePassword"  runat="server" /></td>
                                <td><asp:Button Text="Remove Files" CssClass="menutb"  ID="btnRemoveFiles" OnClick="showRemoveFiles" runat="server" /></td>
                                <td><asp:Button Text="Delete Records" CssClass="menutb"  ID="btnDeleteRecords" OnClick="showDeleteRecords" runat="server" /></td>
                         </tr>
                     </table>
                </td>
            </tr>
            <tr>
                <td class="contents">
                <asp:UpdatePanel ID="UPUpload" runat="server" UpdateMode="Conditional">
                        <Triggers>
                           <asp:PostBackTrigger  ControlID="btnuploadfile"/>
                        </Triggers>
                        <ContentTemplate>
                                <table cellpadding="0" cellspacing="0" border="0" class="smallform1left">
                                    <tr>
                                        <td colspan="2" > &nbsp;&nbsp;<asp:FileUpload runat="server" CssClass="inputfile" ID="inputfile" /></td>
                                    </tr>
                                    <tr><td class="bottommargin">.</td></tr>
                                    <tr>
                                        <td class="lbl" >&nbsp;&nbsp;&nbsp;&nbsp;Description</td>
                                        <td><asp:TextBox runat="server" ID="txtfiledesc" CssClass="txtboxextralong"></asp:TextBox></td>
                                    </tr>
                                    <tr><td class="bottommargin">.</td></tr>
                                    <tr>
                                        <td class="floatleft" colspan="2"><asp:Button runat="server" ID="btnuploadfile" Text="Upload" CssClass="btn2sml" OnClick="UploadFile" /></td>
                                    </tr>
                                </table>
                        </ContentTemplate>
                        </asp:UpdatePanel>
              </td>
            </tr>
        </table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>


-------------------------------------------code behind----------------------------------------------------


protected void UploadFile(object sender, EventArgs e)
        {
            if (txtfiledesc.Text.Trim() != "")
            {
                if ((inputfile.PostedFile != null) && (inputfile.PostedFile.ContentLength > 0))
                {
                    try
                    {
                        string fn = System.IO.Path.GetFileName(inputfile.PostedFile.FileName);
                        string savelocation = Server.MapPath("Downloads\\Hotfixes") + "\\" + fn;
                        inputfile.PostedFile.SaveAs(savelocation);

                        SqlConnection mySqlConnection = new SqlConnection(GlobalClass.ConnectionStr);
                        mySqlConnection.Open();
                        SqlCommand mySqlCommand = new SqlCommand(GlobalClass.I_Uploads, mySqlConnection);
                        mySqlCommand.Parameters.AddWithValue("@filename", inputfile.PostedFile.FileName);
                        mySqlCommand.Parameters.AddWithValue("@filedesc", txtfiledesc.Text);
                        mySqlCommand.ExecuteNonQuery();
                        mySqlCommand.Dispose();
                        mySqlConnection.Close();

                        txtfiledesc.Text = "";
                        UPUpload.Update();
                        GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert('File has been Uploaded Successfully.');</script>";
                        ScriptManager.RegisterStartupScript(btnuploadfile, btnuploadfile.GetType(), "fileuploadScript1", "GlobalClass.JScript_Alert", true);
                        
                        
                    }
                    catch (Exception exp)
                    {
                        GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert(" + exp.Message.ToString() + ");</script>";
                        ScriptManager.RegisterStartupScript(this,Page.GetType(),"courseschScript1", GlobalClass.JScript_Alert,true);
                    }
                }
            }
            else
            {
                GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert('Please add a Description for the File being Uploaded');</script>";
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "courseschScript2", GlobalClass.JScript_Alert,true);
            }
        }

Open in new window

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Downloads_Manager.aspx.cs" Inherits="VA.Downloads_Manager" Title="Downloads Manager" MasterPageFile="~/Site.master" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<asp:ScriptManager ID="SM1" runat="server" EnablePageMethods="true" EnablePartialRendering="true" EnableScriptLocalization="true" EnableViewState="true"> </asp:ScriptManager>

<asp:UpdatePanel ID="UPmain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
        <table cellpadding="0" cellspacing="0" border="0" class="defaultpg">
            <tr>
                <td>
                     <table cellpadding="0" cellspacing="0" border="0" class="productmenu5">
                          <tr>
                                <td><asp:Button Text="Upload Files" CssClass="menutb" ID="btnUpload" OnClick="ShowUpload" runat="server"/></td>
                                <td><asp:Button Text="Create User" CssClass="menutb"  ID="btnCreateUser" OnClick="showCreateUser"  runat="server" /></td>
                                <td><asp:Button Text="Retrieve Password" CssClass="menutb"  ID="btnRetrievePassword" OnClick="showRetrievePassword"  runat="server" /></td>
                                <td><asp:Button Text="Remove Files" CssClass="menutb"  ID="btnRemoveFiles" OnClick="showRemoveFiles" runat="server" /></td>
                                <td><asp:Button Text="Delete Records" CssClass="menutb"  ID="btnDeleteRecords" OnClick="showDeleteRecords" runat="server" /></td>
                         </tr>
                     </table>
                </td>
            </tr>
            <tr>
                <td class="contents">
                <asp:UpdatePanel ID="UPUpload" runat="server" UpdateMode="Conditional">
                        <Triggers>
                           <asp:PostBackTrigger  ControlID="btnuploadfile"/>
                        </Triggers>
                        <ContentTemplate>
                                <table cellpadding="0" cellspacing="0" border="0" class="smallform1left">
                                    <tr>
                                        <td colspan="2" > &nbsp;&nbsp;<asp:FileUpload runat="server" CssClass="inputfile" ID="inputfile" /></td>
                                    </tr>
                                    <tr><td class="bottommargin">.</td></tr>
                                    <tr>
                                        <td class="lbl" >&nbsp;&nbsp;&nbsp;&nbsp;Description</td>
                                        <td><asp:TextBox runat="server" ID="txtfiledesc" CssClass="txtboxextralong"></asp:TextBox></td>
                                    </tr>
                                    <tr><td class="bottommargin">.</td></tr>
                                    <tr>
                                        <td class="floatleft" colspan="2"><asp:Button runat="server" ID="btnuploadfile" Text="Upload" CssClass="btn2sml" OnClick="UploadFile" /></td>
                                    </tr>
                                </table>
                        </ContentTemplate>
                        </asp:UpdatePanel>
              </td>
            </tr>
        </table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>


-------------------------------------------code behind----------------------------------------------------


protected void UploadFile(object sender, EventArgs e)
        {
            if (txtfiledesc.Text.Trim() != "")
            {
                if ((inputfile.PostedFile != null) && (inputfile.PostedFile.ContentLength > 0))
                {
                    try
                    {
                        string fn = System.IO.Path.GetFileName(inputfile.PostedFile.FileName);
                        string savelocation = Server.MapPath("Downloads\\Hotfixes") + "\\" + fn;
                        inputfile.PostedFile.SaveAs(savelocation);

                        SqlConnection mySqlConnection = new SqlConnection(GlobalClass.ConnectionStr);
                        mySqlConnection.Open();
                        SqlCommand mySqlCommand = new SqlCommand(GlobalClass.I_Uploads, mySqlConnection);
                        mySqlCommand.Parameters.AddWithValue("@filename", inputfile.PostedFile.FileName);
                        mySqlCommand.Parameters.AddWithValue("@filedesc", txtfiledesc.Text);
                        mySqlCommand.ExecuteNonQuery();
                        mySqlCommand.Dispose();
                        mySqlConnection.Close();

                        txtfiledesc.Text = "";
                        UPUpload.Update();
                        GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert('File has been Uploaded Successfully.');</script>";
                        ScriptManager.RegisterStartupScript(btnuploadfile, btnuploadfile.GetType(), "fileuploadScript1", "GlobalClass.JScript_Alert", true);
                        
                        
                    }
                    catch (Exception exp)
                    {
                        GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert(" + exp.Message.ToString() + ");</script>";
                        ScriptManager.RegisterStartupScript(this,Page.GetType(),"courseschScript1", GlobalClass.JScript_Alert,true);
                    }
                }
            }
            else
            {
                GlobalClass.JScript_Alert = "@<script language='javascript' type='text/javascript'>alert('Please add a Description for the File being Uploaded');</script>";
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "courseschScript2", GlobalClass.JScript_Alert,true);
            }
        }

Open in new window

OK I see what's wrong. I have to do something else first then I'll come back and explain in about 1/2 hour :)
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WOW!!! thank you so much. It's working now :)
The explanation was excellent. Thank you very much