Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript alert function works on second click.. i am putting script on master page. and on add to cart button i am calling that alert fuction. but on first click on add to cart button its doesn't work.. but on second click. A clear box pops up and works fine

i am just finding the button id from gridview
C#
for (int i = 0; i < GridView1.Rows.Count; i++)
           {
               Button btn = (Button)GridView1.Rows[i].FindControl("btnaddcart");
               //Change the property settings for the button in your TemplateField
               btn.Attributes["onclick"] = "showstock()";
           }

and i am calling that JavaScript function which is on gridview. i just want that it works on first click. but ist working on second click to add to cart button.

the JavaScript code is

C#
<script type="text/javascript" language="javascript">


       function showstock()
       {
           alert("Sorry This Product is not in stock!");

       }


   </script>
Posted
Updated 16-Feb-12 18:33pm
v3

Hi, You have called js function at wrong place, pls write below code in rowdatabound event of gridview.

C#
btn.Attributes.Add("onclick","showstock();");
   
Comments
Varun Sareen 17-Feb-12 0:44am
   
good one sarvesh jee
codegeekalpha 26-Feb-12 20:15pm
   
its not working. same problem...with that
Dear Friend,

Try to put your code like this:-

btn.Attributes.Add("onclick","showstock();");


I hope this will work out for solution.

Thanks
   
Comments
codegeekalpha 26-Feb-12 20:15pm
   
nop its not working... same issues is also with this..
where are you trying to run this loop to add buttons.
are you doing a databind on gridview after that ?

If u add these 'buttons calling java-script function' on gridview.rowcreated event, they should work.

try something like below (just wrote it not tested)

C#
void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      Button btn= (Button)e.Row.FindControl("btnaddcart");
      
      btn.Attributes["onclick"] = "showstock()";
    }

  }
   
v2

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