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..
<html xmlns="">
<head runat="server">
    <title>How to change link button text in gridview at runtime in ASP.NET using C#/VB.NET.</title>
    <form id="form1" runat="server">
        <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="true" OnRowDataBound="gvEmployee_RowDataBound"
                <asp:TemplateField SortExpression="ID" HeaderText="Edit">
                    <HeaderStyle ForeColor="white"></HeaderStyle>
                        <asp:LinkButton ID="lnkEdit" runat="server"
CommandArgument='<%#(Convert.ToString( Eval("Emp_ID")))%>'
                                        Edit </asp:LinkButton>
                    <HeaderStyle ForeColor="White" HorizontalAlign="Left" />
                    <ItemStyle Width="5%" HorizontalAlign="Left" />

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)
    protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
            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();
        gvEmployee.DataSource = ds;

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)
    End Sub
    Protected Sub gvEmployee_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            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()
        gvEmployee.DataSource = ds
    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 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.