Applying Click event for gridview row
00:53
Here I am explaining how to apply click event for entire row of gridview..
Take a grid view and bind some data to grid view.
<asp:GridView ID="grdlease" runat="server" Width="100%" CssClass="Gridview" CellPadding="6"
Take a grid view and bind some data to grid view.
.Aspx Page write the code like below.
<asp:GridView ID="grdlease" runat="server" Width="100%" CssClass="Gridview" CellPadding="6"
AutoGenerateColumns="false"
AllowPaging="true"
AllowSorting="true"
PageSize="10"
HeaderStyle-Height="20px"
HeaderStyle-CssClass="gridleaseheader"
OnPageIndexChanging="grdlease_PageIndexChanging"
RowStyle-Height="20px"
RowStyle-CssClass="gridtd"
GridLines="Horizontal"
RowStyle-BorderColor="#dddddd"
PagerStyle-BorderColor="#dddddd"
HeaderStyle-BorderColor="#454545"
OnSorting="grdlease_Sorting"
OnRowDataBound="grdlease_RowDataBound"
onrowcommand="grdlease_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<br />
<asp:LinkButton ID="Label1" runat="server"
Text="BUYER/CURRENT
VEHICLE" CommandName="Sort"
CommandArgument="Name"
ForeColor="White"
Style="text-decoration:
none;"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblname"
runat="server"
Text='<%#Eval ("Name") %>' Font-Bold="true"></asp:Label>
<br />
<asp:Label ID="lblmodel"
runat="server"
Text='<%#Eval("exmodel")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Middle"
Wrap="true"
/>
</asp:TemplateField>
<asp:TemplateField SortExpression="lstatus">
<HeaderTemplate>
<br />
<asp:LinkButton ID="lbstatus"
runat="server"
CommandName="Sort"
CommandArgument="lstatus"
Text="STATUS" ForeColor="White" Style="text-decoration: none;"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblstatus"
runat="server"
Text='<%#Eval("lstatus")%>' CssClass="grdstatus"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<br />
<asp:LinkButton ID="lbltotal"
runat="server"
CommandName="Sort"
CommandArgument="tot"
Text="TOTAL" ForeColor="White" Style="text-decoration: none;"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbltot" runat="server"
Text='<%#Eval ("tot")%>' CssClass="grdtot"></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Image ID="imgpayment"
runat="server"
ImageUrl="~/images/price.png"
Width="30px"
Height="30px"
/>
<br />
<asp:LinkButton ID="lblpayment" runat="server" Text="PAYMENT" CommandArgument="pmt"
CommandName="Sort"
ForeColor="White"
Style="text-decoration:
none;"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblpmt" runat="server"
Text='<%#Eval ("pmt") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"
VerticalAlign="Middle"
/>
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Middle"
Wrap="true"
/>
</asp:TemplateField>
</columns>
</asp:GridView>
.Cs file write the code like below
protected override void
Render(System.Web.UI.HtmlTextWriter writer)
{
foreach
(GridViewRow row in
grdlease.Rows)
{
if
(row.RowType == DataControlRowType.DataRow)
{
row.Attributes["onmouseover"] =
"this.style.cursor='hand';this.style.textDecoration='none';";
row.Attributes["onmouseout"] =
"this.style.textDecoration='none';";
row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(grdlease,
"Select$"
+ row.RowIndex, true);
}
}
base.Render(writer);
}
the aboue code is for if u mouse over the grid row then the mouse pointer to convert to hand symbol and creating the click event for the selected row and giving command name and command argument to that row.
in the aboue code command name is Select and command argument is row.rowindex
protected void grdlease_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if
(e.CommandName == "Select")
{
Label
lblstatus = grdlease.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lblstatus") as
Label;
string status = lblstatus .Text;
Label
lbltot = grdlease.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("lbltot") as
Label;
string total = lbltot .Text;
Response.redirect("home.aspx");
}
}
in the row command event what you want to perform with that click event write that code here
Thats it..enjoy Happy Coding..
0 comments :