Wednesday 27 November 2013

How to change link button text in GridView at Runtime in ASP.NET using C#/VB.NET.

Hi Friends, in this article I will explain about How to change link button text in GridView at Runtime in ASP.NET using C#/VB.NET..
ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to change link button text in gridview at runtime in ASP.NET using C#/VB.NET.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="true" OnRowDataBound="gvEmployee_RowDataBound"
            Width="500px">
            <Columns>
                <asp:TemplateField SortExpression="ID" HeaderText="Edit">
                    <HeaderStyle ForeColor="white"></HeaderStyle>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkEdit" runat="server"
CommandArgument='<%#(Convert.ToString( Eval("Emp_ID")))%>'
                            CommandName="EDIT">
                                        Edit </asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="White" HorizontalAlign="Left" />
                    <ItemStyle Width="5%" HorizontalAlign="Left" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


C#.NET:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class ChangeGVLinkButtonText : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        BindCompanies();
    }
    protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                String statusId = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DepartmentName"));
                if (statusId == "HR" || statusId == "Admin")
                {
                    LinkButton lnkID = (LinkButton)e.Row.FindControl("lnkEdit");
                    e.Row.BackColor = System.Drawing.Color.LightPink;
                    lnkID.Text = "View";
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void BindCompanies()
    {
        SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE ORDER BY NAME", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvEmployee.DataSource = ds;
        gvEmployee.DataBind();
    }
}

VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Public Class ChangeGVLinkButtonText
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        BindCompanies()
    End Sub
    Protected Sub gvEmployee_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim statusId As [String] = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "DepartmentName"))
                If statusId = "HR" OrElse statusId = "Admin" Then
                    Dim lnkID As LinkButton = DirectCast(e.Row.FindControl("lnkEdit"), LinkButton)
                    e.Row.BackColor = System.Drawing.Color.LightPink
                    lnkID.Text = "View"
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Protected Sub BindCompanies()
        Dim cmd As New SqlCommand("SELECT * FROM EMPLOYEE ORDER BY NAME", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        gvEmployee.DataSource = ds
        gvEmployee.DataBind()
    End Sub
End Class

The output of the above code as shown in the below figure.




"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.