简体   繁体   中英

Selecting a Gridview row without a select column in ASP.NET

Im a newbie and totally new in ASP.NET. I need to select a row in a gridview by just clicking a row in it and not by clicking a select button from a select row column. Thanks :)

You can add this to your code to make the any cells of any row selectable.

//Select a row by clicking any cells of it
        protected void grdEmployeeList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdEmployeeList, "Select$" + e.Row.RowIndex);
            }
        }

and then add this page attribute to the first line of the source code of the page designer:

EnableEventValidation = "false"

FYI the grid specified in the above code refers to my gridview object.

Just fill in the GridView with your Rows and you're good to go.

Yes you can do that. You don't want to click select button so make your data in the grid a link button to show the data as well as click on it. Following is the code for the same

<asp:TemplateField HeaderText="CODE" ShowHeader="true" ItemStyle-CssClass="td" HeaderStyle-CssClass="grid_header"
                                ItemStyle-BorderWidth="1" ItemStyle-BorderColor="LightGray" HeaderStyle-Width="10%">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkCode" runat="server" Text='<% #Bind("Amm_code") %>' CommandName="Select"
                                        ForeColor="Blue" ToolTip="Click To Edit"></asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle CssClass="td" />
                                <HeaderStyle CssClass="td" />
                            </asp:TemplateField>

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