Change the visibility property from code behind - asp.net

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.

Related

Row command not working in gridview

Hi I am using following code to display a grid on aspx
<asp:GridView Caption="Search Results" ID="PhysicianGrid"
runat="server" EnableSortingAndPagingCallbacks="false" PageSize="5" Style="width: 100%"
PagerSettings-Visible="false" AutoGenerateColumns="false" CssClass="grid" RowStyle-CssClass="gridDataRow"
HeaderStyle-CssClass="headerRow" EnableViewState ="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<HeaderStyle Width="70px" HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="SelectedPhysician" runat="server" Text="Select" CommandName="SelectAffiliation" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Physician ID" DataField="AMDM_Phys_ID" HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="70px" ItemStyle-Width="70px" />
</Columns>
</asp:GridView>
and following is the row bound function for the above grid
Private Sub PhysicianGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles PhysicianGrid.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim selectLink As LinkButton = DirectCast(e.Row.FindControl("SelectedPhysician"), LinkButton)
Dim physId As String = e.Row.Cells(1).Text
If OperationType.Value.Equals("ADD ALLOCATION") Then
selectLink.OnClientClick() = String.Format("DisplayUpdateAllocationDivFromSearch('{0}');", physId)
Else
'selectLink.OnClientClick() = String.Format("DisplayUpdateAllocationDivFromSearchNewPhy('{0}');", physId)
selectLink.CommandArgument = physId
End If
selectLink.CommandArgument = physId
End If
End Sub
when I am clicking on the select link the application gets postback and hitting the page_load function but on the click event handler
Private Sub PhysicianGrid_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles PhysicianGrid.RowCommand
If _requestView.CRTypeId = Resources.PageResources.CRTYPEAddNewPhysician AndAlso Request.Form("__EVENTTARGET").Contains("SelectedPhysician") AndAlso OperationType.Value.Equals("ADD NEW PHY") Then
Dim selectedPhys As Integer = CInt(e.CommandArgument)
Dim tempPhys As PhysicianView = New PhysicianView
tempPhys.LoadSearchPhysician(Master.Environment, selectedPhys, True)
Dim dt As ShipToDetailsDataSet.AMU_SHIPTO_ALLOCATION_VDataTable = New ShipToDetailsDataSet.AMU_SHIPTO_ALLOCATION_VDataTable
If allocationOperation.Value.Equals("UPDATE") Then
If GridSelectedShipToAllocations.Rows.Count > 0 Then
For Each row As GridViewRow In GridSelectedShipToAllocations.Rows
Dim allocationString As String = DirectCast(row.FindControl("TextBoxNewAllocation"), TextBox).Text
SaveGridAllocationChangeForPostback(allocationString)
dt.AddAMU_SHIPTO_ALLOCATION_VRow(CInt(_shipToID), CInt(row.Cells(GridSelectedShipToAllocations_columns.colAMDM_PHYS_ID).Text), row.Cells(GridSelectedShipToAllocations_columns.colPhysician_Name).Text, Nothing, CDec(row.Cells(GridSelectedShipToAllocations_columns.colAllocation).Text.Substring(0, row.Cells(GridSelectedShipToAllocations_columns.colAllocation).Text.Length - 1)), CDate("01/01/2007"), Nothing, "", "", "", row.Cells(GridSelectedShipToAllocations_columns.colMajorSpec).Text.Replace(" ", ""), row.Cells(GridSelectedShipToAllocations_columns.colSecondarySpecialty).Text.Replace(" ", ""), row.Cells(GridSelectedShipToAllocations_columns.colTertiarySpecialty).Text.Replace(" ", ""), "", "", "", "")
Next
End If
dt.AddAMU_SHIPTO_ALLOCATION_VRow(CInt(_shipToID), tempPhys.PhysicianID, String.Format("{0}, {1} {2}", tempPhys.LastName, tempPhys.FirstName, tempPhys.MiddleName), Nothing, 0, CDate("01/01/2007"), Nothing, tempPhys.StatusId, tempPhys.FirstName, tempPhys.LastName, tempPhys.MajorSpec, tempPhys.SECONDARY_SPECIALTY_LD, tempPhys.TERTIARY_SPECIALTY_LD, "", "", "", "")
GridSelectedShipToAllocations.DataSource = dt
GridSelectedShipToAllocations.DataBind()
GridSelectedShipToAllocations.Style("display") = "block"
'DivAddAllocationChange.Style("display") = "none"
LabelTotal.Style("display") = "block"
ScriptManager.RegisterStartupScript(Me.PhysicianGrid, GetType(Page), "hideSearchPhysDiv", "DisplayUpdateAllocationDivFromSearchNewPhy('" + selectedPhys.ToString + "');", True)
End If
End If
End Sub
how can I get it to hit the row command function
P.S. I need to use the EnableViewState ="False" attribute in the grid I cant remove this attribute. Is there any workaround to this issue?
This is how I achieved it in a recent project, may be it could help you find the problem. The command argument has been specified in the ASPX instead of RowDataBound event.
As a work around you may just comment out the code in your RowDataBound event and set CommandArgument in ASPX and check what happens.
Also I think instead of Request.Form("__EVENTTARGET").Contains("SelectedPhysician") you may try e.CommandName.Equals("SelectedPhysician")
ASPX:
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false">
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="BtnUnlock" runat="server" Text="Unlock" CausesValidation="True" CommandName="UnlockProfile" CommandArgument='<%# Eval("User_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
CodeBehind:
Private Sub gvStudents_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvStudents.RowCommand
If e.CommandName.Equals("UnlockProfile") Then
Dim lnUser_ID As Integer = Convert.ToInt32(e.CommandArgument)
...
ElseIf e.CommandName.Equals("LockProfile") Then
...
End If
End Sub

How to hide ImageButton based on Row Cell value

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

Checkbox on Datagrid for ASP

I have a data grid on my project with a Checkbox as a TemplateField; but I can't acces the checkbox.checked property. Does anyone have any idea?
My ASP code:
<asp:GridView ID="GVP" runat="server" AutoGenerateColumns="False" DataSourceID="DSP">
<Columns>
<asp:TemplateField HeaderStyle-Width="5%" ItemStyle-Width="5%" FooterStyle-Width ="5%">
<ItemTemplate>
<asp:CheckBox ID="SelectCb" runat="server"></asp:CheckBox>
</ItemTemplate>
<FooterStyle Width="5%"/>
<HeaderStyle Width="5%"/>
<ItemStyle Width="5%"/>
</asp:TemplateField>
<asp:BoundField DataField="Answers" HeaderText="Options" SortExpression="Answers" />
</Columns>
</asp:GridView>
My VB code behind:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click
Dim SelectedBox As Boolean = False
For Each row As GridViewRow In GVP.Rows
Dim cb As CheckBox = row.FindControl("SelectCb")
If cb IsNot Nothing AndAlso cb.Checked Then
SelectedBox = True
Dim RID As Integer = Convert.ToInt32(GVP.DataKeys(row.RowIndex).Value)
Else
ShowMessage("You did not select anything")
End if
try this:
For Each row As GridViewRow In gvTest.Rows
Dim cb As CheckBox = row.FindControl("SelectCb")
If (CType(row.FindControl("SelectCb"), CheckBox)).Checked = True Then
SelectedBox = True
Dim RID As Integer = Convert.ToInt32(gvTest.DataKeys(row.RowIndex).Value)
End If
Next
It's hard to tell what you are trying to do here and how you are testing but my guess is that this is because you are not checking for row type. So the first row is actually the header and therefore will not have a checkbox at all (and you will get the message).
For Each row As GridViewRow In GVP.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim cb As CheckBox ...
The problem really resided on the Page_Load where I was binding the grid to the datasource; I deleted it and the problem was solved.

Why isn't the new values pulling up when I update the GridViewRow?

So what's happening is that I click the Edit button, type the updated values and hit Update. But the code-behind gets the original values not the updated values. I can't figure out why. It's always worked before.
Markup
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
CellPadding="7" ForeColor="#333333" GridLines="None" Font-Size="Small"
ShowFooter="True" DataKeyNames="CapID">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="AllocationId">
<ItemTemplate>
<asp:Label ID="show" runat="server" Text='<%# Eval("CapID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reference Number">
<ItemTemplate>
<asp:Label ID="showRefNo" runat="server" Text='<%# Eval("RefNo") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditRefNo"
Text='<%# Bind("RefNo") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="InsertRefNo"
Text='<%# Bind("RefNo") %>'/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Resource">
<ItemTemplate>
<asp:Label ID="showResource" runat="server"
Text='<%# Eval("Resource") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditResource"
Text='<%# Bind("Resource") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="InsertResource"
Text='<%# Bind("Resource") %>'/>
</FooterTemplate>
</asp:TemplateField>
<!-- and so on... -->
</Columns>
<!-- styles etc -->
<EmptyDataTemplate>
Ref Num: <asp:TextBox ID="newRefNo" runat="server"/>
Resource: <asp:TextBox ID="newResource" runat="server"/>
Hours Allocated: <asp:TextBox ID="newHours" runat="server"/>
Week Offset: <asp:TextBox ID="newOffset" runat="server"/>
<asp:Button runat="server" ID="NewDataInsert"
CommandName="NewDataInsert" Text="Insert"/>
</EmptyDataTemplate>
</asp:GridView>
Code Behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not IsPostBack Then
GridView1_DataBind()
GridView2_DataBind()
End If
End Sub
Protected Sub GridView2_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs) Handles GridView2.RowUpdating
Dim capID As Label = GridView2.Rows(e.RowIndex).Cells(0)
.FindControl("show")
Dim refNo As TextBox = GridView2.Rows(e.RowIndex).Cells(1)
.FindControl("EditRefNo")
Dim resource As TextBox =
GridView2.Rows(e.RowIndex).Cells(2).FindControl("EditResource")
Dim hours As TextBox =
GridView2.Rows(e.RowIndex).Cells(3).FindControl("EditHours")
Dim offSet As TextBox =
GridView2.Rows(e.RowIndex).Cells(4).FindControl("EditOffset")
Dim newResAlloc As DataTable = resourceInfo.loadResAllocations
Dim updateRows As DataRow() =
newResAlloc.Select("CapID = " & "'" & capID.Text & "'")
If (Not updateRows Is Nothing) And updateRows.Length > 0 Then
For Each updRow As DataRow In updateRows
updRow.BeginEdit()
updRow.Item("Refno") = refNo.Text
updRow.Item("Resource") = resource.Text
updRow.Item("Hours") = hours.Text
updRow.Item("Offset") = offSet.Text
updRow.EndEdit()
Next
End If
resourceInfo.updateResAllocations(newResAlloc)
GridView2.EditIndex = -1
GridView2_DataBind()
End Sub
This could be a million and one things. What have you checked? Have you narrowed it down?
Here's some starting points for you:
Put a breakpoint inside GridView2_RowUpdating to make sure it's being called.
Check the value of capID, or capID.Text - it shouldn't be 0.
Check that your database table contains a row where CapID equals the value of capID.Text
Put a breakpoint on updateRows to ensure that the database row is being loaded into your code.
Make sure that the updRow.EndEdit() is being hit, and that the values are populated into the row correctly.
Finally, check that the updateResAllocations is working properly. It's impossible to see from here how it works, so I can't give any advice on that.
The easiest way to fix this might be to ask whoever wrote it for some assistance.
The problem was that my RowCommand method was calling the DataBind
Wrong way:
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "NewDataInsert" Then
Dim refNo As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewRefNo")
Dim resource As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewResource")
Dim hours As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewHours")
Dim offset As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewOffset")
Dim newResAlloc As DataTable = resourceInfo.loadResAllocations
Dim newAllocRow As DataRow = newResAlloc.NewRow
newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text}
newResAlloc.Rows.Add(newAllocRow)
resourceInfo.updateResAllocations(newResAlloc)
ElseIf e.CommandName = "InsertNew" Then
Dim refNo As TextBox = GridView2.FooterRow.FindControl("InsertRefNo")
Dim resource As TextBox = GridView2.FooterRow.FindControl("InsertResource")
Dim hours As TextBox = GridView2.FooterRow.FindControl("InsertHours")
Dim offset As TextBox = GridView2.FooterRow.FindControl("InsertOffset")
Dim newResAlloc As DataTable = resourceInfo.loadResAllocations
Dim newAllocRow As DataRow = newResAlloc.NewRow
newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text}
newResAlloc.Rows.Add(newAllocRow)
resourceInfo.updateResAllocations(newResAlloc)
End If
GridView2_DataBind() 'Culprit
End Sub
Right way:
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "NewDataInsert" Then
Dim refNo As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewRefNo")
Dim resource As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewResource")
Dim hours As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewHours")
Dim offset As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewOffset")
Dim newResAlloc As DataTable = resourceInfo.loadResAllocations
Dim newAllocRow As DataRow = newResAlloc.NewRow
newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text}
newResAlloc.Rows.Add(newAllocRow)
resourceInfo.updateResAllocations(newResAlloc)
GridView2_DataBind() 'Only called if IF is true
ElseIf e.CommandName = "InsertNew" Then
Dim refNo As TextBox = GridView2.FooterRow.FindControl("InsertRefNo")
Dim resource As TextBox = GridView2.FooterRow.FindControl("InsertResource")
Dim hours As TextBox = GridView2.FooterRow.FindControl("InsertHours")
Dim offset As TextBox = GridView2.FooterRow.FindControl("InsertOffset")
Dim newResAlloc As DataTable = resourceInfo.loadResAllocations
Dim newAllocRow As DataRow = newResAlloc.NewRow
newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text}
newResAlloc.Rows.Add(newAllocRow)
resourceInfo.updateResAllocations(newResAlloc)
GridView2_DataBind() 'Only called if IF is true
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