This article will show you how you can find the itemtemplate
control value on gridview RowDataBound in asp.net using C#.net and VB.net.
So for this just add a gridview and generate the RowDataBound
event of gridview and add the below code. In this code I have shown how to find
the label control value on RowDataBound event of the gridview.
So here is the aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView
Find Control Value on RowDataBound In Asp.Net Using C#.Net and VB.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label9" runat="server" Text="" style="font-weight:bold;color:Red;"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
Width="100%" BorderStyle="Solid" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="PRICE">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#
Bind("retail_price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#66CCFF" />
</asp:GridView>
</form>
</body>
</html>
|
Now here is the code to find the label control value.
C#.Net
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
var
bookPrice = (Label)e.Row.FindControl("Label4");
if
(Convert.ToDecimal(bookPrice.Text) >
12)
{
e.Row.BackColor =
System.Drawing.Color.Red;
}
}
}
|
VB.Net
Protected Sub
GridView1_RowDataBound(sender As Object, e As
GridViewRowEventArgs)
If
e.Row.RowType = DataControlRowType.DataRow Then
Dim
bookPrice = DirectCast(e.Row.FindControl("Label4"), Label)
If
Convert.ToDecimal(bookPrice.Text) > 12 Then
e.Row.BackColor = System.Drawing.Color.Red
End If
End If
End Sub
|
Here is the image of demo to get control value
Nice post
ReplyDelete