简体   繁体   中英

Find ButtonField in Gridview's Row DataBound Event

Here is my Inline Code:

<asp:GridView ID="GrdVacation" runat="server" DataKeyNames="ID" AutoGenerateColumns="False">
  <Columns>

   <asp:TemplateField HeaderText="S.No">
               <HeaderTemplate>
               Sno</HeaderTemplate>
               <ItemTemplate>
               <%#Container.DataItemIndex + 1%>
               </ItemTemplateField>
   </asp:TemplateField>
   <asp:BoundField HeaderText="Badge No" DataField="EmpBadge" />
   <asp:BoundField HeaderText="Last Vacation Date" DataField="LastVacDate" DataFormatString="{0:dd-MMM-yyyy}" />
   <asp:BoundField HeaderText="Vacation Expiry Date" DataField="VacValidity" DataFormatString="{0:dd-MMM-yyyy}" />
   <asp:BoundField HeaderText="Vacation Start Date" DataField="VacStartDate" DataFormatString="{0:dd-MMM-yyyy}" />
   <asp:BoundField HeaderText="Vacation End Date" DataField="VacEndDate" DataFormatString="{0:dd-MMM-yyyy}" />
   <asp:BoundField HeaderText="13 Salary Request" DataField="E13SalRequest" />
   <asp:ButtonField ButtonType="Image" CommandName="select" HeaderText="Edit" ImageUrl="~/images/Edit.png"></asp:ButtonField>

</Columns>
</asp:GridView>

I am about to Change the Image URL of the ButtonFiled on some condition in GridView RowDataBound event.

The Code what i have tried so far,

   Protected Sub GrdVacation_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdVacation.RowDataBound

    If (e.Row.RowType = DataControlRowType.DataRow) Then
    Dim NM = CType(e.Row.Cells(0).Controls(7), ImageButton)

     if(true) Then
        NM.ImageURL="somepath"
     End If

I am getting exception as Specified argument was out of the range of valid values. Please suggest me what went wrong.

Change

Dim NM = CType(e.Row.Cells(0).Controls(7), ImageButton)

to

Dim NM = CType(e.Row.Cells(7).Controls(0), ImageButton)

Like this:

Protected Sub GrdVacation_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdVacation.RowDataBound

    If (e.Row.RowType = DataControlRowType.DataRow) Then
    //// Dim NM = CType(e.Row.Cells(0).Controls(7), ImageButton)
    Dim NM = CType(e.Row.Cells(7).Controls(0), ImageButton)


     if(true) Then
        NM.ImageURL="somepath"
     End If

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