简体   繁体   中英

Problems making LinkButton visible in certain rows only within a GridView

I have set up a GridView as set out below. You can see there are four LinkButton's per row. What I want to do is make certain LinkButtons available depending on which row they appear in.

<asp:GridView ID="FormsGrid" runat="server" Width="657px" Height="250px" DataKeyNames="FORM_NAME,FORM_ACCESS,STATUS,ID"
               AutoGenerateColumns="False" DataSourceID="SqlDataSource1" >
             <Columns>
                 <asp:BoundField DataField="DEADLINE_DATE" HeaderText="Date" DataFormatString="{0:d}"
                     SortExpression="DEADLINE_DATE" />
                 <asp:BoundField DataField="FORM_NAME" HeaderText="Event" 
                     SortExpression="FORM_NAME" >                     
                     <ItemStyle Width="240px" />
                 </asp:BoundField>
                 <asp:BoundField DataField="COMPULSORY" HeaderText="Compulsory?" 
                     SortExpression="COMPULSORY" />
                 <asp:BoundField DataField="FORM_NO" HeaderText="Form" 
                     SortExpression="FORM_NO" />     
                 <asp:TemplateField HeaderText="Access">
                     <ItemTemplate>
                         <asp:LinkButton ID="FormLinkBtn" runat="server"  
                             onclick="FormLinkBtn_Click">Form Link</asp:LinkButton>
                         <br />
                         <asp:LinkButton ID="NotReqBtn" runat="server"  
                             onclick="NotReqBtn_Click">Not Required</asp:LinkButton>
                         <br />
                         <asp:LinkButton ID="DnLoadBtn" runat="server"  
                             onclick="DnLoadBtn_Click">Download Pdf</asp:LinkButton>
                         <br />
                         <asp:LinkButton ID="UploadBtn" runat="server"  
                             onclick="UploadBtn_Click">Upload Pdf</asp:LinkButton>                             
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="STATUS" HeaderText="Completed?" 
                     SortExpression="STATUS" />
                 <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False" />
             </Columns>
         </asp:GridView>

When I try to make them not visible use the code below in FormsGrid_RowDataBound, it doesn't work and a get a null value error.

if e.Row.Cells(4).Text = "1" then
   Dim FLbtn As LinkButton = FormsGrid.FindControl("FormLinkBtn")
   FLbtn.Visible = True
   Dim NRbtn As LinkButton = FormsGrid.FindControl("NotReqBtn")
   NRbtn.Visible = False
   Dim DLbtn As LinkButton = FormsGrid.FindControl("DnLoadBtn")
   DLbtn.Visible = False
   Dim ULbtn As LinkButton = FormsGrid.FindControl("UploadBtn")
   ULbtn.Visible = False
end if

Also when I click say the FormLinkBtn, how do I determine the value of the fields in that particualr row?

Suggestion #1:

wrap your "if" statement in an "if" that checks for a row type: if (e.Row.RowType == DataControlRowType.DataRow) {......// your code here } You getting null value exception becuase first you hit the header row. There are few types of rows. You are only interested in DataRow in this case.

On question part 2:

if you go with just a link buttons and click events you will need to pass some data in CommandArgument or CommandName properties of the linkButton. You can use CommandField or ButtonField instead to avoid dealing with passing data identifying the row in CommandArgument or CommandName.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM