Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this behind code
VB
Protected Sub gvData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvData.RowCommand
       'lblError.Text = ""

       Dim oButton As Button = fvData.FindControl("BtnRevisi")

       'Dim oTextbox As TextBox = DirectCast(Me.fvData.FindControl("txtRevisi"), TextBox)
       lblError.Visible = False
       If e.CommandName = "Select" Then
           fvData.ChangeMode(FormViewMode.Edit)
           fvData.Caption = "Edit Data"
           oButton.Visible = False
       End If
       If e.CommandName = "Revisi" Then

       End If

   End Sub


this is on html
ASP.NET
<pre><asp:FormView ID="fvData" runat="server" DataSourceID="sdsForm"
    DefaultMode="Edit" Width="100%" DataKeyNames="ID">
    <EditItemTemplate>
        <table cellpadding=0 cellspacing=0 width="100%">
            <tr>
                <td colspan="2">
                    <hr />
                </td>
            </tr>

            <tr>
                <td colspan="2">
                     </td>
            </tr>
            <tr>
                <td colspan="2" class="FooterStyle">
                    <uc1:BtnUpdate ID="BtnUpdate1" runat="server" />
                    <uc2:BtnDelete ID="BtnDelete1" runat="server" />
                    <uc3:BtnCancel ID="BtnCancel1" runat="server" />

                    <asp:Button ID="btnRevisi" runat="server" OnClick="btnSave_Click" Style="font-size: 8pt; cursor: pointer; background-position: left; background-image: url(../images/Save.gif); background-repeat: no-repeat; text-align: right;" Text="Revisi" ToolTip="Save" ValidationGroup="Pricing" Width="55px" />

                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <hr />
                </td>
            </tr>
        </table>
    </EditItemTemplate> 


in essence I just want to grab the id property from the btnRevisi, because the location of the btnRevisi is in the formview, so the id of the btnRevisi can not be called. and display error after the run is btnRevisi it is nothing Object reference not set to an instance of an object.

What I have tried:

I have declare by using DirectCast, the result is the same still (Object reference not set to an instance of an object). like this the code Dim oButton As Button = fvData.FindControl("btnRevisi")

oButton.Visible = False in this section its an error
Posted
Updated 10-May-18 6:07am
Comments
CHill60 10-May-18 6:03am    
Has the button actually been rendered on the page when this code is being called?
Member 10843905 10-May-18 6:46am    
yes, i think so. i mean this problem is how to get btnRevisi ID when I click command button in gridview

when CommandField click any error on this section
oButton.Visible = False


this code on gridview :
<asp:GridView ID="gvData" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="sdsGrid" Width="100%"
DataKeyNames="ID" Caption="PreCosting" RowHeaderColumn="ID">
<columns>

<asp:CommandField ButtonType="Image" SelectImageUrl="~/images/edit.gif" ShowSelectButton="True">
<ItemStyle Width="20px" HorizontalAlign="Center" />



<emptydatatemplate>
<uc6:NoDataFound ID="NoDataFound1" runat="server" />

1 solution

The button you're trying to find is in the EditItemTemplate.

The controls in that template aren't created until the FormView is changed to "edit" mode and bound to its data source.

Something like this should work:
VB.NET
If e.CommandName = "Select" Then
    fvData.Caption = "Edit Data"
    fvData.ChangeMode(FormViewMode.Edit)
    fvData.DataBind()
    
    Dim oButton As Button = fvData.FindControl("btnRevisi")
    oButton.Visible = False
End If

However, if all you're doing is setting the visibility of the button, you can do that in the markup:
ASP.NET
<asp:Button ID="btnRevisi" runat="server" Visible="False" ...
 
Share this answer
 

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