Hide Button based on Database Value

2 posts, 1 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 08 Feb 2011 Link to this post

    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.Connection.Open()
        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
        myDataAdapter.Fill(myDataSet)
        conn.Close()
        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
        Next
    End Sub

    Here is what I have attempted so far. I get no errors, however, the button is still visible. Any help much appreciated.
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Feb 2011 Link to this post

    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

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top