How to hide ImageButton based on Row Cell value - asp.net

I am trying to hide my imagebutton based on the cell value of another column.
So if my cell value.Text = "OPEN" then I want that specific imagebutton for that row to be invisible.
However my code hides all of the imagebuttons and I just wanna hide the ones that contain the cell text "OPEN"
Here is the code I have:
<asp:GridView ID="gvv" OnRowDataBound="gv1_RowDataBound" onrowcommand="gridupdate_RowCommand" OnPreRender="GridView1_PreRender" class="table table-striped table-bordered table-hover" runat="server">
<Columns>
<asp:TemplateField HeaderStyle-Width ="115px" HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ImageButton3" runat="server" CommandName="Submit" ImageUrl="~/img/Sumbit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CommandName="ASN" ImageUrl="~/img/ASN-send.png" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/invoice.png" CommandName="View" />
</ItemTemplate>
<HeaderStyle Width="115px"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
Backend Code:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.Cells(2).Text.ToString = "OPEN") Then
Else
Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
imgBtn.Visible = False
End If
End If
End Sub

I think your code works correctly but you just need to revise your If statement, it should be:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.Cells(2).Text.ToString = "OPEN") Then
'Hide ImageButton3
Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
imgBtn.Visible = False
Else
'Do nothing
End If
End If
End Sub
Tried it on my side and it's working, unless you are doing something else in GridView1_PreRender method that maybe affect on this.

You can use the following scenario if you are using telerik rad grid. hope that this may help you to find a solution.
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
Dim imgBtn As ImageButton = DirectCast(e.Item.FindControl("ImageButton3"), ImageButton)
If (e.Item.Cells(2).Text.ToString = "OPEN") Then
imgBtn.Visible = True
Else
imgBtn.Visible = False
End If
End If
End Sub

Related

display image after row click of gridview

I want to have an image displayed from a row click of gridview. not part of gridview, so should be outside of gridview when have image. It will show different images based to a value from a column in the gridview. I have tried a couple of ways, such as using gridview on row data bound plus on selected index changed. But I just could not even have the image displayed upon row click.
Ok, so we assume that the GridView has a column with the path name to the image?
The markup can say be this:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="float:left">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="ImageName" HeaderText="Image Name" />
<asp:BoundField DataField="Image" HeaderText="Image Name" />
</Columns>
</asp:GridView>
</div>
<div style="float:left;margin-left:40px">
<asp:Image ID="Image1" runat="server" Height="302px" Width="448px" />
</div>
<br />
</div>
</form>
data table:
Results:
I clicked on dog in the grid, and above was the result.
Edit: it would seem that the user wants a row click and NOT necessary a button on the page.
Ok, button has been removed.
Code is now this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using cmdSQL As New SqlCommand("SELECT ID, ImageName, Image from tblImage",
New SqlConnection(My.Settings.TEST4))
cmdSQL.Connection.Open()
GridView1.DataSource = cmdSQL.ExecuteReader
GridView1.DataBind()
End Using
End Sub
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
e.Row.ToolTip = "Click to select this row."
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Image1.ImageUrl = GridView1.SelectedRow.Cells(2).Text
End Sub
so we now add a click row event to the WHOLE row.
Note that you MUST add this to the first line in the page directive:
EnableEventValidation="false"
The above goes in the VERY first line of the markup.
We now add two more events.
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
e.Row.ToolTip = "Click to select this row."
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
Image1.ImageUrl = GridView1.SelectedRow.Cells(2).Text
End Sub
So now the button can be REMOVED from the row - you can click anywhere on the row. And I suppose we could hide the 3rd column - the "url of the image"

Change the visibility property from code behind

I'm trying to hide the 'Delete' link if value in CUST_ORDER_ID = 'X' but I don't know how to set the visibility property to "False"
My asp.
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ROWID" SortExpression="ROWID" Visible="False"> </asp:BoundField>
<asp:BoundField DataField="CUST_ORDER_ID" HeaderText="ORDER ID" SortExpression="CUST_ORDER_ID">
<ItemStyle Width="50px"></ItemStyle>
and the code behind
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'check is row non order type and allow user to delete
Dim oid As TableCell = e.Row.Cells(2)
If oid.Text = "X" Then
Dim tb As Button = e.Row.Cells(1).Controls(1)
'Dim tb = e.Row.FindControl("DeleteButton")
tb.Visible = "False"
End If
End If
End Sub
Thanks for all of the ideas. This is the cleanest solution I found on this site but it was in c# so converted to vb.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="DeleteButton" CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
and the code behind
Dim oid As TableCell = e.Row.Cells(2)
Dim tb = e.Row.FindControl("DeleteButton")
If oid.Text = "X" Then
tb.Visible = True
Else
tb.Visible = False
End If
Maybe you can change to Templatefield
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:label ID="lbl1" runat="server" text='<%#left(DataBinder.Eval(Container.DataItem, "field1"),20)%>'>
</asp:label>
</ItemTemplate>
With this code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim lbl1 As Label
If e.Row.RowType = DataControlRowType.DataRow Then
lbl1 = CType(e.Row.FindControl("lbl1"), Label)
If oid.Text = "X" Then
lbl1 .Visible = "False"
End If
End If
End Sub
You can use almost any control in template.

Get ChekBox Component in Gridview Inside Content Template

I have this aspx snippet:
<asp:updatepanel runat="server" id="resultPanel">
<contenttemplate>
<app:exgridview id="referenceGridView" runat="server" allowpaging="True" allowsorting="True">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAllBox" runat="server" CausesValidation="false"></asp:CheckBox>
</HeaderTemplate>
<asp:TemplateField>
.....
How do I get checkAllBox component in .vb code behind?
I have tried this:
referenceGridView.HeaderRow _
.Cells(0).FindControl("checkAllBox")
And
referenceGridView.FindControl("checkAllBox")
but it doesn't work neither.
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
EDITED
You can find the control in GridView.HeaderRow:
NOTE : This code block should be executed after referenceGridView.DataBind(). I am running this in a button click:
Protected Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim checkAllBox As CheckBox = TryCast(referenceGridView.HeaderRow.FindControl("checkAllBox"), CheckBox)
If checkAllBox IsNot Nothing Then
'checkAllBox exists here.
'Place your code for checkAllBox
If checkAllBox.Checked Then
lblResult.Text = "All checked"
Else
lblResult.Text = "All not checked"
End If
End If
End Sub
Presuming that you already have this in page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'clear xml on page load if it contain data
If Not Page.IsPostBack Then
'Getting datasource
'referenceGridView.DataSource = MyDataSource.GetTable()
referenceGridView.DataBind()
End If
End Sub
And my test button in markup:
<asp:button id="btnTest" runat="server" Text="TEST" />
And you can download the test project here.

can't find a control inside my gridview

I have a simple gridview that contains a label in one of the rows. I'm trying to access that label in the RowDataBound event, but for some reason I keep getting a "Object reference not set to an instance of an object." error on the line where I am using FindControl.
I've tried using "gvQReport.FindControl", "e.Row.FindControl", and "Me.FindControl" but nothing works.
Am I not doing this correctly?
Thanks!
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim lblTest As Label = CType(gvQReport.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End Sub
<asp:GridView ID="gvQReport" OnRowDataBound="gvQReport_RowDataBound" runat="server">
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Row property of GridViewRowEventArgs is the current row, look for your control there instead of the whole GridView.
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblTest As Label = CType(e.Row.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End If
End Sub

change gridview row color based on templatefields without controls

My gridview does not use controls, because it is populated using expressions
<asp:TemplateField HeaderText="As Of Sales">
<ItemTemplate>
<%#Getsales(Decimal.Parse(Eval("asofsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
I want to compare column index 1 and column index 8, and if 8 is bigger then 1 it should be a different font color.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim x As String
x = e.Row.Cells(1).Text
Dim y As String
y = e.Row.Cells(8).Text
If Convert.ToInt32(x) <= Convert.ToInt32(y) Then
e.Row.ForeColor = System.Drawing.Color.Blue
End If
End If
End Sub
Here is something that you can try
The gridview
<asp:GridView runat="server" ID="grdv" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="T1">
<ItemTemplate>
<%# Eval("T1")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="T2">
<ItemTemplate>
<%# Eval("T2")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim d As New DataTable
d.Columns.Add("T1")
d.Columns.Add("T2")
d.Rows.Add(1, 2)
grdv.DataSource = d
grdv.DataBind()
End Sub
Private Sub grdv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdv.RowDataBound
Dim data As DataRowView = e.Row.DataItem
If data Is Nothing Then Exit Sub
If e.Row.RowType = DataControlRowType.DataRow Then
If data.Item("T1") <= data.Item("T2") Then e.Row.ForeColor = Color.Red
End If
End Sub
This should work for binding to a DataTable. If you're using a collection then the RowDataBound event will need changing slightly.

Resources