行点击时Asp.net Webforms GridView

时间:2018-04-18 09:54:17

标签: c# asp.net gridview webforms

我正在尝试做一件事,所以,当我点击GridView行时我想发送CommandArgument,现在,我正在使用链接按钮,我想摆脱它,我希望用户点击只有行然后它发送CommanArgument。

目前这是我的代码:

 <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>

这是C#:

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

这不是完整的代码,只是必要的,但是,我需要在行上点击而不是在链接按钮上发送命令参数。

1 个答案:

答案 0 :(得分:0)

您必须使用GridView的RowCreated事件,并且必须在每次回发时添加此事件。

例如

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);
    }
}