Shoot event by clicking on a cell in a GridView

0

I'm trying to open a new page by clicking on any cell of GridView does anyone know if this is possible?

    <table>
        <tr>
            <td>
                <asp:GridView ID="GV_Gastos" runat="server" OnRowDataBound="GV_Gastos_RowDataBound" CssClass="grid-view" AutoGenerateColumns="False" AllowSorting="True" Font-Names="century gothic,small" Font-Size="Small" HorizontalAlign="Center" Width="1000px" DataSourceID="DS_Gastos" ShowFooter="True">
                <HeaderStyle CssClass="HeaderRojo" />
                <RowStyle CssClass="normal" />
                <AlternatingRowStyle CssClass="alternate" />
                <PagerStyle ForeColor="#CC0000" Font-Bold="true" />
                <Columns>
                    <asp:BoundField DataField="GASTO" FooterText="Total" HeaderText="Tipo de Gasto" ReadOnly="True" SortExpression="GASTO" ItemStyle-Width="100px">
                        <FooterStyle Font-Bold="True" HorizontalAlign="Center" />
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle BackColor="White" Font-Bold="True"/>
                    </asp:BoundField>
                    <asp:BoundField DataField="ANTERIOR" HeaderText="ANTERIOR" SortExpression="ANTERIOR"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                    </asp:BoundField>
                    <asp:BoundField DataField="UNO" HeaderText="UNO" SortExpression="UNO"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle HorizontalAlign="Left" Width="15%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="DOS" HeaderText="DOS"  SortExpression="DOS"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" Width="10%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="TRES" HeaderText="TRES" SortExpression="TRES"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="CUATRO" HeaderText="CUATRO" SortExpression="CUATRO"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="CINCO" HeaderText="CINCO" SortExpression="CINCO"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SEIS" HeaderText="SEIS" SortExpression="SEIS"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="TOTAL" HeaderText="TOTAL" SortExpression="TOTAL"  DataFormatString="{0:c}" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="RESPONSABLE" HeaderText="RESPONSABLE" SortExpression="RESPONSABLE" ItemStyle-Width="100px">
                        <HeaderStyle Font-Names="Century Gothic" Font-Size="Small" ForeColor="White" HorizontalAlign="Center"/>
                        <ItemStyle Font-Names="Century Gothic" Font-Size="Small" HorizontalAlign="Left" />
                    </asp:BoundField>
                </Columns>
                        <RowStyle BorderColor="White" BorderStyle="Solid" BorderWidth="2px" Font-Bold="False" />
                        <FooterStyle BackColor="Red" Font-Bold="True" Font-Italic="False" ForeColor="White" />
                        <HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
                <asp:SqlDataSource ID="DS_Gastos" runat="server" ConnectionString="ejemplo" SelectCommand="ejemplo" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="102" Name="ACCION" Type="Int32" />
                        <asp:ControlParameter ControlID="ejemplo" DefaultValue="ejemplo" Name="ejemplo" PropertyName="Value" Type="String" />
                        <asp:ControlParameter ControlID="ejemplo" DefaultValue="ejemplo" Name="ejemplo" PropertyName="Value" Type="String" />
                        <asp:ControlParameter ControlID="ejemplo" DefaultValue="" Name="IDRESPONSABLE" PropertyName="Value" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </table>
    
asked by ARR 27.02.2018 в 16:24
source

1 answer

2

Here you have two options, or we do postback because you need to perform actions on the server side or directly link to the gridview click ... for that I leave you with the two options

  • OPTION 1: How to select the row / row of the gridview directly with one click in row / row
  • OPTION 2: How to make a link in the row of a gridview (by clicking)

We go in parts ... but in your case as a hyperlink says, then OPTION 2 is what you need.

OPTION 1: How to select the row / row of the gridview directly with    one click in row / row You can use the GetPostBackClientHyperlink method to justify what it says in the event the name of the method, get the link to do postback ... you must register it in each row in the event onclick of javascript doing when data is linked to each row or in the event RowDataBound

The idea

protected void GV_Gastos_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow){
    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GV_Gastos, "Select$" + e.Row.RowIndex);
    e.Row.ToolTip = "Haga click para seleccionar la fila.";

}
}

Here you have that for each click (you can also do it with doubleclick in the event of the ondclclick delciente side) the selection event of the gridview (with a special "Select" command and the argument you need is the number of the row) In the event SelectedIndexChanged that you should perform the actions you want with the selected row

void GV_Gastos_SelectedIndexChanged(Object sender, EventArgs e) {
   GridViewRow row = GV_Gastos.SelectedRow;
   //acciones...
}

NOTE: If you get an error you should set the variable on the page (or in the webconfig if you want for the whole app)

  

<% @ Page Title="" Language="C #" MasterPageFile="~ / Site.Master" AutoEventWireup="true" CodeBehind="GridviewBasic.aspx.cs"        EnableEventValidation="false" ...

OPTION 2: How to make a link in the gridview row (click) Here the option 2 is identical to 1 only there is no postback, which is just what you need .. is to add to the onclick the opening of the link you want (if you want on the same page can also be depends on the javascript you place) Something like that

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes["onclick"] = "javascript:window.open('https://es.stackoverflow.com', 'Demo', 'width=800,height=800')";
            e.Row.ToolTip = "Haga click para seleccionar la fila.";
        }
    }

Links that can help you

Occurs when a row of data is linked to data in a GridView control.

    
answered by 27.02.2018 / 17:40
source