Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

I have asp.net website,

Where i have one small code in javascript.

<script type="text/javascript">
        function GetAlt(a, b, c) {
            alert(a + "|" + b + "|" + c);
        }
    </script>


and i want to call this function on button click in a repeater.
<asp:ImageButton ID="imgEdit" runat="server" CommandArgument='<%# Eval("tc") + "," + Eval("JobName") + "," + Eval("Discount") %>'
                                OnClientClick='<%# "GetAlt(" + Eval("tc") + "," + Eval("JobName") + "," + Eval("Discount") + ");"  %>' ToolTip="Edit" ImageUrl="~/images/Edit.png" Width="20"
                                Height="20" />


on button click its, just getting postback but not getting an popup box.

I changed imagebutton to simple html button.

<input type="button" name="E" id="E" value="E" onclick='<%# "GetAlt(" + Eval("tc") + "," + Eval("JobName") + "," + Eval("Discount") + ");"  %>' />


Trying this code:
Button
<asp:ImageButton ID="imgEdit" runat="server" CommandArgument='<%# Eval("tc") + "," + Eval("JobName") + "," + Eval("Discount") %>'
                                CommandName="Edit" ToolTip="Edit" ImageUrl="~/images/Edit.png" Width="20"
                                Height="20" />


Repeater_Item...
if (e.CommandName == "Edit")
        {
            ImageButton btnedit = (ImageButton)e.Item.FindControl("imgEdit");
            string[] cmdArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
            String strJSFunctionCall = String.Format("return GetAlt({0},{1},{2})", cmdArgs[0], cmdArgs[1], 0);
            btnedit.Attributes.Add("onclick", strJSFunctionCall);
        }


But still i'm not getting this popup.

can any one please help me...



Thanks
Posted
Updated 6-Jun-15 21:16pm
v2

The problem is that the default behaviour of ImageButton is to postback to the server. So what actually happening, that the client code is distracted by the postback and for that the page refreshing and you never see the popup...
What you can do is to stop the postback from happening...
HTML
OnClientClick='<%# "GetAlt(" + Eval("tc") + "," + Eval("JobName") + "," + Eval("Discount") + ");" %>; return false;'

Update the OnClientClick property and add retrun false at the end...It will stop the postback and only you code will run...
 
Share this answer
 
You added the answer at another old question - javascript function in repeater control[^]. You should have added it in this question of yours.
 
Share this answer
 
Thank you guys,

I tried this, i got it...

if (e.CommandName == "Edit")
        {
            ImageButton btnedit = (ImageButton)e.Item.FindControl("imgEdit");
            string[] cmdArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
            String strJSFunctionCall = String.Format("GetAlt({0},{1},{2})", cmdArgs[0], "'"+cmdArgs[1]+"'", 0);
            btnedit.Attributes.Add("onclick", strJSFunctionCall);
        }

its started working when i placed quotes for string.

Thanks Once Again everyone.
 
Share this answer
 

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