Link to home
Start Free TrialLog in
Avatar of zebasdad
zebasdad

asked on

itemtemplate with button and parameters to enable/disable button control

ASP.net Gridview...  How can I enable / disable a buton in a cell of a gridvew control on an asp.net page?  I'm sure it will involve the itemtemplate... but I'm finding it difficult to find a good refernece on its use.

Scenario:
Button   -  Shpped  -  123 Main Street... bla bla bla
Button  -  PENDING  -  435 Main Street... bla bla bla

I want the button to show up... but be disabled... of maybe I'll greate another image, too for a 'grayed-out' button... but I want the button disabled based on the status field... this grid is connected to a sqldatasource... Please show me detail...

Thanks!!!
Avatar of igor_alpha
igor_alpha

Hi zebasdad,
You have to add event handler to RowDataBound event of GridView. On event handel you would check out status field and set button field to disabled or not:

protected void gvYourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv  = e.Row.DataItem as DataRowView;
                if (drv["status"].ToString()=="PENDING")
                {
                    ((Button) e.Row.Cells[0].Controls[0]).Enabled = false;
                }
            }
        }
Avatar of zebasdad

ASKER

Can you translate to VB,  please?
I came up with this... but I'm getting an error: Type 'DataRowView' is not defined... ??

Protected Sub gvYourGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim drv As DataRowView = CType(e.Row.DataItem,DataRowView)
            If (drv("status").ToString = "PENDING") Then
                CType(e.Row.Cells(0).Controls(0),Button).Enabled = false
            End If
        End If
    End Sub

ASKER CERTIFIED SOLUTION
Avatar of igor_alpha
igor_alpha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS... THAT WORKED WELL... I HAD TO INCLUDE .TRIM ON THE ' If (drv("status").ToString ' STATEMENT... AND ALSO I HAD TO ACTUALLY CLICK ON THE LIGHTENING BOLT TO ASSIGN THE EVENT... BUT AFTER ALL THAT, IT'S DOING EXACTLY WHAT I WANT IT TO...   NOW I'M WORKING ON INCORPORATING A MOUSEOVER EVENT TO DISPLAT A FIELD - NOTES...

THANKS!