Click here to Skip to main content
16,009,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to show /Hide an image button that is inside gridview Item Template
Posted

1 solution

Solution

  • Inside the RowDataBound Event, find the control.
  • Run your logic and set Visible property to true/false.

Example


C#
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if(someCondition)
        {
            ImageButton imgBtn= (ImageButton)e.Row.FindControl("imageButtonId");
            imgBtn.Visible = false;
        }
    }
}
 
Share this answer
 
v2
Comments
Member 10578683 21-Apr-14 2:35am    
i did like this but not coming. please check the code

protected void gdvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Footer)
{
}
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
string txt_ddlStatus = e.Row.FindControl("ddlStatus").ClientID;
string txt_ddlSubStatus = e.Row.FindControl("ddlSubStatus").ClientID;

ImageButton lnlUpdate = (ImageButton)e.Row.FindControl("lnkUpdate");
lnlUpdate.Attributes.Add("onclick", "javascript:return Validate('" + txt_ddlStatus + "','" + txt_ddlSubStatus + "')");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblId = (Label)e.Row.FindControl("lblID");
string id = lblId.Text;
Label lblProductName = (Label)e.Row.FindControl("lblProduct");
Label lblSerial = (Label)e.Row.FindControl("lblSerial");
Label lblStatus = (Label)e.Row.FindControl("lblStatus");
TextBox txtBox1 = (TextBox)PlaceHolder1.FindControl("tb1");
string subProductName = lblProductName.Text;
string subSerial = lblSerial.Text;
string subStatus = lblStatus.Text;

if (Session["Role"] != null && !string.IsNullOrEmpty(Session["Role"].ToString()))
{
con.Open();
using (SqlCommand comm = new SqlCommand("select [Edit] from Role1 where Role_Name=@Role and Pages=@Pages", con))
{
string Role = (string)(Session["Role"]);
// 2. define parameters used in command object
SqlParameter para = null;
para = new SqlParameter();
para.ParameterName = "@Role";
para.Value = Role;
comm.Parameters.Add(para);

//Pass @Pages parameter
para = new SqlParameter();
para.ParameterName = "@Pages";
para.Value = "Checkin";
comm.Parameters.Add(para);
SqlDataReader reade = comm.ExecuteReader();
while (reade.Read())
{
Session["Edit"] = Convert.ToString(reade["Edit"]);
}
//Button Add = (Button)PreviousPage.FindControl("Button2");
if (Session["Edit"].ToString() == "Y")
{

ImageButton img = (ImageButton)e.Row.FindControl("lnkEdit");
img.Visible = true;
}
else
{
ImageButton img = (ImageButton)e.Row.FindControl("lnkEdit");
img.Visible = false;

}
}

}
con.Close();
}
}


catch (Exception ex)
{
}
}
and the mark up is
<itemtemplate>

<asp:ImageButton CssClass="txtBtn" ID="lnkEdit" runat="server" CommandName="Edit"
ImageUrl="~/images/edit.gif" ToolTip="Edit" />  
<asp:ImageButton CssClass="txtBtn" ID="lnkDelete" runat="server" CommandName="Delete"
CommandArgument='' OnClientClick="javascript:return confirm('Are you sure you want to delete the record?')"
Did you debug and see if it satisfying the condition or not?
Member 10578683 21-Apr-14 2:52am    
that condition is not executing
You mean the below condition, right?

if (Session["Edit"].ToString() == "Y")

But why???

One more thing here. Always check for null before getting the value like...

if(Session["Edit"] != null && !string.IsNullOrEmpty(Session["Edit"].ToString()))
{
if (Session["Edit"].ToString() == "Y")
{
// Do whatever you want.
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900