How to place an Onclick event by calling a Javascript Function on a Link inside a GRIDVIEW on Asp.net?

Asked

Viewed 3,267 times

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 return of the query, creates a gridview where each record contains a link that will return the data to a java script. My problem is that I cannot place 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>

1 answer

1


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

If you want to continue in onClientClick would be something like this:

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

Tidies up your Eval, position of as my example.


The best way would be using RowCommand, example:

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

And adds in the Gridview the event onRowCommand="gvwPES_RowCommand"

Finalizing the method

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

        // o que vc precisa fazer
    }

}
  • Avoid long discussions in the comments; this conversation was moved to the chat

  • @bfavaretto It is that he did not have enough scores to participate in the chat, so this is the only way I can try to help him. Anyway thanks for the "cleaning".

  • @Maiconcarraro, I did what you said, and I gave to make the conversions perfectly. But he is not passing the data from the child page to the parent page. This query form is a popup. Can I use the javascript method self.opener.Document? and if I set the parameters with the same name that is received in Function, it already takes automatic?

  • Would not be window.opener.document?

  • is the self.opener itself!

  • @Maiconcarraro I passed the function call as you commented in the chat as follows: Page.ClientScript.Registerclientscriptblock(this.Gettype(), "informa_pessoa", parameters, true); where returned string <script>informa_pessoa(4, 204058, FRANCISCA GERMANO DOS SANTOS SANTIAGO , 78288002315,'',''',''',''',''',''',''','', 0,', 002,'',''''', 24,''', 0,'', 0)"</script> where the following error returns: Syntaxerror: Missing ) after list argument argument.

  • you need to have the parameters between simple quotes '' if it will not give problem type in FRANCISCA GERMANO

  • @Maiconcarraro, that’s right. Just one more thing. When you open a popup, because all I do in it like open a grid, it opens in a new window?

  • I couldn’t quite understand your question, you don’t want to open another question with this?

  • And if you solved your problem in this question, please mark as the right answer :)

  • Blz... thanks for the help!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.