Hide ASP.Net LinkButton inside GridView on page load using C# and VB.Net

indradeo
 
on Aug 02, 2022 06:03 AM
562 Views

how to make gridview row button visible false in page load using asp c#

namespace vstr_gt_ps_systm
{
    public partial class cisf : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindGrid();
            }
            if (Session["emp_nm"] == null)
                Response.Redirect("emp.aspx");
            else
            {
                String EmplooyeId = Session["emp_id"].ToString();
                Label1.Text = " " + EmplooyeId + "";
                String emp_nm = Session["emp_nm"].ToString();
                Label2.Text = "" + emp_nm + " ";
            }
            Label1.Visible = false;
        }
        private void BindGrid()
        {
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT Id, emp_id,dept, emp_nm,vst_nm,vst_adhr,fa_hu_nm,gndr,crnt_add,pur_pos,emp_in_flag,cisf_in_flag,mrk_in_emp,mrk_in_cisf,cisf_out_flag,mrk_out_emp,mark_out_cisf FROM vstr WHERE emp_id LIKE '%' + @emp_id + '%' and dept LIKE '%' + @dept + '%'and cisf_out_flag='N'";
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@emp_id", txtSearch.Text.Trim());
                    cmd.Parameters.AddWithValue("@dept", txtSearch0.Text.Trim());
                    DataTable dt = new DataTable();
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
    }
}

 

<asp:GridView ID="GridView1"
    HeaderStyle-BackColor="#99ccff"
    HeaderStyle-ForeColor="black"
    DataKeyNames="Id"
    runat="server" Font-Size="Medium" AutoGenerateColumns="False"
    HeaderStyle-Font-Size="17px"
    OnRowCommand="GridView1_RowCommand"
    EmptyDataText="There Is No Records In Database!"
    Height="122px" Width="1226px"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnRowDataBound="GridView1_RowDataBound"
    CssClass="auto-style7" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Visitor Id" Visible="true" />
        <asp:BoundField DataField="emp_id" HeaderText="Emp.Id" ItemStyle-Width="100px">
            <ItemStyle Width="100px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="emp_nm" HeaderText="Emp. Name" ItemStyle-Width="100px">
            <ItemStyle Width="100px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="dept" HeaderText="Dept" ItemStyle-Width="100px">
            <ItemStyle Width="100px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="vst_nm" HeaderText="Visitor Name" />
        <asp:BoundField DataField="vst_adhr" HeaderText="Visitor Aadhar No." />
        <asp:BoundField DataField="fa_hu_nm" HeaderText="Father/Husband Name" DataFormatString="{0:dd-MM-yyy}" />
        <asp:BoundField DataField="gndr" HeaderText="Gender" DataFormatString="{0:dd-MM-yyy}" />
        <asp:BoundField DataField="crnt_add" HeaderText="Current_Address" />
        <asp:BoundField DataField="cisf_in_flag" HeaderText="Mark_In_Cisf_" />
        <asp:BoundField DataField="mark_out_cisf" HeaderText="Mark_Out_Cisf" />
        <asp:TemplateField HeaderText="View">
            <ItemTemplate>
                <asp:Button ID="LinkButton1" Text="View" runat="server" OnClick="LinkButton1_Click" CommandName="EditButton" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Out">
            <ItemTemplate>
                <asp:Button ID="Button2" Text="Mark Out" UseSubmitBehavior="false" runat="server" CommandName="Closed" OnClick="Closed" CommandArgument="<%# Container.DataItemIndex %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Size="17px" ForeColor="White" Font-Bold="True"></HeaderStyle>
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
KasimA
 
on Aug 02, 2022 08:52 AM
on Aug 03, 2022 08:45 AM

Hi indradeo,

Please refer below sample.

Database

I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.

Download SQL file

HTML

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit"></asp:LinkButton>
                <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    {
        this.BindGridView();
        foreach (GridViewRow row in gvCustomers.Rows)
        {
            (row.FindControl("lnkEdit") as LinkButton).Visible = false;
        }
    }        
}

private void BindGridView()
{
    string ConString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(ConString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
        {
            con.Open();;
            gvCustomers.DataSource = cmd.ExecuteReader();
            gvCustomers.DataBind();
            con.Close();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGridView()

        For Each row As GridViewRow In gvCustomers.Rows
            TryCast(row.FindControl("lnkEdit"), LinkButton).Visible = False
        Next
    End If
End Sub

Private Sub BindGridView()
    Dim ConString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(ConString)
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
            con.Open()
            gvCustomers.DataSource = cmd.ExecuteReader()
            gvCustomers.DataBind()
            con.Close()
        End Using
    End Using
End Sub

Screenshot