Create Rounded LinkButton inside ASP.Net GridView using CSS

ramco1917
 
on Jul 14, 2022 01:35 AM
Sample_133140.zip
552 Views

Hi 

I have used below code & it is working. I want LinkButton control should appear in Circle & its color should gets changed.

protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
        e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
        for (int i = 2; i < e.Row.Cells.Count; i++)
        {
            var row = (DataRowView)e.Row.DataItem;
            var currentPerson = row[0].ToString();
            var currentDate = row.DataView.Table.Columns[i];
            var commandArgument = currentPerson + ":" + currentDate;
            LinkButton button = new LinkButton();
            button.Text = row[i].ToString();
            button.CommandName = "Select";
            button.CommandArgument = commandArgument;
            button.OnClientClick = "GetModuleData(this)";
            if (row[i].ToString() == "A")
                button.ForeColor = Color.Red;
            if (row[i].ToString() == "S")
                button.ForeColor = Color.Green;
            //{ Text = row[i].ToString(), CommandName = "Select", CommandArgument = commandArgument, CssClass = "badge badge-warning badge-pill", OnClientClick = "GetModuleData(this)" };
            e.Row.Cells[i].Controls.Add(button);

            e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
        }
    }
    catch (Exception ex)
    {
        Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfLoginCode.Value));
        ShowMessage("Oops...", ex.Message, "error");
    }
}

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 15, 2022 12:01 AM
on Jul 15, 2022 12:05 AM

Hi ramco1917,

You need to set LinkButton CSS style border-radius to 100% and padding to 0.2em 0.4em.

Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.

HTML

<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="CurrentPerson" HeaderText="CurrentPerson" />
        <asp:BoundField DataField="CurrentDate" HeaderText="CurrentDate" />
    </Columns>
</asp:GridView>

<style type="text/css">
    .red_button {
        border-radius: 100%;
        padding: 0.2em 0.4em;
        background-color: red;
        color: white;
    }

    .green_button {
        border-radius: 100%;
        padding: 0.2em 0.4em;
        background-color: green;
        color: white;
    }
</style>

Namespace

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    this.BindGrid();
}
 
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            var row = (DataRowView)e.Row.DataItem;
            var currentPerson = row[0].ToString();
            var currentDate = row.DataView.Table.Columns[i].ColumnName;
            var commandArgument = currentPerson + ":" + currentDate;
            LinkButton button = new LinkButton();
            button.Text = row[i].ToString();
            button.CommandName = "Select";
            button.CommandArgument = commandArgument;
            button.OnClientClick = "GetModuleData(this)";
            if (row[i].ToString().ToUpper() == "A")
            {
                button.CssClass = "red_button";
            }
            if (row[i].ToString().ToUpper() == "S")
            {
                button.CssClass = "green_button";
            }
            e.Row.Cells[i].Controls.Add(button);
            e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
        }
    }
}
 
private void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] {
                        new DataColumn("CurrentPerson"),
                        new DataColumn("CurrentDate") });
    dt.Rows.Add("A", "08/07/2022");
    dt.Rows.Add("S", "10/07/2022");
    gvDetails.DataSource = dt;
    gvDetails.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Me.BindGrid()
End Sub
 
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
 
        For i As Integer = 0 To e.Row.Cells.Count - 1
            Dim row = CType(e.Row.DataItem, DataRowView)
            Dim currentPerson = row(0).ToString()
            Dim currentDate = row.DataView.Table.Columns(i).ColumnName
            Dim commandArgument = currentPerson & ":" & currentDate
            Dim button As LinkButton = New LinkButton()
            button.Text = row(i).ToString()
            button.CommandName = "Select"
            button.CommandArgument = commandArgument
            button.OnClientClick = "GetModuleData(this)"
 
            If row(i).ToString().ToUpper() = "A" Then
                button.CssClass = "red_button"
            End If
 
            If row(i).ToString().ToUpper() = "S" Then
                button.CssClass = "green_button"
            End If
 
            e.Row.Cells(i).Controls.Add(button)
            e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Center
        Next
    End If
End Sub
 
Private Sub BindGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {New DataColumn("CurrentPerson"), New DataColumn("CurrentDate")})
    dt.Rows.Add("A", "08/07/2022")
    dt.Rows.Add("S", "10/07/2022")
    gvDetails.DataSource = dt
    gvDetails.DataBind()
End Sub

Screenshot