简体   繁体   中英

make Row in gridview asp.net clickable?

I create gridview and remove the select button to make all row clickable but I want to redirect the user for selected item detail
Note: I remover the CommandField select Visible="False"

    int rowCount = 0;
protected void gv_TasksProjectForUser_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //var taskID = gv_TasksProjectForUser.SelectedDataKey.Value;
        e.Row.Attributes.Add("onclick", "location='TaskDetail.aspx?taskID=" + e.Row.RowIndex + "'");
        e.Row.Attributes.Add("onmouseover", "JavaScript:this.style.cursor='pointer';");

    }
    rowCount++;
}

I have made this with Item Templates.

What you have to do is remove the property AutogenerateColums and add them manually with objects and item templates, on those add one that could be a button.

Later on the code Behind add a event to handle the button click, on that event you can do the response.redirect to another page.

<asp:GridView ID="userGrid" runat="server" CssClass="AdminGrid" 
                        AllowPaging="True" AutoGenerateColumns="False" PageSize="11">
                        <Columns>                
                            <asp:BoundField DataField="ApplicationId" Visible="False" />
                            <asp:BoundField DataField="UserName" Visible="False" />                                                                   
                            <asp:TemplateField >
                                <HeaderTemplate>
                                    <asp:Label ID="lblEmail" Text="E-Mail" runat="server" CssClass = "HeaderLabel" meta:resourcekey="lblEmail"></asp:Label>
                                    <asp:ImageButton ID="imgSortEMail" runat="server" ImageUrl="~/Images/normal.gif" OnClick="SortGrid" CommandArgument="EMail" CssClass="SortButton" ToolTip="Click here to Order" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:HyperLink ID="lnkEMail" runat="server" CssClass="EmailLinkButton" Text='<%# FormatGridTextDisplay(DataBinder.Eval(Container.DataItem, "EMail")) %>' ToolTip='<%# DataBinder.Eval(Container.DataItem, "EMail") %>' NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "EMail","mailto:{0}") %>' ></asp:HyperLink>                                                                                
                                </ItemTemplate>
                                <HeaderStyle CssClass="OverFlowStringField" />
                                <ItemStyle CssClass="OverFlowLeftAligned" />
                            </asp:TemplateField>
                            <asp:TemplateField >
                                <HeaderTemplate>
                                    <asp:Label ID="lblSalonUser" Text="Salon User" runat="server" CssClass = "HeaderLabel" meta:resourcekey="lblSalonUser"></asp:Label>
                                    <asp:ImageButton ID="imgSortIsSalonUser" runat="server" ImageUrl="~/Images/normal.gif" OnClick="SortGrid" CommandArgument="IsSalonUser" CssClass="SortButton" ToolTip="Click here to Order" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkSalonUser" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "IsSalonUser") %>' onclick="javascript:if (this.checked==true) this.checked=false; else this.checked=true;"/>                      
                                </ItemTemplate>
                                <HeaderStyle CssClass="OverFlowStringField" />
                                <ItemStyle CssClass="CenterAligned" />
                            </asp:TemplateField>
                            <asp:TemplateField >                
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" runat="server" Text="Editar" CssClass="GridButton" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserName")%> '  OnClick="btnEdit_Click" meta:resourcekey="btnEdit"/>                        
                                </ItemTemplate>
                                <HeaderStyle CssClass="OverFlowStringField" />
                                <ItemStyle CssClass="CenterAligned" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

There You can see that i add a few types of items template, the most important is the btnEdit, on that one there are 2 important properies, one is the CommandArgument where you send the values to redirect to the page and the other is the event OnClick that is the one that will handle the click for the button.

Protected void btnEdit_Click(Object sender , System.EventArgs e ){
    Button clickedButton = (sender)Button;
    String() argumentsSend = clickedButton.CommandArgument.ToString().Split("|");
    String backParameters;


    Response.Redirect(String.Concat("RedirectPage.aspx?user=", Server.UrlEncode(argumentsSend(0)), "&company=", Server.UrlEncode(argumentsSend(1)), True);
}

I took this code from VB and change it to c# with a compiler, it may have errors, but that's the idea. A easyest way is to use a link button or an hipperlink on the template of the grid, that eay you may no need to go to the code file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM