GridView单击行值

时间:2012-02-08 17:40:34

标签: c# javascript jquery gridview

在我的gridview中,我将此代码用于获取gridview行中的单击。

$('#grdImoveis tr').click(function () {
        openPopup('../Financeiro/DetalheImovelProposta.aspx', 'Detalhes', 'toolbar=yes,directories=no,status=yes,menubar=yes, scrollbars=yes,resizable=yes', '850', '600', 'true');
    });

在我的gridview中,我有5列。如何获取单击行中第一列的值并通过查询字符串传递?

我想在像这样的解决方案中

$('#grdImoveis tr').click(function () {
        // Get the value of a specific column of my clicked row.
        //var imovel = $('# ???)
        openPopup('../Financeiro/DetalheImovelProposta.aspx?Imovel_id=' + imovel, 'Detalhes', 'toolbar=yes,directories=no,status=yes,menubar=yes, scrollbars=yes,resizable=yes', '850', '600', 'true');
    });

标记:

<asp:GridView ID="grdImoveis" CssClass="StyleGrid" Width="100%" runat="server" AutoGenerateColumns="false" DataSourceID="dsGrid" BorderWidth="0" GridLines="None" OnRowDataBound="grdImoveis_DataBound">
        <AlternatingRowStyle BackColor="White" CssClass="EstiloDalinhaAlternativaGrid"  HorizontalAlign="Center"/>
        <RowStyle CssClass="EstiloDalinhaGrid" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#e2dcd2" ForeColor="#000" CssClass="thGrid" Height="20" />
        <Columns>
            <asp:BoundField HeaderText="Código" DataField="Imovel_Id" />
            <asp:BoundField HeaderText="Tipo" DataField="TipoDsc1" />
            <asp:BoundField HeaderText="(L/V)" DataField="TransacaoSigla" />
            <asp:TemplateField HeaderText="Endereco">
                <ItemTemplate>
                    <%# Eval("Descricao") %> <%# Eval("Logradouro") %>, <%# Eval("Numero") %> - <%# Eval("Expr1") %> <%# Eval("Complemento") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Propostas Ativas" DataField="NumeroProposta" />
        </Columns>
    </asp:GridView>
</div>
    <asp:SqlDataSource ID="dsGrid" runat="server" OnSelected="ds_Selected"></asp:SqlDataSource>

HTML呈现:

<table class="StyleGrid" cellspacing="0" border="0" id="grdImoveis" style="border-width:0px;width:100%;border-collapse:collapse;">
            <tr class="thGrid" style="color:#000000;background-color:#E2DCD2;height:20px;">
                <th scope="col">C&#243;digo</th><th scope="col">Tipo</th><th scope="col">(L/V)</th><th scope="col">Endereco</th><th scope="col">Propostas Ativas</th>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>373034</td><td>Apartamento 3 quartos</td><td>V</td><td>
                    Rua COUROPITA, 998541 -  
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>372533</td><td>Apartamento 3 quartos</td><td>V</td><td>
                    Rua GIOVANI JOSÉ CHIODI, 362 - Apto 23
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>372387</td><td>Apartamento 6 quartos</td><td>V</td><td>
                    Rua GIOVANI JOSÉ CHIODI, 1625 - Fazenda 525
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>372383</td><td>Cobertura</td><td>V</td><td>
                    Avenida NOSSA SENHORA DO CARMO, 1623 - Loteamento 12
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>372310</td><td>Apartamento 4 quartos</td><td>V</td><td>
                    Rua MOARA, 362 - Sala 52
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>371853</td><td>Apartamento 3 quartos</td><td>V</td><td>
                    Rua GIOVANI JOSÉ CHIODI, 523 - Apto 362
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>370983</td><td>Apartamento 3 quartos</td><td>V</td><td>
                    Rua GIOVANI JOSÉ CHIODI, 163 - Condomínio 526
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>370400</td><td>Apartamento 3 quartos</td><td>V</td><td>
                    Rua teset, 35555 -  0
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>370181</td><td>Haras</td><td>V</td><td>
                    Rua A, 5266612 - Barracão 2
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>369833</td><td>Apartamento 4 quartos</td><td>V</td><td>
                    Rua teset, 2288 -  
                </td><td>4</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>369688</td><td>Apartamento 6 quartos</td><td>V</td><td>
                    Avenida NOSSA SENHORA DO CARMO, 1665 - Vaga 2
                </td><td>2</td>
            </tr><tr class="EstiloDalinhaAlternativaGrid" align="center" style="background-color:White;">
                <td>352992</td><td>Apartamento 3 quartos</td><td>L</td><td>
                    Travessa B-5, 787878 -  
                </td><td>1</td>
            </tr><tr class="EstiloDalinhaGrid" align="center">
                <td>343962</td><td>Andar/pilotis</td><td>V</td><td>
                    Avenida NOSSA SENHORA DO CARMO, 1650 -  testeeee2
                </td><td>1</td>
            </tr>
        </table>

1 个答案:

答案 0 :(得分:1)

跳过javascript的东西,改为服务器端。在RowDataBound

GridView事件中添加以下代码
protected void grdImovei_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       var url = "../Financeiro/DetalheImovelProposta.aspx?Imovel_Id=" +
                 ((SomeType)r.Row.DataItem).Imovel_id.ToString();

       e.Row.Attributes.Add("onclick", "openPopup('" + url + "'," +
           @"'Detalhes', 
           'toolbar=yes,directories=no,status=yes,menubar=yes,
           scrollbars=yes,resizable=yes', '850', '600', 'true');");
    }
}

其中SomeType是数据源中项目的类型。

修改
对于SqlDataSource,请尝试以下操作:

var url = "../Financeiro/DetalheImovelProposta.aspx?Imovel_Id=" +
                     ((DataRowView)r.Row.DataItem)["Imovel_id"].ToString();