This is a migrated thread and some comments may be shown as answers.

Hide Button based on Database Value

1 Answer 37 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 2
Allan asked on 08 Feb 2011, 05:46 PM
I am attempting to hide a button in a radgrid template column based on a value selected from a sql query. Here is what I am attempting to accomplish.

I have two grids on a page (rgd_Authorizers) and (rgd_Approvals)

The rgd_Authorizers is a list of managers, each of whom must approve the page's information by clicking btn_approve contained in a template column. When they click btn_approve, I add a record to the approval table.

This record is then displayed in rgd_Approvals showing that that manager has approved the page.

The goal is to have the btn_approve to no longer be visible next to that manager's name in rgd_Authorizers so that they cannot re-approve the page.

Protected Sub rgd_Authorizers_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgd_Authorizers.ItemCreated
    Dim IDPurchaseRequisition = Request.QueryString("IDPurchaseRequisition")
    Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("Purchasing_PRS_ConnectionString").ConnectionString, String)
    Dim conn As New SqlConnection(connectionString)
    Dim comm As New SqlCommand("SELECT * FROM [PRS_PurchaseRequisitionsApprovals] WHERE IDPurchaseRequisition = @IDPurchaseRequisition", conn)
    comm.Parameters.Add("@IDPurchaseRequisition", SqlDbType.Int).Value = IDPurchaseRequisition
    Dim myDataAdapter As New SqlDataAdapter(comm)
    Dim myDataSet As New DataSet
    Dim dtData As New DataTable
    Dim dtRow As DataRow
    For Each dtRow In myDataSet.Tables(0).Rows
        Dim IDAuthorizer = dtRow.Item("IDAuthorizer")
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
            Dim value1 As String = item("IDAuthorizer").Text
            If value1 = IDAuthorizer Then
                Dim btn_Approve As Button = DirectCast(item("TemplateColumn").Controls(0), Button)
                btn_Approve.Visible = False
            End If
        End If
End Sub

Here is what I have attempted so far. I get no errors, however, the button is still visible. Any help much appreciated.

1 Answer, 1 is accepted

Sort by
Top achievements
Rank 1
answered on 09 Feb 2011, 05:01 AM
Hello Alan,

ItemCreated is fired before the item is data-bound. Thus no data is still in the cells' text or input controls. In ItemDataBound all is available. so try the same in ItemDataBound event other than ItemCreated.

Refer the documentation for more on these events.
Distinguishing the major differences between ItemCreated and ItemDataBound events

Asked by
Top achievements
Rank 2
Answers by
Top achievements
Rank 1
Share this question