I’m new in vb.net. I have some questions how to implement this..I have gridview and the button will change depending on the column status..if it’s status is “Blocked” the button will be “Unblock” and if the status is blank the button will be “Block”. My other problem is after clicking the button the selected row button will update in the database and change the status to blocked if user clicked the “Block” then again the button will change again to “Unblock” button and vice versa. Your help is much appreciated..
Status | Unblock/Block
(blank)| Block(button)
Blocked| Block(it should be Unblock button)
What’s wrong with this code..why is it not working?
<asp:BoundField DataField="AllowStatus"
HeaderText="" ReadOnly="True"
SortExpression="AllowStatus">
<ItemStyle Width="300px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Block/Unblock">
<ItemTemplate>
<asp:Button ID="BlockButton" runat="server" CausesValidation="false" CommandName="" Text="Block" />
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) ' Handles GridView1.RowDataBound **<<ooops my mistake I comment out this >.< ..that's why it's not working**
If e.Row.RowType = DataControlRowType.DataRow Then
Dim allowstatus As String = (DataBinder.Eval(e.Row.DataItem, "AllowStatus")).ToString
Dim btn As Button = DirectCast(e.Row.FindControl("BlockButton"), Button)
If allowstatus = "Blocked" Then
btn.Text = "Unblocked"
End If
End If
End Sub
handle GridView1_RowDataBound event. Based on value of column (blocked/EmptyString), use FindControl to locate button and change its text to block/unblock