Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
javascript function is not working. how to solve this problem:


aspx Page:

JavaScript
 <div>
     <style type="text/css">
  .black_overlay{
display:none;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color:black;
z-index:1001;
-moz-opacity: 0.8;
opacity:.80;
filter: alpha(opacity=80);
}

  .popup {
    width:439px;
    height:365px;
    position:fixed;
    top:50%;
    left:36%;
    right:10%;
    bottom:50%;
    margin:-50px 0 0 -100px; /* [-(height/2)px 0 0 -(width/2)px] */
    z-index:2222;
    background-color:#FFFFFF;
     color:White;
    visibility:hidden;
  }
    .style1
    {
        height: 15px;
    }
    .style2
    {
        width: 40%;
        height: 97px;
    }
    .style3
    {
        height: 97px;
    }
</style>

<script type="text/javascript">
    
    function show_popup(pric) {
        //calling from code behind...
       
        var popup = document.getElementById('<%=popup.ClientID %>');
        popup.style.visibility = 'visible';
        document.getElementById('Div1').style.display = 'block';
        document.getElementById('<%=lblnotice.ClientID %>').innerHTML = pric;

        return false;
    }
    function hidePopup() {
        var popup = document.getElementById('<%=popup.ClientID %>');
        popup.style.visibility = 'hidden';
        document.getElementById('Div1').style.display = 'none';
        return false;
    }
</script>
<asp:DataList ID="dl" runat="server" OnItemDataBound="item_bound">
<itemtemplate>
<asp:Label ID="ru" Text='<%# DataBinder.Eval(Container.DataItem,"name")%>' runat="server">
 
                   <asp:LinkButton ID="lnkviewcart" CausesValidation="true" Enabled="true"  CommandName="imgclicked" Visible="false" runat="server"  Text="See Price in Cart" >
</itemtemplate>

  <div id="Div1" class="black_overlay">
                  </div>
                  <div id="popup" align="center" class="popup"  runat="server">
                    
  <table width="100%" style="removed50%;">
  <tr><td><hr style=" height:10px; background-color:#ff0000;" /></td></tr>
  <tr align="center" style="  width:90%;"><td align="left" style=" color:#565454;"><table width="100%"><tr><td style="font-family:Verdana; font-size:16px; color:#565454; ">Your Price @ Lee's Tools</td> 
  <td><asp:Label ID="lblnotice" runat="server" ForeColor="#ff0000" Font-Names="Verdana" Font-Size="15px" Font-Bold="true"></td></tr></table></td></tr>
  <tr><td><hr style=" height:4px; background-color:#ff0000;" /></td></tr>

  <tr><td></td></tr>

   <tr>
   <td align="right">
   <table width="100%">
   <tr><td align="left"><asp:ImageButton ID="imgclose" runat="server" ImageUrl="../Image/closebut.png" OnClientClick="return hidePopup();" /> </td>
   <td align="right"><asp:ImageButton ID="imgpopadd" runat="server" ImageUrl="../Image/Addtocart/cart_button.png" OnClick="addtocart_clicked_popup" /> </td>
   </tr>
   </table>
   </td></tr>
   <tr><td></td></tr>
  </table>
 
</div>



aspx.cs:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace popjavascript
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }

        }

        public void Bind()
        {
            DataTable dt = new DataTable("new");
            dt.Columns.Add("name");
            dt.Columns.Add("price");
            dt.Rows.Add("christh", "100.00");


            //  dt.Rows.Add("100.00","price");
            dl.DataSource = dt;
            dl.DataBind();
        }
        protected void addtocart_clicked_popup(object sender, ImageClickEventArgs e)
        {

            Response.Write("hello");
        }

        protected void item_bound(object sender, DataListItemEventArgs e)
        {
            LinkButton linkmp = (LinkButton)e.Item.FindControl("lnkviewcart");
            Label lblprice = (Label)e.Item.FindControl("ru");
          
            linkmp.Visible = true;
            linkmp.OnClientClick = "retun show_popup(' " + lblprice.ToString().Trim() + "'); retun false;";

        }
    }
}



help needed.
Posted
Updated 6-Dec-13 2:31am
v5
Comments
Richard MacCutchan 6-Dec-13 8:32am    
With what? you have not explained what the problem is. Please use the Improve question link and add some proper detail.
Sampath Lokuge 6-Dec-13 8:41am    
Check whether is there any errors on your js by using chrome dev tools ?
ZurdoDev 6-Dec-13 9:52am    
Why doesn't it work?
govardhan4u 6-Dec-13 10:48am    
try with script manager
ScriptManager.RegisterStartupScript(
Page,
Page.GetType(),
"uniquescriptkey",
"alert('hi');",
true
);
christhuxavier 6-Dec-13 11:22am    
hi Richard, Sampath,

I am sorry for not clear..i mean my javascript function is not invoking when i click on click button"see price in cart"

1 solution

christhuxavier wrote:
I am sorry for not clear... I mean my Javascript function is not invoked when I click on click button "See Price in Cart".
[text modified to fix a number of mistakes — SA]
It does not work just because you did not write a single word of code to invoke it.

One of the many ways to do that is this (with the help of jQuery library):
JavaScript
$(document).ready(function() {
    $("#lnkviewcart").click(function() { // where id="lnkviewcart" for 
       // do something on click, can use $(this), which is a jQuerty wrapper of the clicked element
    });
});


See also:
http://api.jquery.com/id-selector/[^],
http://api.jquery.com/category/selectors/[^].

To write proper id attribute via ASP.NET, see also: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900