0

I am trying to do a thing, so, when I click on the GridView Row I want to send the CommandArgument, for now, I am doing with linkbuttons and I want to get rid of that, I want the user to click only the row and then it sends the CommanArgument.

At the moment this is my code:

 <asp:GridView ID="jj" runat="server" AllowPaging="true" AllowSorting="false"
                AutoGenerateColumns="false" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr"
                AlternatingRowStyle-CssClass="alt" OnPageIndexChanging="gridView_PageIndexChanging" PageSize="10" EmptyDataText="Não existem dados para mostrar">

                <Columns>    
                    <asp:TemplateField HeaderText="Pré Ordens">
                                <ItemTemplate>
                                    <asp:LinkButton ID="PreOrdens" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TIPO_Registo") %>' runat="server"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                     <asp:TemplateField HeaderText="Descrição do Erro">
                                <ItemTemplate>
                                    <asp:LinkButton ID="DescricaoErro" OnClientClick="ddd();"  onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("Erro") %>' runat="server"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                    <asp:TemplateField HeaderText="NUV">
                                <ItemTemplate>
                                    <asp:LinkButton ID="NUV" OnClientClick="ddd();"  onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("NUV") %>' runat="server"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                    <asp:TemplateField HeaderText="TV">
                                <ItemTemplate>
                                    <asp:LinkButton ID="ErroID" OnClientClick="ddd();" onClick="SEND_ID_REGISTO" CommandArgument = '<%# Eval("ID_REGISTO_BASE") %>' Text='<%# Eval("TV") %>' runat="server"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                </Columns>
            </asp:GridView>

This is the C#:

protected void SEND_ID_REGISTO(object sender, EventArgs e)
        {
    id = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}

This is not the full code, is just the necessary, but yea, I need to send the command argument on the row click not on the linkbutton click.

  • Possible duplicate of [Making an entire row clickable in a gridview](https://stackoverflow.com/questions/686240/making-an-entire-row-clickable-in-a-gridview) – VDWWD Apr 18 '18 at 10:21

1 Answers1

0

you must use the RowCreated-Event of the GridView and You must add this on every postback . .

For example

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "select row";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}
Morteza Jangjoo
  • 1,780
  • 2
  • 13
  • 25