带有HyperLinkField的GridView会阻止选择

时间:2014-04-15 17:58:11

标签: c# asp.net gridview

我有以下带有信息列的GridView和行末的详细信息超链接:

<cml:DataGrid ID="gvDados" DataKeyNames="Codigo_da_Agencia, Codigo_Do_Agente" runat="server" AutoGenerateColumns="False" AllowPaging="false"
                SkinID="datagridSkin" TotalRecords="0" OnSelectedIndexChanged="gvDados_SelectedIndexChanged"
                OnPageIndexChanging="gvDados_PageIndexChanging" HeaderStyle-Wrap="false">
                <Columns>
                    <asp:BoundField DataField="Codigo_Do_Agente" HeaderText="Codigo_Do_Agente" SortExpression="Codigo_Do_Agente" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Da_Agencia" HeaderText="Codigo_Da_Agencia" SortExpression="Codigo_Da_Agencia" ItemStyle-Wrap="false" />
                    <asp:BoundField DataField="Nome_Do_Contato" HeaderText="Nome_Do_Contato" SortExpression="Nome_Do_Contato" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Nome_Da_Agencia" HeaderText="Nome_Da_Agencia" SortExpression="Nome_Da_Agencia" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Telefone" HeaderText="Telefone" SortExpression="Telefone" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Numero" HeaderText="Numero" SortExpression="Numero" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Do_Logradouro" HeaderText="Codigo_Do_Logradouro" SortExpression="Codigo_Do_Logradouro" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Apartamento" HeaderText="Apartamento" SortExpression="Apartamento" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Complemento" HeaderText="Complemento" SortExpression="Complemento" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Do_Bairro" HeaderText="Codigo_Do_Bairro" SortExpression="Codigo_Do_Bairro" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="DTHR" HeaderText="DTHR" SortExpression="DTHR" ItemStyle-Wrap="false"/>
                    <asp:HyperLinkField DataTextField="Codigo_Da_Agencia" DataTextFormatString="Detalhe" DataNavigateUrlFields="Codigo_Da_Agencia, Codigo_Do_Agente" 
                        HeaderText="Detalhes" DataNavigateUrlFormatString="../../Controls/Detalhes/Agencia.aspx?codigoAgencia={0}&codigoAgente={1}" Target="_blank"  />
                </Columns>
            </cml:DataGrid>

一切正常,点击超链接后会打开一个新标签。但我的问题是该行也被选中。

我想知道在单击超链接时是否有办法阻止选择行。或者,如果有一种方法可以验证代码隐藏中的方法PageIndexChanging,那是最后一个单击的列。

提前致谢!

2 个答案:

答案 0 :(得分:0)

当用户单击SelectionIndex_Changing事件中的特定列时,可以取消所选索引,如SelectedRow的msdn示例中所示。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.100).aspx

答案 1 :(得分:0)

找到解决方案,我必须说经过许多绝望的尝试后非常混乱......

我有一个扩展原始GridView的自定义类,然后我重写Render()就像这样:

protected override void Render(HtmlTextWriter writer)
    {
        foreach (GridViewRow row in this.Rows)
            if (row.RowType == DataControlRowType.DataRow)
                foreach (DataControlFieldCell cell in row.Cells)
                    if ((cell.ContainingField).HeaderText != "Detalhes")
                        cell.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this, string.Format("Select${0}", row.RowIndex), true);

        base.Render(writer);
    }

foreach单元格我将验证标题名称是否为详细名称。如果不是,我将添加一个onclick属性

老实说,我不认为它漂亮,但至少它正在发挥作用:P