C Sharper
C Sharper

Reputation: 8626

Not able to pass eval to javascript function

I have following imagebutton in gridview:

<asp:TemplateField HeaderText="Edit" ControlStyle-CssClass="smallTxt"
                                HeaderStyle-CssClass="smallTxt">
                                <ItemTemplate>

                                <asp:ImageButton runat="server" ImageUrl="../images/yellow_arrow.gif" ID="imgbtnsearch"
                     OnClientClick="javascript:setCustID('<% Eval(idCustomer)%>');return false;" />


                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>

Through this image button I am calling a Javascript function. See code below.

 function setCustID(cid) {
        alert(cid);
        ShowHideControl('ctl04_divEdit');
        document.getElementById('ctl04_hdnIdCustomer').value = cid;


    }

When I run this, instead of getting Eval(idCustomer) result in figures (in alert box of javascript), I am getting:

<% Eval(idCustomer).toString()%>

When I put # while passing parameter as : <%#Eval(idCustomer)%>

OnClientClick="javascript:setCustID('<%#Eval(idCustomer)%>');return false;" 

Function alerts:

<%#Eval(idCustomer)%>

Upvotes: 0

Views: 812

Answers (2)

Andrei
Andrei

Reputation: 56688

The move with # was the right one. Besides not to get lost with quotation (as I did before the edit) call string.Format to deal with js expression:

OnClientClicking='<%# string.Format("javascript:setCustID({0});return false;", Eval("idCustomer")) %>'

Also make sure to wrap function parameter with single quotes if this is a string. Leave it as it is if this is an integer though.

Upvotes: 3

Ash
Ash

Reputation: 1422

Sorry ,As I am not not so good in ASP, but for Eval: I think it should not pass as String : "Eval(string)" instead this you may use Eval("string").

Upvotes: 1

Related Questions