connectseekers.com

Friday, 1 August 2014

GridView Find Control Value on RowDataBound In Asp.Net Using C#.Net and VB.Net

8/01/2014 - By Pranav Singh 0

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


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top