How to put an OnClick event by calling a javascript function on a Link inside a GRIDVIEW in ASP.NET?

1

I have a form containing a button that will open a query popup. In this popup, I make a query passing as parameters name and cpf where the query returns, creates a gridview where each record contains a link that will return the data to a java script. My problem is that I can not put a link inside the grid by passing the parameters.

Below my grid and the javascript function call:

<asp:GridView ID="gvwPES" runat="server" AutoGenerateColumns="False" DataKeyNames="PES_ID" AllowPaging="True" AllowSorting="true" PageSize="10" OnPageIndexChanging="gvwPES_PageIndexChanging" CssClass="table table-bordered table-hover table-striped">

    <Columns>
        <asp:TemplateField HeaderText="Cod." Visible="False">
            <ItemTemplate><%#Eval("PES_ID") %>    </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <itemtemplate>
              <%#Eval("PES_NM") %>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Enviar" runat="server" OnClick="javascript:informa_pessoa(<%#Eval("PES_ID") %>,<%#Eval("PES_NM") %>,<%#Eval("PES_DOCID") %>,
                                                     <%#Eval("PES_END") %>,<%#Eval("PES_CEP") %>,<%#Eval("PES_CID") %>,
                                                     <%#Eval("PES_UF") %>,<%#Eval("PES_FON") %>,<%#Eval("PES_FAX") %>,
                                                     <%#Eval("PES_INSCE") %>,<%#Eval("PES_INSCM") %>,<%#Eval("PES_BAI") %>,
                                                     <%#Eval("PES_DOC_IDENT") %>,<%#Eval("CON_ID") %>,<%#Eval("CON_NM") %>,
                                                     <%#Eval("PES_TP") %>,<%#Eval("PES_NUM_END") %>,<%#Eval("PES_COMPLEMENTO")%>,
                                                     <%#Eval("TCP_TP_LOGR") %>,<%#Eval("PES_TP_APR") %>,<%#Eval("SUS_LIM") %>,
                                                     <%#Eval("SUS_DT") %>,<%#Eval("PES_INST_PROT") %>)" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerSettings Mode="NextPrevious" NextPageText="Próximo" PreviousPageText="Anterior" />
</asp:GridView>

Java script:

<script type="text/javascript">
       function informa_pessoa(parametros da popup...) {

           Código da popup...
           window.close();
       }
    </script>
    
asked by anonymous 21.06.2015 / 23:07

1 answer

1

If you want to call function javascript in the click it is with the OnClientClick attribute, although my recommendation is you add a click event in CodeBehind and call there passing the attributes.

If you want to continue in the onClientClick it would look something like this:

<asp:Button ID="Enviar" runat="server" OnClientClick='<%# String.Format("informa_pessoa({0}, '{1}') ", Eval("PES_ID"), Eval("PES_NM")) %>' />

Arrange your Eval , put it according to my example.


The best way would be to use RowCommand , example:

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="Enviar" runat="server" 
                CommandName="Enviar" 
                CommandArgument="<%# Eval("PES_ID") %>"
                Text="Enviar" />
  </ItemTemplate> 
</asp:TemplateField>

And add in the GridView event onRowCommand="gvwPES_RowCommand"

Finishing method

protected void gvwPES_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Enviar")
    {
        var pesId = e.CommandArgument;

        // o que vc precisa fazer
    }

}
    
22.06.2015 / 00:12