Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datatable that is binding to Gridview. Now in that Datatable if a column["File"] has File name then Enable linkbutton for that respective row in Gridview, if column["File"] doesn't have Filename then disable Linkbutton of that row in Gridview.

Here is what I tried

VB
Protected Sub gdvLineItems_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
     Dim rowIndex As Integer = 0
     If (e.Row.RowType = DataControlRowType.DataRow) Then

         Dim dt As DataTable = ViewState("Grid")

         For i As Integer = 1 To dt.Rows.Count
            Dim lnk As LinkButton = DirectCast(e.Row.FindControl("btndownload"), LinkButton)
             If Not dt.Rows(rowIndex)("File") Is String.Empty Then
                 lnk.Enabled = True
             Else
                 lnk.Enabled = False
             End If

             rowIndex += 1
         Next

     End If
 End Sub


Note: My Datatable gets updated each time someone enters data, and each row is appended to Gridview.
Posted

1 solution

Put breakpoint on Linkbutton cast row and have a look. Also common problem with strings is the multiple things that can happen. You are testing for empty string/zero length string. Could be null, could be space, etc. Use locals window with breakpoint or just assign property to string in code. Must be sure what you have.
 
Share this answer
 
Comments
sudevsu 8-Jan-15 8:40am    
First of all, do you think that is the right approach... I mean the code? because I am new to .Net so just wondering if there could be some other options to make it happen.
.Net Jim 8-Jan-15 14:59pm    
OK. Something else has come to my attention. You are mixing things up and this is why things are firing multiple times.

ViewState is a cache for the entire web page. It is not advisable to tap into it from a multiple event situation like rowdatabound. You should tap into your rows of data by grabbing data from sender. Sender is a copy of the control creating the event, and the args are the settings for the object, not the data.

Follow how you see it done here, making sure to search for your control, then set the value. Also, this works much better if you set some column templates with names for the controls.

I will try to find a youtube tutorial later, but this article will give you plenty of code to tap into your grid from sender object. Viewstate should only be done once or twice on page from event that only fires once or twice during page load sequence of events, maybe from button click. Rowdatabound is called with every row of your grid, so performance will slow down also.

http://www.codeproject.com/Answers/305101/Set-Eval-in-Code-Behind#answer2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900